This repository has been archived by the owner on Jun 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
camion.ino
181 lines (145 loc) · 4.52 KB
/
camion.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <Wire.h> //Needed for I2C to Qwiic MP3 Trigger
#include "SparkFun_Qwiic_MP3_Trigger_Arduino_Library.h" //http://librarymanager/All#SparkFun_MP3_Trigger
class Flasher
{
int ledPin; // the number of the LED pin
long OnTime; // milliseconds of on-time
long OffTime; // milliseconds of off-time
long duration; // Total duration of all the blinks in ms
long endBlink; // Time when the blinks end in ms
int ledState; // ledState used to set the LED
unsigned long previousMillis; // will store last time LED was updated
public:
Flasher(int pin, long on, long off, long dur)
{
ledPin = pin;
pinMode(ledPin, OUTPUT);
OnTime = on;
OffTime = off;
duration = dur;
endBlink = 0;
ledState = LOW;
previousMillis = 0;
}
void Update()
{
unsigned long currentMillis = millis();
if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
{
ledState = LOW; // Turn it off
previousMillis = currentMillis; // Remember the time
digitalWrite(ledPin, ledState); // Update the actual LED
}
else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime) && (currentMillis < endBlink))
{
ledState = HIGH; // turn it on
previousMillis = currentMillis; // Remember the time
digitalWrite(ledPin, ledState); // Update the actual LED
}
}
void setEndBlink()
{
endBlink = (millis() + duration);
}
};
// Constants and variables used in playSongBtn, setup and loop
// Qwiic MP3 trigger object
MP3TRIGGER mp3;
// Pin constants
const int OLAF_LED = 3;
const int ELSA_LED = 4;
const int OLAF_BTN = 8;
const int ELSA_BTN = 6;
const int KLAX_BTN = 7;
const int BEEP_BTN = 5;
const int ONE_BTN = 9;
const int TWO_BTN = 10;
const int THREE_BTN = 11;
// Buttons management
int olaf_btn_prev = HIGH;
int elsa_btn_prev = HIGH;
int klax_btn_prev = HIGH;
int beep_btn_prev = HIGH;
int one_btn_prev = HIGH;
int two_btn_prev = HIGH;
int three_btn_prev = HIGH;
int olaf_btn_state = HIGH;
int elsa_btn_state = HIGH;
int klax_btn_state = HIGH;
int beep_btn_state = HIGH;
int one_btn_state = HIGH;
int two_btn_state = HIGH;
int three_btn_state = HIGH;
// Debouncer
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 200; // the debounce time; increase if the output flickers
// Flashers
Flasher olaf_flash(OLAF_LED, 1000, 1000, 30000);
Flasher elsa_flash(ELSA_LED, 1000, 1000, 27000);
void startSong(int btn, int song)
{
mp3.playFile(song);
SerialUSB.print("Pressed button number ");
SerialUSB.println(btn);
if (btn == OLAF_BTN)
{
olaf_flash.setEndBlink();
}
else if (btn == ELSA_BTN)
{
elsa_flash.setEndBlink();
}
}
void playSongBtn(int btn, int *btn_state, int *btn_prev, int song)
{
int reading;
reading = digitalRead(btn);
// Debouncing: If the button changed, due to noise or pressing, reset the timer
if (reading != *btn_prev) { lastDebounceTime = millis(); }
// Debouncing: If the button is stable, set it as the current state
if ((millis() - lastDebounceTime) > debounceDelay) {
// If the button is now reading LOW from HIGH, play the song
if ((*btn_state == HIGH) && (reading == LOW)) { startSong(btn, song); }
// Set the button to its stable state
*btn_state = reading;
}
*btn_prev = reading;
}
void setup()
{
// Initialize the Qwiic MP3 Trigger
Wire.begin();
//Check to see if Qwiic MP3 is present on the bus
if (mp3.begin() == false)
{
SerialUSB.println("Failed to initialize MP3 trigger");
while (1);
}
mp3.setVolume(9); //Volume can be 0 (off) to 31 (max)
//pinMode setup
pinMode(OLAF_BTN, INPUT_PULLUP);
pinMode(ELSA_BTN, INPUT_PULLUP);
pinMode(KLAX_BTN, INPUT_PULLUP);
pinMode(BEEP_BTN, INPUT_PULLUP);
pinMode(ONE_BTN, INPUT_PULLUP);
pinMode(TWO_BTN, INPUT_PULLUP);
pinMode(THREE_BTN, INPUT_PULLUP);
// Serial initialize
SerialUSB.begin(9600);
//while (!SerialUSB);
//SerialUSB.println("SerialUSB initialized");
}
void loop()
{
// Update flashing leds
olaf_flash.Update();
elsa_flash.Update();
// Actions for all the buttons
playSongBtn(OLAF_BTN, &olaf_btn_state, &olaf_btn_prev, 5);
playSongBtn(ELSA_BTN, &elsa_btn_state, &elsa_btn_prev, 6);
playSongBtn(KLAX_BTN, &klax_btn_state, &klax_btn_prev, 7);
playSongBtn(BEEP_BTN, &beep_btn_state, &beep_btn_prev, 8);
playSongBtn(ONE_BTN, &one_btn_state, &one_btn_prev, 9);
playSongBtn(TWO_BTN, &two_btn_state, &two_btn_prev, 10);
playSongBtn(THREE_BTN, &three_btn_state, &three_btn_prev, 11);
}