aboutsummaryrefslogtreecommitdiff
path: root/arduino/rpmcount.ino
diff options
context:
space:
mode:
Diffstat (limited to 'arduino/rpmcount.ino')
-rwxr-xr-xarduino/rpmcount.ino147
1 files changed, 147 insertions, 0 deletions
diff --git a/arduino/rpmcount.ino b/arduino/rpmcount.ino
new file mode 100755
index 0000000..51e6bfe
--- /dev/null
+++ b/arduino/rpmcount.ino
@@ -0,0 +1,147 @@
1#include "config.h"
2#include "Counter.h"
3
4#define RPM_INPUT 2
5#define START_BUTTON 3
6#define COUNTING_LED 4
7#define TIMEOUT_LED 5
8#define READY_LED 6
9#define SIGNAL_PIN 7
10
11enum ErrorCode
12{
13 Ok,
14 Error
15};
16
17enum MessageHeader
18{
19 ReadConfig,
20 UpdateStartDelay,
21 UpdateRPMCount,
22 UpdateSignalsPerRPM
23};
24
25Config config;
26Counter counter;
27
28void setup ()
29{
30 Serial.begin(9600);
31
32 pinMode(RPM_INPUT, INPUT);
33 pinMode(START_BUTTON, INPUT);
34 pinMode(COUNTING_LED, OUTPUT);
35 pinMode(TIMEOUT_LED, OUTPUT);
36 pinMode(READY_LED, OUTPUT);
37 pinMode(SIGNAL_PIN, OUTPUT);
38 config = readConfig();
39 counter.init(config.startDelay, config.rpmCount, config.signalsPerRPM);
40 digitalWrite(READY_LED, HIGH);
41}
42
43void updateSerial ()
44{
45 unsigned long val = -1;
46 bool sendACK = false;
47 byte code;
48
49 while (Serial.available() > 0)
50 {
51 MessageHeader header = (MessageHeader) Serial.read();
52 switch (header)
53 {
54 case ReadConfig:
55 code = Ok;
56 Serial.write(code);
57 Serial.write((const uint8_t*) &config.startDelay, sizeof(config.startDelay));
58 Serial.write((const uint8_t*) &config.rpmCount, sizeof(config.rpmCount));
59 Serial.write((const uint8_t*) &config.signalsPerRPM, sizeof(config.signalsPerRPM));
60 Serial.flush();
61 break;
62
63 case UpdateStartDelay:
64 Serial.readBytes((char*)&val, sizeof(val));
65 config.startDelay = val;
66 writeConfig(config);
67 sendACK = true;
68 break;
69
70 case UpdateRPMCount:
71 Serial.readBytes((char*)&val, sizeof(val));
72 config.rpmCount = val;
73 writeConfig(config);
74 sendACK = true;
75 break;
76
77 case UpdateSignalsPerRPM:
78 Serial.readBytes((char*)&val, sizeof(val));
79 config.signalsPerRPM = val;
80 writeConfig(config);
81 sendACK = true;
82 break;
83
84 default:
85 code = Error;
86 Serial.write(code);
87 Serial.flush();
88 break;
89 }
90 }
91
92 if (sendACK)
93 {
94 code = Ok;
95 Serial.write(code);
96 Serial.write((const uint8_t*) &val, sizeof(val));
97 Serial.flush();
98 }
99}
100
101void loop ()
102{
103 Counter::State state = counter.getState();
104 if (state == Counter::READY)
105 {
106 if (digitalRead(START_BUTTON) == HIGH)
107 {
108 unsigned long t = millis();
109 counter.start(t);
110 digitalWrite(READY_LED, LOW);
111 digitalWrite(TIMEOUT_LED, HIGH);
112 }
113 else updateSerial();
114 }
115 else if (state == Counter::WAITING_FOR_TIMEOUT)
116 {
117 unsigned long t = millis();
118 int rpmSignal = digitalRead(RPM_INPUT);
119 counter.update(t, rpmSignal);
120 if (counter.getState() == Counter::COUNTING)
121 {
122 digitalWrite(TIMEOUT_LED, LOW);
123 digitalWrite(COUNTING_LED, HIGH);
124 }
125 }
126 else if (state == Counter::COUNTING)
127 {
128 unsigned long t = millis();
129 int rpmSignal = digitalRead(RPM_INPUT);
130 counter.update(t, rpmSignal);
131 if (counter.getState() == Counter::SIGNALING)
132 {
133 digitalWrite(COUNTING_LED, LOW);
134 digitalWrite(SIGNAL_PIN, HIGH);
135 }
136 }
137 else if (state == Counter::SIGNALING)
138 {
139 unsigned long t = millis();
140 counter.update(t, LOW);
141 if (counter.getState() == Counter::READY)
142 {
143 digitalWrite(SIGNAL_PIN, LOW);
144 digitalWrite(READY_LED, HIGH);
145 }
146 }
147}