-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathampel.ino
133 lines (81 loc) · 1.85 KB
/
ampel.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
122
123
124
125
126
127
128
129
130
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <stdint.h>
volatile bool nextGreen = false;
volatile bool green = false;
volatile bool nextRed = false;
volatile bool red = false;
volatile bool counterGo = false;
volatile uint8_t counter = 0;
int main(void) {
cli();
// initialize digital pin LED_BUILTIN as an output.
pinMode(PB2, OUTPUT);
pinMode(PB3, OUTPUT);
pinMode(PB4, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(PB0, INPUT);
ledRedOff();
ledYellowOff();
ledGreenOff();
ledRedOn();
red = true;
wdt_reset(); // Reset watchdog
WDTCR = _BV(WDTIE) | _BV(WDP2) | _BV(WDP0); // Turn on WDT interrupt and cause an interrupt every 500 ms
sei();
while (1) {
if (nextGreen) {
if(!counterGo)counter = 0;
counterGo = true;
ledYellowOn();
if (counter >= 2) {
counterGo = false;
ledYellowOff();
ledRedOff();
ledGreenOn();
green = true;
red = false;
nextGreen = false;
}
}
if (nextRed) {
if(!counterGo)counter = 0;
counterGo = true;
ledYellowOn();
ledGreenOff();
if (counter >= 4) {
counterGo = false;
ledYellowOff();
ledRedOn();
red = true;
green = false;
nextRed = false;
}
}
}
}
void ledRedOn() {
digitalWrite(PB3, HIGH);
}
void ledYellowOn() {
digitalWrite(PB4, HIGH);
}
void ledGreenOn() {
digitalWrite(PB2, HIGH);
}
void ledRedOff() {
digitalWrite(PB3, LOW);
}
void ledYellowOff() {
digitalWrite(PB4, LOW);
}
void ledGreenOff() {
digitalWrite(PB2, LOW);
}
ISR(WDT_vect)
{
if (digitalRead(PB0) && red) nextGreen = true;
if (!digitalRead(PB0) && green) nextRed = true;
if (counterGo)counter++;
}