Skip to content

Commit 2ac4b87

Browse files
committed
Change hardware configuration: 1 switch for sleep mode, 1 for unit select
+ clean up some of the code
1 parent ea24246 commit 2ac4b87

File tree

2 files changed

+50
-39
lines changed

2 files changed

+50
-39
lines changed

DigitalSpeedometer/DigitalSpeedometer.h

+8-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#define STATE_CONNECTED 0x2
88
#define STATE_SLEEPING 0x3
99

10+
#define UNIT_MPH HIGH
11+
#define UNIT_KMH LOW
12+
1013
#define TIMER_INTERVAL_DISP_REFRESH_MS 10
1114
#define TIMER_INTERVAL_DISP_INC_MS 500
1215

@@ -22,10 +25,10 @@
2225
#define PIN_DIG_1 A1
2326
#define PIN_DIG_2 A2
2427

25-
#define PIN_USE_IMPERIAL 2
26-
#define PIN_USE_METRIC 3
28+
#define PIN_SPEED_ADJUST A0
2729

28-
#define PIN_SPEED_ADJUST 0
30+
#define PIN_UNIT_SELECT 2
31+
#define PIN_SLEEP_ENABLE 3
2932

3033
//#define MODE_SIMULATION
3134

@@ -39,7 +42,8 @@ void setup_obd_connection();
3942

4043
void isr_display();
4144
void isr_refresh_display();
42-
void isr_read_display_unit();
45+
void isr_check_display_unit();
46+
void isr_check_sleep_mode();
4347

4448
speed_t adjust_speed(speed_t);
4549
void set_displayed_speed(speed_t);

DigitalSpeedometer/DigitalSpeedometer.ino

+42-35
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include <Arduino.h>
2+
13
#include <avr/sleep.h>
24
#include <TimerOne.h>
35
#include <MsTimer2.h>
@@ -13,16 +15,15 @@ COBD obd;
1315
volatile state_t state;
1416
volatile speed_t target_read_speed;
1517

16-
float modifier;
17-
volatile bool should_display_imperial;
18-
volatile bool should_display_metric;
18+
volatile float modifier;
19+
volatile int unit_select;
1920

2021
void setup() {
2122
state = STATE_DISCONNECTED;
2223
target_read_speed = 0;
2324

24-
pinMode(PIN_USE_IMPERIAL, INPUT);
25-
pinMode(PIN_USE_METRIC, INPUT);
25+
pinMode(PIN_UNIT_SELECT, INPUT);
26+
pinMode(PIN_SLEEP_ENABLE, INPUT);
2627

2728
// Read speed modifier (1.0 keeps raw speed read from OBD)
2829
// OBD speed can be a bit different from real life speed, so play with
@@ -39,8 +40,9 @@ void setup() {
3940
setup_timers();
4041
setup_interrupts();
4142

42-
// Initialize display unit switch interrupt
43-
isr_read_display_unit();
43+
// Initialize display unit switch and sleep mode interrupts
44+
isr_check_display_unit();
45+
isr_check_sleep_mode();
4446
}
4547

4648
void setup_timers() {
@@ -55,9 +57,9 @@ void setup_timers() {
5557
}
5658

5759
void setup_interrupts() {
58-
// When switching kph/OFF/mph, update the switch states in memory
59-
attachInterrupt(digitalPinToInterrupt(PIN_USE_IMPERIAL), isr_read_display_unit, CHANGE);
60-
attachInterrupt(digitalPinToInterrupt(PIN_USE_METRIC), isr_read_display_unit, CHANGE);
60+
// Update the switch states in memory when they're changed
61+
attachInterrupt(digitalPinToInterrupt(PIN_UNIT_SELECT), isr_check_display_unit, CHANGE);
62+
attachInterrupt(digitalPinToInterrupt(PIN_SLEEP_ENABLE), isr_check_sleep_mode, CHANGE);
6163
}
6264

6365
void setup_display() {
@@ -118,26 +120,25 @@ void setup_obd_connection() {
118120
}
119121

120122
void loop() {
121-
// In the main loop, we'll read the car's speed.
122-
// It's easier to do that here, because the UART connection uses interrupts
123-
124123
noInterrupts();
125124
state_t loc_state = state;
126125
interrupts();
127-
128-
if (loc_state == STATE_DISCONNECTED) {
129-
// If we got disconnected (or on first loop), try reconnecting
130-
setup_obd_connection();
131-
}
132126

133-
if (loc_state == STATE_SLEEPING) {
134-
// If we switched the switch to OFF, time for sleepies
135-
enter_sleep_mode();
136-
return;
127+
// Yay, finite state machine!
128+
switch (loc_state) {
129+
case STATE_DISCONNECTED:
130+
// If we got disconnected (or on first loop), try reconnecting
131+
setup_obd_connection();
132+
break;
133+
case STATE_SLEEPING:
134+
// If we switched the switch to OFF, time for sleepies
135+
enter_sleep_mode();
136+
break;
137+
case STATE_CONNECTED:
138+
// We should be connected. PROBE!
139+
probe_current_speed();
140+
break;
137141
}
138-
139-
// We should be connected. PROBE!
140-
probe_current_speed();
141142
}
142143

143144
// INTERRUPTS
@@ -147,7 +148,7 @@ void isr_display() {
147148
static speed_t curr_disp_speed = 0;
148149

149150
if (state != STATE_CONNECTED) {
150-
return;
151+
return;
151152
}
152153

153154
// Make copy of target speed
@@ -184,27 +185,31 @@ void isr_refresh_display() {
184185
sevseg.refreshDisplay();
185186
}
186187

187-
void isr_read_display_unit() {
188+
void isr_check_display_unit() {
189+
// Read unit mode from switch
190+
unit_select = digitalRead(PIN_UNIT_SELECT);
191+
}
192+
193+
void isr_check_sleep_mode() {
194+
// Check if we've enabled the sleep switch
195+
int sleep_enable = digitalRead(PIN_SLEEP_ENABLE);
196+
188197
// If we were sleeping, wake up
189-
if (state == STATE_SLEEPING) {
198+
if (state == STATE_SLEEPING && !sleep_enable) {
190199
leave_sleep_mode();
191200
state = STATE_DISCONNECTED;
192201
}
193202

194-
// Read display mode from switch
195-
should_display_imperial = digitalRead(PIN_USE_IMPERIAL);
196-
should_display_metric = digitalRead(PIN_USE_METRIC);
197-
198-
if (!should_display_imperial && !should_display_metric) {
203+
// Go to sleep
204+
if (sleep_enable) {
199205
state = STATE_SLEEPING;
200-
sevseg.blank();
201206
}
202207
}
203208

204209
speed_t adjust_speed(speed_t speed) {
205210
// Adjust read speed using modifier (set via potentiometer) and take
206211
// unit into account
207-
if (should_display_imperial) {
212+
if (unit_select == UNIT_MPH) {
208213
// Convert from km/h to mph
209214
return round(modifier * (float)speed * 0.621371);
210215
}
@@ -262,6 +267,8 @@ int analog_read_avg(const int sensor_pin, const int nb_samples, const long time_
262267
}
263268

264269
void enter_sleep_mode() {
270+
sevseg.blank();
271+
265272
#ifndef MODE_SIMULATION
266273
obd.enterLowPowerMode();
267274
#endif

0 commit comments

Comments
 (0)