-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlights.cpp
135 lines (123 loc) · 3.07 KB
/
lights.cpp
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
131
132
133
134
135
/*
Functions to manipulate lights on the Arduino. Some sound-related code is also included.
*/
#include "Arduino.h"
#include "lights.h"
#include "SNESpad.h"
#include "MelodyPlayer.h"
#include "pins.h"
#include "gameplay.h"
#include "buttonInput.h"
// Outputs a random integer between 0 and 3 (corresponding to a color)
int randomColor()
{
return (int)random(0, 4);
}
// Displays a color and plays the corresponding sound
void playColorAndSound(int color, int duration, boolean playAudio)
{
int toneToPlay;
// Turn on LED corresponding to the color
switch (color)
{
case 0:
digitalWrite(RED_LED_PIN, HIGH);
toneToPlay = redTone;
break;
case 1:
digitalWrite(GREEN_LED_PIN, HIGH);
toneToPlay = greenTone;
break;
case 2:
digitalWrite(BLUE_LED_PIN, HIGH);
toneToPlay = blueTone;
break;
case 3:
digitalWrite(YELLOW_LED_PIN, HIGH);
toneToPlay = yellowTone;
break;
default:
break;
}
// Play the corresponding tone for the time in "duration",
// unless you're not playing audio, in which case just wait
if (playAudio)
{
melodyPlayer.playTone(toneToPlay, duration);
}
else
{
delay(duration);
}
}
// Lights the LED of the corresponding color
void lightUpColor(int color)
{
switch (color)
{
case 0:
digitalWrite(RED_LED_PIN, HIGH);
break;
case 1:
digitalWrite(GREEN_LED_PIN, HIGH);
break;
case 2:
digitalWrite(BLUE_LED_PIN, HIGH);
break;
case 3:
digitalWrite(YELLOW_LED_PIN, HIGH);
break;
default:
return;
}
}
// Turn off all LEDs
void clearLights()
{
digitalWrite(RED_LED_PIN, LOW);
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(BLUE_LED_PIN, LOW);
digitalWrite(YELLOW_LED_PIN, LOW);
}
// Plays the appropriate sound given a certain color
void playSoundForColor(int color)
{
// Get frequency in hz
int frequency;
switch (color)
{
case 0:
frequency = redTone;
break;
case 1:
frequency = greenTone;
break;
case 2:
frequency = blueTone;
break;
case 3:
frequency = yellowTone;
break;
default:
return;
}
// Now, play audio (will be turned off in another function,
// when the user releases the button)
tone(SPEAKER_PIN, frequency);
}
// Play the color and sound for a pressed button until the button is released
void playPressedButtonColorAndSound()
{
// Grab first color pressed by the user
int pressedColor = getPressedColor();
// Light corresponding light
lightUpColor(pressedColor);
// Loop until the user releases that button
while (isPressed(pressedColor))
{
playSoundForColor(pressedColor);
updateButtonStates();
}
noTone(SPEAKER_PIN);
clearLights();
}