adding a button to the blink tutorial

hey all, im new to the forum, arduino and microprocessors.

my aim is to learn to control servo motors and stepper motors, alot of these would be told what to do via buttons. but i wanted to start with basics :D

i have started out with some tutorials in the learning section of the arduino website. i have succesfully done the blink tutorial and the button tutorial. for a better understanding i have now combined them so that i have to hold the button down before the led will flash. now i'd like to make it so that the button worked like a hold in, so i only have to press it and the led will blink and press it again to stop it.

but honestly i dont know where to start with that.

i dont want/expect people to give me the code already done, if someone can point me in the right direction so i can learn this that would be fantastic. :grin:

here is how i combined the code

// constants won't change. They're used here to 
// set pin numbers:
 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

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:
   if (buttonState == HIGH) {        
  digitalWrite(ledPin, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(ledPin, LOW);    // set the LED off
  delay(1000);              // wait for a second
   } 
  else {
     // turn LED off:
     digitalWrite(ledPin, LOW); 
  }
 }

cheers, Ash.

Rather than requiring that the switch state be HIGH, you would compare the current switch state to the previous switch state, and do something if they are different.

This requires that you save, at the end of loop, the current state as the previous state.

Then, you do something like this:

int currState;
int prevState = LOW;
bool blinking = false;

void loop()
{
   currState = digitalRead(switchPin);
   if(currState != prevState)
   {
     // A transition occurred - the switch is now pressed but was released
     // or the switch was pressed and is now released
     if(currState == HIGH)
     {
       // The switch was just pressed
       blinking = !blinking; // Change the flag
     }
   }
   prevState = currState;

   if(blinking)
   {
      // Make the LED do its blinking thing
   }
}

Your current code only reads the state of the switch every two seconds. This change is not going to make the switch get read any sooner.

You really need to look at the blink without delay example, and forget that you ever heard of the delay() function.

thanks for the quick response i'll give this a try now :D :D :D

im bad at this, to simplify it further for me i removed trying to blink the led for now, using your code i tried to just turn the led on pin 13 on and off, after uploading the code the led is off, i press the button and the led turns on, when i press the button again it doesnt turn off.

here is what i have done with your code

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

int currState;
int prevState = LOW;
bool blinking = false;
 
void setup() {
   
   pinMode(ledPin, OUTPUT);       // initialize the LED pin as an output
   pinMode(buttonPin, INPUT);     // initialize the pushbutton pin as an input
}
 
void loop(){
   currState = digitalRead(buttonPin); //check the current state of the button pin
   if (currState != prevState){        //the button has been pressed
   if(currState == HIGH){               //not sure what this is doing
   blinking = !blinking;                  //not sure what this is doing
   }
  }
   prevState = currState;               //not sure what this is doing
  
   if (blinking){
   digitalWrite(ledPin, HIGH);
  }
 }

cheers, Ash.

Perhaps you noticed that I put each { on it's own line. That makes it easier, to me, to see where a block of code starts and ends.

If you look at your code, you set blinking to true the first time the switch is pressed. The next time, you set it to false. Then true, then false.

However, you turn the LED on when blinking is true, but you do not turn it off when blinking is not true.

i added

if (!blinking){
    digitalWrite(ledPin, LOW);}

now it works :D so i went back to the blinking if the button is pressed and i ended up with this code

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


int currState;
int prevState = LOW;
bool blinking = false;

void setup() {
   
   pinMode(ledPin, OUTPUT);       // initialize the LED pin as an output
   pinMode(buttonPin, INPUT);     // initialize the pushbutton pin as an input
}

void loop(){
   currState = digitalRead(buttonPin); //check the current state of the button pin
   if (currState != prevState){        //the button has been pressed
   if(currState == HIGH){
   blinking = !blinking;
   }
  }
   prevState = currState;
  
   if (blinking){
   digitalWrite (ledPin, HIGH);
   delay(500);
   digitalWrite (ledPin, LOW);
   delay(500);
  }
  if (!blinking){
   digitalWrite(ledPin, LOW);
  }
 }

this does the job how ever, i must hold the button for maybe 1 second before it will turn off, i expect this is due to the delay's im using on the blink. time to learn blink with out delays.

thank you very much for the assistance tonight. most appreciated.

ash.

i expect this is due to the delay's im using on the blink.

It is indeed.

time to learn blink with out delays.

This too.