-
Notifications
You must be signed in to change notification settings - Fork 1
/
Traffic_Light_Sketch.ino
78 lines (63 loc) · 2.06 KB
/
Traffic_Light_Sketch.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
// Traffic Lights Circuit
// Includes Sparkfun .wav trigger, Arduino Mega
// Written by Steph Piper under an Open Source licence
const int buttonPin = 9;
const byte LED = 4;
int buttonState;
unsigned long buttonPushedMillis; // when button was released
unsigned long ledTurnedOnAt; // when led was turned on
unsigned long turnOnDelay = 5500; // wait to turn on LED
unsigned long turnOffDelay = 13000; // turn off LED after this time
bool ledReady = false; // flag for when button is let go
bool ledState = false; // for LED is on or not.
void setup() {
// put your setup code here, to run once:
//Connection to WAV Trigger
pinMode(2, OUTPUT);
// Connection to Traffic Light
pinMode(4, OUTPUT);
// Connection to button
Serial.begin(9600);
pinMode(buttonPin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
Serial.println("Button On");
digitalWrite(2, HIGH);
} else {
Serial.println("Button Off");
digitalWrite(2, LOW);
}
// get the time at the start of this loop()
unsigned long currentMillis = millis();
// check the button
if (buttonState == HIGH) {
// update the time when button was pushed
buttonPushedMillis = currentMillis;
ledReady = true;
}
// make sure this code isn't checked until after button has been let go
if (ledReady) {
//this is typical millis code here:
if((unsigned long)(currentMillis - buttonPushedMillis) >= turnOnDelay) {
// okay, enough time has passed since the button was let go.
digitalWrite(LED, HIGH);
// setup our next "state"
ledState = true;
// save when the LED turned on
ledTurnedOnAt = currentMillis;
// wait for next button press
ledReady = false;
}
}
// see if we are watching for the time to turn off LED
if (ledState) {
// okay, led on, check for now long
if ((unsigned long)(currentMillis - ledTurnedOnAt) >= turnOffDelay) {
ledState = false;
digitalWrite(LED, LOW);
}
}
}