-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGiantDerestrict.ino
76 lines (61 loc) · 2.13 KB
/
GiantDerestrict.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
// wheel 700c = 622 diameter - 622 * 3.14 /1000 = 1.953 it is the distance for each wheel turn
const int reedSwitch = 2; // Declaring pin for reed switch to be used. Pin 2 or 3 must be used for the reed switch on the Uno
const int electroMagnet = 3; // ElectroMagnet
const float wheelLong = 1.953; // Wheel Long when 700C
unsigned long printTime; // declaring the total time until the next trip on the reed switch variable
float speed; // declaring the Speed variable
unsigned long duration; // declaring the timer to calculate the total time variable
unsigned long lastCurrentTime;
boolean lightOn;
boolean highSpeed;
void setup() {
Serial.begin(9600); // Start serial monitor after a few seconds. Mainly for testing code to get it to work.
attachInterrupt(digitalPinToInterrupt(2), isr, RISING); //Begins the count of seconds when the reed switch in pin 2 is set off
pinMode(reedSwitch, INPUT_PULLUP); // Sets the reed switch to be an input
pinMode(electroMagnet, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
lastCurrentTime = 0;
duration = 0;
speed = 0;
printTime = 0;
lastCurrentTime = millis();
lightOn = false;
highSpeed = false;
}
void isr() {
// some LED activity for visual debug
if (lightOn) {
digitalWrite(LED_BUILTIN, HIGH);
lightOn = false;
} else {
digitalWrite(LED_BUILTIN, LOW);
lightOn = true;
}
if (millis() - lastCurrentTime < 300)
highSpeed = true;
else
highSpeed = false;
if (highSpeed == false)
{
// We report the speed to the bike when < 25 kmh
digitalWrite(electroMagnet, HIGH);
delay(10);
digitalWrite(electroMagnet, LOW);
lastCurrentTime = millis();
}
}
void loop() {
duration = millis() - lastCurrentTime;
if (duration > 4000) {
duration = 0;
speed = 0;
}
else
speed = (wheelLong * 3.6 * 1000) / duration;
if (millis() - printTime >= 1000) {
printTime = millis();
Serial.print("Speed "); // Prints "Speed" to serial monitor. Mainly for troubleshooting.
Serial.print(speed);
Serial.print('\n'); // Prints the speed to the serial monitor. Mainly for troubleshooting.
}
}