Help for a simple code-checking

Hi Guys,

Finally I have got those pieces to try it on, however my code is not working, could you guys kindly help me out here.

What I want:

In my circuit, I have a momentarily contact switch, and I want to use it to control the built-in LED at Pin 13 on the Arduino Board. Basically when I press down the button, I want the LED to be illuminated for 5 seconds and then automatically turned-off. I am successful to do this by using debounce + delay function. However, I want to make it harder by replacing the delay function by millis function. I have watched the Youtube 2 blinking LED tutorial, but apparently my code is no good…

// A single button-on & -off to control a LED with debounce 

// Assign Pins to constants
const int Led1Pin1=13; // a Led is attached to this Pin
const int Button1Pin1=2; // a Push button is attached to this Pin

// Assign variables to states of LED and Button
int Button1State = LOW; // low if button not pressed; High if pressed 
int Led1State = LOW; // Initially LED is off at the beginning

long LastDebounceTime = 0; // the last time the output Pin was toggled (for debounce of button)
long DebounceDelay = 1000; // the debounce time; increase if the output flickers:
                          
// LED is light for 4 seconds then turned-off      
long ReleaseTime = 4000;  // the total discharge time period
long LastLedTime = 0;    // the last time the LED output Pin was toggled (controlling LED)

unsigned long currentMillis = 0;

void setup(){
  // setup the modes of Pins
  currentMillis = millis();
  pinMode(Led1Pin1, OUTPUT); // supply voltage
  pinMode(Button1Pin1, INPUT); // read voltage
}

void loop(){
  currentMillis = millis();
  controlLed();
}

void controlLed()
{
  Button1State = digitalRead(Button1Pin1); // read state of button
  if( (millis() - LastDebounceTime) > DebounceDelay){  // check if the time gap is greater than the debounce buffer time 
  if(Button1State == HIGH) {                                             // initiate LED program when button is pressed
    if( currentMillis - LastLedTime < ReleaseTime){    //  keep LED light for the time interval
    LastDebounceTime = millis();                                 // update to the current time controlling button
    LastLedTime = millis();                                          //update to the current time controlling LED
    Led1State == HIGH;                                                // turn-on LED from -off
    }
    else 
    Led1State = LOW;                                       
    digitalWrite(Led1Pin1, Led1State);                    // turn-off Led after 4 seconds
  }
}
}

When I upload the code, with or without pressing button, the LED never lights…Could you help me find out the problem. Thank you so much !!

Kind Regards,
Jeff

http://www.arduino.cc/en/Tutorial/Switch modify this

else
Led1State = LOW;

The end of the if should be here so }

digitalWrite(Led1Pin1, Led1State); // turn-off Led after 4 seconds
} and delete this one
in your the write is only done when false (as it is in the else

everybody makes this kind of errors, now with internet it is easy asking

Jeff27:

Write a comment after each of your closing curly braces saying something like "//end of if(LED on) ". That’ll show you where your mistakes are. Also, the statements following your ‘else’ should be enclosed in a pair of curly braces.