-
Notifications
You must be signed in to change notification settings - Fork 0
/
Branlix2000.ino
101 lines (80 loc) · 1.71 KB
/
Branlix2000.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
#include <Servo.h>
// White LED
int ledW=5;
// Red LED (Left)
int ledR=2;
// Green LED (Right)
int ledG=7;
// Left button
int buttonL=10;
// Right button
int buttonR=4;
long unsigned int oldmillis1=0;
long unsigned int oldmillis2=0;
// Old button states for rising edge detection
bool buttonL_old=0;
bool buttonR_old=0;
// Speaker
int spk=3;
// Servo declaration
Servo servo;
// Init arrow in center position
int arrowPos = 87;
void setup() {
// Pin affectation
servo.attach(9);
// LED pins declarations
pinMode(ledG, OUTPUT);
pinMode(ledR, OUTPUT);
pinMode(ledW, OUTPUT);
digitalWrite(ledW, HIGH);
digitalWrite(ledG, HIGH);
digitalWrite(ledR, HIGH);
// Buttons pin declarations
pinMode(buttonL,INPUT);
pinMode(buttonR,INPUT);
}
void loop() {
//////////////////////////////////////////////
servo.write(arrowPos);
// Left button rising edge
if (!digitalRead(buttonL) && buttonL_old==1){
arrowPos++;
digitalWrite(ledR, HIGH);
oldmillis1=millis();
tone(spk, 900, 50);
}
// Right button rising edge
if(!digitalRead(buttonR) && buttonR_old==1){
arrowPos--;
digitalWrite(ledG, HIGH);
oldmillis2=millis();
tone(spk, 700, 50);
}
// Tempo de clignotement des leds
if(millis()-oldmillis1==50)
digitalWrite(ledR, LOW);
if(millis()-oldmillis2==50)
digitalWrite(ledG, LOW);
// Buttons state reading
buttonL_old=digitalRead(buttonL);
buttonR_old=digitalRead(buttonR);
//////////////////////////////////////////////
if (arrowPos>111 || arrowPos<65){
win();
}
}
void win(){
delay(2000);
int i=20;
while (digitalRead(buttonL)!=0 || digitalRead(buttonR)!=0)
{
for(; i<5000; i++)
tone(spk, i, 50);
i=20;
}
arrowPos=87;
delay(1000);
digitalWrite(ledG, LOW);
digitalWrite(ledR, LOW);
}