Hello - I am working on a project where once a button is pressed, a sound file will play and LED lights will come on. Ideally, I'd like to have the led lights turn off once the sound file is complete/played once. The next time the button is pressed, a different sound file (in sequence) would play and the same LED lights would turn on again for the duration of the sound file. This would repeat until the 11th press when the system would loop back to the beginning and restart.
I'm relatively new at all of this but have scoured the forums to put together a schematic and program that I thought would work. However, when I finally had it set up, nothing happened. No LED lights or sounds.
Below is the code I attempted to use. Any information, tips, or suggestions you may have would be much appreciated. I am sure that I made this a lot harder than it needed to be, but have been unable to figure out how to simplify.
#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3.h>
#include <Wire.h>
#include <I2Cdev.h>
#include <Button.h>
int inPin = 2; // the number of the input pin
int outPin = 4; // the number of the output pin
int ledPin = 5;
byte buttonPresses = 0; // how many times the button has been pressed
byte lastPressCount = 0; // to keep track of last press count
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
int brightness = 0; // how bright the LED is
int track = 20;
int prev = 20;
//Button button = Button(12,PULLDOWN);
boolean state = false; // the current state of the circuit
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 500; // the debounce time, increase if the output flickers
long looptime = 0; // the last time the loop was played
long gtime = 0;
SoftwareSerial mySerial(0, 1); // RX, TX
void setup()
{
Wire.begin(); // join I2C bus
//Serial.begin(38400); // initialize serial communication
//while (!Serial); // wait for Leonardo enumeration, others continue immediately
Serial.println("INIT");
mySerial.begin(9600);
mp3_set_serial(mySerial); //set Serial for DFPlayer-mini mp3 module
pinMode(inPin, INPUT);
pinMode(outPin, OUTPUT);
pinMode(ledPin, OUTPUT);
}
void loop()
{
reading = digitalRead(inPin);
buttonPresses++; // increment buttonPresses count
delay(250); // debounce switch
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
{
(buttonPresses == 1);
digitalWrite(ledPin, HIGH);
looptime = millis();
mp3_play(001);
for(brightness = 0; brightness < 200; brightness++) {
analogWrite(outPin, brightness);
delay(16);
}
}
{
(buttonPresses == 2);
digitalWrite(ledPin, HIGH);
looptime = millis();
mp3_play(002);
for(brightness = 0; brightness < 200; brightness++) {
analogWrite(outPin, brightness);
delay(16);
}
}
{
(buttonPresses == 3);
digitalWrite(ledPin, HIGH);
looptime = millis();
mp3_play(003);
for(brightness = 0; brightness < 200; brightness++) {
analogWrite(outPin, brightness);
delay(16);
}
}
{
(buttonPresses == 4);
digitalWrite(ledPin, HIGH);
looptime = millis();
mp3_play(004);
for(brightness = 0; brightness < 200; brightness++) {
analogWrite(outPin, brightness);
delay(16);
}
}
{
(buttonPresses == 5);
digitalWrite(ledPin, HIGH);
looptime = millis();
mp3_play(005);
for(brightness = 0; brightness < 200; brightness++) {
analogWrite(outPin, brightness);
delay(16);
}
}
{
(buttonPresses == 6);
digitalWrite(ledPin, HIGH);
looptime = millis();
mp3_play(006);
for(brightness = 0; brightness < 200; brightness++) {
analogWrite(outPin, brightness);
delay(16);
}
}
{
(buttonPresses == 7);
digitalWrite(ledPin, HIGH);
looptime = millis();
mp3_play(007);
for(brightness = 0; brightness < 200; brightness++) {
analogWrite(outPin, brightness);
delay(16);
}
}
{
(buttonPresses == 8);
digitalWrite(ledPin, HIGH);
looptime = millis();
mp3_play(011);
for(brightness = 0; brightness < 200; brightness++) {
analogWrite(outPin, brightness);
delay(16);
}
}
{
(buttonPresses == 9);
digitalWrite(ledPin, HIGH);
looptime = millis();
mp3_play(012);
for(brightness = 0; brightness < 200; brightness++) {
analogWrite(outPin, brightness);
delay(16);
}
}
{
(buttonPresses == 10);
digitalWrite(ledPin, HIGH);
looptime = millis();
mp3_play(010);
for(brightness = 0; brightness < 200; brightness++) {
analogWrite(outPin, brightness);
delay(16);
}
}
{
if (buttonPresses == 11) buttonPresses = 0; // rollover every 11th press
if (lastPressCount != buttonPresses); // only do output if the count has changed
}
Serial.print ("Button press count = "); // out to serial
Serial.println(buttonPresses, DEC);
previous = reading;
}
}