Hope I loaded the code correctly, I have been researching a way to do this and I blended multiple codes. It seems like a mess but I couldn't seem to figure how to do what I wanted unless I blended codes. My Question is why is pin 5 led on when I have not toggled the button. I need it off to start the delay then stay on after the .wav is played. I am using a generic SDcard reader for Arduino and an Uno and a speaker. The idea is to have led pin 3 on till i push button to play .wav ...that works and the .wav plays but I wanted the led on pin 5 to come on 11sec into the .wav and that works, the reasoning behind the 10 for turning on trigger is so it would turn off the led and start the delay then turn on 11 sec later and stay on.
I just would like it to be off before the trigger which is pushing the button.
The schematics are fairly simple...SD Card reader ...CS to pin 10, SCK to pin 13, MOSI to pin 11, MISO to pin 12, VCC to 5v GND to GND... Speaker pos to pin 9 and neg to gnd.
Button is pin 7. I had to use the easyButton because the button is very sensitive and would keep restarting the .wav
#include "SD.h"
#define SD_ChipSelectPin 10
#include "TMRpcm.h"
#include "SPI.h"
#include <ezButton.h>
TMRpcm tmrpcm;
/// constants won't change
const int BUTTON_PIN = 7; // the number of the pushbutton pin
const int LED_PIN = 3; // the number of the LED pin
const int LED=5;
ezButton button(BUTTON_PIN); // create ezButton object that attach to pin 7;
// variables will change:
int ledState = LOW; // the current state of LED
unsigned long buttonPushedMillis; // when button was released
unsigned long ledTurnedOnAt; // when led was turned on
unsigned long turnOnDelay = 10; // wait to turn on LED
unsigned long turnOffDelay = 11000; // turn off LED after this time
bool ledReady = false; // flag for when button is let go
void setup() {
tmrpcm.speakerPin=9;
Serial.begin(9600); // initialize serial
pinMode(LED_PIN, OUTPUT); // set arduino pin to output mode
if(!SD.begin(SD_ChipSelectPin))
button.setDebounceTime(1500); // set debounce time to 50 milliseconds
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
}
void loop() {
button.loop(); // MUST call the loop() function first
unsigned long currentMillis = millis();
// check the button
if (digitalRead(BUTTON_PIN) == LOW) {
// update the time when button was pushed
buttonPushedMillis = currentMillis;
ledReady = true;
}
// make sure this code isn't checked until after button has been let go
if (ledReady) {
//this is typical millis code here:
if ((unsigned long)(currentMillis - buttonPushedMillis) >= turnOnDelay) {
// okay, enough time has passed since the button was let go.
digitalWrite(LED, HIGH);
// setup our next "state"
ledState = true;
// save when the LED turned on
ledTurnedOnAt = currentMillis;
// wait for next button press
ledReady = false;
}
}
// see if we are watching for the time to turn off LED
if (ledState) {
// okay, led on, check for now long
if ((unsigned long)(currentMillis - ledTurnedOnAt) >= turnOffDelay) {
ledState = false;
digitalWrite(LED, LOW);
}
}
if(button.isPressed()) {
Serial.println("The button is pressed");
// toggle state of LED
ledState = !ledState;
tmrpcm.setVolume(6);
tmrpcm.play("WeChoose.wav");
// control LED arccoding to the toggleed sate
digitalWrite(LED_PIN, ledState);
}
}