-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArduino.ino
109 lines (89 loc) · 1.89 KB
/
Arduino.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <Wire.h>
#define SLAVE_ADDRESS 0x04
#define IGNITION_PIN A0
#define WAKE_PIN A5
#define TURN_ON_TIME 0 //wait millis till pi starts
#define SHUT_DOWN_TIME 15000 // wait millis till pi stops
int number = 0;
int state = 0;
int piAlive = 0;
unsigned long ignitionTurnedOn = 0;
unsigned long ignitionTurnedOff = 0;
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
pinMode(IGNITION_PIN, INPUT);
digitalWrite(IGNITION_PIN, HIGH);
Serial.begin(9600); // to enable outputinitialize
Serial.println("Ready");
}
void setupI2C()
{
Wire.begin(SLAVE_ADDRESS); // i2c as slave
Wire.onReceive(receiveData);
Wire.onRequest(sendData);
}
void wakePi()
{
TWCR = 0; // reset TwoWire Control Register to default, inactive state
pinMode(WAKE_PIN, OUTPUT);
digitalWrite(WAKE_PIN, LOW);
delay(500);
digitalWrite(WAKE_PIN, HIGH);
pinMode(WAKE_PIN, INPUT);
setupI2C();
}
void loop()
{
int ignition = analogRead(IGNITION_PIN);
if(ignition < 200)
{
ignitionTurnedOn = 0;
if (ignitionTurnedOff == 0)
{
ignitionTurnedOff = millis();
}
else if ((millis()- ignitionTurnedOff) > SHUT_DOWN_TIME)
{
digitalWrite(LED_BUILTIN, LOW);
number = 0;
Serial.println("shutdown");
}
}
else if (ignition > 800)
{
ignitionTurnedOff = 0;
if (ignitionTurnedOn == 0)
{
ignitionTurnedOn = millis();
}
if ((millis() - ignitionTurnedOn) > TURN_ON_TIME && number == 0)
{
Serial.println("startPi");
digitalWrite(LED_BUILTIN, HIGH);
// to resume to try starting if pi is not responding
if (piAlive == 0)
{
wakePi();
}
else
{
number = 1;
}
}
}
delay(100);
}
//callback for received data
void receiveData(int byteCount)
{
while(Wire.available())
{
piAlive = Wire.read();
}
}
//callback for sending data
void sendData()
{
Wire.write(number);
}