-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJuegoBasket.ino
83 lines (62 loc) · 1.35 KB
/
JuegoBasket.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
const int ledCount = 5;
int ledPins[] = {3, 4, 5, 6, 7};
const int photoSensor = 0;
int minLight;
int maxLight;
int currentLightLevel;
int lightLevel;
int score = 0;
int levelActive = 100;
const int buzzerPin = 8;
void setup() {
for (int i=0;i<ledCount;i++)
pinMode(ledPins[i], OUTPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
lightLevel = getLightLevel();
int lightLevelActive = 0;
if (lightLevel < 50) {
lightLevelActive = lightLevel;
score++;
setScore();
playSound();
while (lightLevelActive >= getLightLevel()) {
delay(500);
}
}
if (score == 5) {
win();
score = 0;
}
delay(500);
}
void setScore() {
for (int led=0;led<ledCount;led++) {
if (led < score)
digitalWrite(ledPins[led], HIGH);
else
digitalWrite(ledPins[led], LOW);
}
}
void playSound() {
digitalWrite(8, HIGH);
delay(200);
digitalWrite(8, LOW);
}
void win() {
for (int i=0;i<5;i++) {
for (int led=0;led<ledCount;led++)
digitalWrite(ledPins[led], HIGH);
playSound();
for (int led=0;led<ledCount;led++)
digitalWrite(ledPins[led], LOW);
delay(50);
}
}
int getLightLevel() {
currentLightLevel = analogRead(photoSensor);
minLight = min(minLight, currentLightLevel);
maxLight = max(maxLight, currentLightLevel);
return map(currentLightLevel, minLight, maxLight, 0, 100);
}