Led on before I activate the code, need it off

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); 
  }
}

Please post schematics. Pen and paper should work fine.

How is the LED on pin 5 wired ?

ezButton button(BUTTON_PIN);  // create ezButton object that attach to pin 7;

later in the code

  if (digitalRead(BUTTON_PIN) == LOW)

Why #include a library and create a button object then not use it here ?

Change this to

if (false) {

and see if the LED is on on startup, if it is not, then your problem is that your button is detected

The led on pin 5 goes to a resistor then led then to 5v on uno

Which explains why it's on: in setup() you set the pin LED as low, but the other end of the led is at 5V. So the cathode's at 0V, anode's at 5V, led is on.

Wired that way, a high at the pin will make both ends 5V and it will be off.

Alternatively, turn the led round and put the anode on the pin and cathode to ground, then a low will be off and high will be on.

That is probably a side effect from me merging codes. I am fairly new at coding and I tried to educate myself on how code works from tutorials. There doesn't seem to be any tutorials on how to light and led, play a .wav from SD card reader and light a led 11sec into playing. So I cut and pasted pieces of 3 different codes that I found individually to get the result I needed. In the tutorial for the led on pin 5 is supposed to be off until the button is pushed. It works by itself but when I merged codes it is on before I push button. Here is the code just for the delayed led, note it goes on at 11 sec stays on the goes off after 20 sec. I need it to do this while playing a .wav file from SD card when I push button.

//Global Variables
const byte BUTTON=2; // our button pin
const byte LED=5;   // LED (built-in on Uno)
 
unsigned long buttonPushedMillis;  // when button was released
unsigned long ledTurnedOnAt;  // when led was turned on
unsigned long turnOnDelay = 11000; // wait to turn on LED
unsigned long turnOffDelay = 20000; // turn off LED after this time
bool ledReady = false; // flag for when button is let go
bool ledState = false; // for LED is on or not.
 
void setup() {
  pinMode(BUTTON, INPUT_PULLUP);
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);
}
 
void loop() {
  // get the time at the start of this loop()
  unsigned long currentMillis = millis(); 
 
  // check the button
  if (digitalRead(BUTTON) == 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);
    }
  }
}

I realized something after checking on that last code I loaded, I used wokwi.com and loaded the code put in parts(led and button and realized I had the led wired wrong because of user graboruk remarks. I fixed it and it works correctly now. I just need the led to never to turn off unless I reset uno and push button again.

I turned it around and nothing happened, but did realize it was wired wrong anyway. now pin 5 goes to resistor then the led then to ground. I changed the delay time in code and works.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.