-
Notifications
You must be signed in to change notification settings - Fork 0
/
BikeTrailer.ino
121 lines (91 loc) · 2.89 KB
/
BikeTrailer.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
110
111
112
113
114
115
116
117
118
119
120
121
/*
BikeTrailer
Arduino microcontroller code to control rear flashing lights and interior
'cabin' reading light for Burley bike trailer.
Rear light pattern is similar to Planet Bike's 'SuperFlash' pattern, which is
very eye catching.
*/
// General setup
byte rearFlashPin = 11;
byte cabinLightPin = 13;
byte cabinLightInterruptPin = 2;
volatile byte cabinLightState = LOW;
unsigned long cabinLightPinLastChangeTime = 0;
unsigned long switchHold = 30;
volatile bool switchLock = true;
// PWM duty cycle for low and high brightness (there is also off, which is 0)
int lowBrightPwm = 30;
int highBrightPwm = 255;
// 2D array, where second dimension represents time (in milliseconds) and duty cycle (0-255)
int rearFlashPattern [][2] = {
{ 30, lowBrightPwm },
{ 120, 0 },
{ 30, lowBrightPwm },
{ 120, 0 },
{ 30, lowBrightPwm },
{ 120, 0 },
{ 30, lowBrightPwm },
{ 140, 0 },
{ 70, highBrightPwm },
{ 110, 0 },
{ 160, highBrightPwm },
{ 130, 0 },
{ 30, lowBrightPwm },
{ 120, 0 },
{ 30, lowBrightPwm },
{ 120, 0 },
{ 30, lowBrightPwm },
{ 120, 0 },
{ 30, lowBrightPwm },
{ 120, 0 },
{ 30, lowBrightPwm },
{ 140, 0 },
{ 70, highBrightPwm },
{ 110, 0 },
{ 160, highBrightPwm },
{ 130, 0 }
};
int rearFlashPatternLength = sizeof(rearFlashPattern)/sizeof(rearFlashPattern[0]);
void setup() {
pinMode(rearFlashPin, OUTPUT);
pinMode(cabinLightPin, OUTPUT);
digitalWrite(cabinLightPin, LOW);
pinMode(cabinLightInterruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(cabinLightInterruptPin), toggleCabinLight, CHANGE);
Serial.begin(9600);
}
void loop() {
int i;
for (i = 0; i < rearFlashPatternLength; i++) {
// This is part of the interrupt based debounceing
if (!switchLock &&
(digitalRead(cabinLightInterruptPin) == HIGH) &&
((millis() - cabinLightPinLastChangeTime) > switchHold))
{
switchLock = true;
// DEBUG
Serial.print("Lock ON");
Serial.println();
}
analogWrite(rearFlashPin, rearFlashPattern[i][1]);
delay(rearFlashPattern[i][0]);
}
}
void toggleCabinLight() {
// ISR for cabin light button. This uses a debounce scheme I cooked up that
// requires no waiting in the ISR (but the main loop has a role to play).
// Light state is toggled when button is released.
if (!switchLock && ((millis() - cabinLightPinLastChangeTime) > switchHold)) {
// DEBUG
Serial.print("Toggling Light");
Serial.println();
cabinLightState = !cabinLightState;
digitalWrite(cabinLightPin, cabinLightState);
}
// DEBUG
Serial.print((millis() - cabinLightPinLastChangeTime));
Serial.print("Lock OFF");
Serial.println();
cabinLightPinLastChangeTime = millis();
switchLock = false;
}