Button and For Loop Troubles

Hello all,

So I am trying to make it so when I press the button the light turns on for 10 seconds then shuts off. For some reason this code does not do that, instead it just turns on when I hold the button down but then turns off when I let go. Please let me know if you can help me with my code.

const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int i = 1;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
for (buttonState == HIGH; i < 5; i++) {
// turn LED on:
delay (100);
digitalWrite(ledPin, HIGH);
}
}

Thank you!

void loop() {
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {
    //turn on LED and wait for 10 seconds
    digitalWrite(ledPin, HIGH);
    delay(10 * 1000);

    //switch off the LED and reset buttonState
    digitalWrite(ledPin, LOW);
    buttonState = LOW;
  }
}
//note: delay is in milliseconds.  1 second = 1000 milliseconds.
  for (buttonState == HIGH; i < 5; i++) {

I don't think you understand the concept of a for loop, or how to implement one. I would recommend doing some reading on them, although maybe not now, since you're not looking for a for loop. There are two examples that demonstrate the concepts that you'll need:

StateChangeDetection - Shows you how to identify the moment when a switch is "pressed" and "released". You can use this method to record the time at which the switch was pressed, as well as turn on the LED.

BlinkWithoutDelay - Demonstrates the timing concepts of the Arduino. Ultimately it will revolve around the proper use of millis() to record the time (as noted above), and some simple subtraction to determine the elapsed time since the aforementioned variable was set. Once the elapsed time has been 10 or more seconds, you can turn the LED off.

Thank you so much but I was trying to do it with a for loop because later im going to try to figure out how to add a tune that plays along the LED for 10 seconds.

I was trying to do it with a for loop because later im going to try to figure out how to add a tune that plays along the LED for 10 seconds.

I'm afraid that this makes no sense. You turn the pin on once and off once. No for loop needed.

Perhaps you need to use a for loop between turning the pin on and turning it off, in which case the advice to learn about for loops applies. That is not how to initialized the variable used in the loop.

void loop() {
  buttonState = digitalRead(buttonPin);

  //note use of comparison operator '=='
  if(buttonState == HIGH) {
    digitalWrite(ledPin, HIGH);

    //note use of assignment operator '=' to initialise the counter variable to zero
    for(int i = 0;  i < 10; i++) {
      delay(1000);
    }

   //still need to reset state  
   digitalWrite(ledPin, LOW);
   buttonPin = LOW;
 }
}

Read through this code and see if you can work out what it will do...

int buttonCount = 0;
int buttonPin = 2;
int ledPin = 13;
int buttonState = LOW;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);  
{

void loop() {
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {
    buttonCount++;
    for (int i = 0; i < buttonCount; i++) {
      //flash on
      digitalWrite(ledPin, HIGH);
      delay(100);

      //flash off
      digitalWrite(ledPin, LOW)
      delay(100);
    }
    //reset the button
    buttonState = LOW; 
  }

  if (buttonCount  > 10) {
    buttonCount = 0;
  }
}

Read through this code and see if you can work out what it will do...

Fail miserably.

  buttonPin = digitalRead(buttonPin);

Reassigning the pin number based on the state of a pin makes little sense.

PaulS:
Fail miserably.

How about I claim the bugs were intended for learning purposes. You've ruined it now :smiley:

Of course I might just have been typing faster than I was thinking...That happens too.

Code corrected.

Using that code above I now hold it down and it blinks continuously. Not really sure what is wrong.

I doubt you've defined this completely as you have not defined how you want the button to act while the led is lit. So far you have a sketch that will do one of two things, wait for a button press and turn on a led or wait to turn off a led if the button has been pressed.

const byte ledPin = 13;
const byte buttonPin = 2;
const unsigned long debounceTime = 20;
const unsigned long ledOnDuration = 10000; // ten second led on time
boolean ledOn = false;
unsigned long turnOnTime = 0;

void setup()
{
  pinMode(ledPin, OUTPUT);      
  pinMode(buttonPin, INPUT_PULLUP); // switch with no resistor 
}

void loop()
{
  if (ledOn)
  {
    if (millis() - turnOnTime >= ledOnDuration)
    {
      digitalWrite (ledPin, LOW); // turn off led
      ledOn = false;
    }
  }
  else
  {
    if (digitalRead(buttonPin) == LOW)
    {
      delay(debounceTime); // rudimentary debounce
      if (digitalRead(buttonPin) == LOW)
      {
        digitalWrite (ledPin, HIGH);  // turn on led
        turnOnTime = millis();         // remember when it happened
        ledOn = true;    
      } 
    }
  }
}

I haven't compiled or tested this.

That happens too.

Boy, does it. A lot more often that I like to admit.

This code now just turns it on while i hold the button down.

This code now just turns it on while i hold the button down.

See reply #2 - the part about looking at the state change detection example.

You want to do something when the switch BECOMES pressed, not when the switch IS pressed. The state change detection example shows how to determine when the switch BECOMES pressed. Your current code only cares that the switch IS pressed. There is a world of difference between IS and BECOMES.