-
Notifications
You must be signed in to change notification settings - Fork 0
/
Injector.ino
88 lines (72 loc) · 1.92 KB
/
Injector.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
/*
Serial Event example
When new serial data arrives, this sketch adds it to a String.
When a newline is received, the loop prints the string and
clears it.
A good test for this is to try it with a GPS receiver
that sends out NMEA 0183 sentences.
Created 9 May 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/SerialEvent
*/
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
const byte INJ_PIN = 2;
const byte LED_PIN = 13;
volatile byte sendMsg = 0;
volatile byte state = 1;
void setup() {
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
pinMode(INJ_PIN, INPUT_PULLUP);
attachInterrupt(0, changeState, CHANGE);
}
void loop() {
// print the string when a newline arrives:
if (stringComplete) {
if (inputString == "INJ?\r") {
sendMsg = 1;
inputString = "";
} else {
Serial.print(inputString);
Serial.print("\r");
// clear the string:
inputString = "";
stringComplete = false;
}
}
if (sendMsg) {
delay(200);
if (digitalRead(INJ_PIN)){
Serial.print("INJ:L\r");
} else {
Serial.print("INJ:I\r");
}
sendMsg = 0;
}
}
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\r') {
stringComplete = true;
}
}
}
void changeState() {
sendMsg = 1;
}