Pushbutton as Tugle Switch Code

ok very simple and stupid question :slight_smile:
how can i code the push button to be a switch / toggle switch…

Like when I press button LED 13 stays on until i press button next time?

Im playing w/ following example but so far have no guesses :frowning:

/*
  Button
 
 Turns on and off a light emitting diode(LED) connected to digital  
 pin 13, when pressing a pushbutton attached to pin 7. 
 
 
 The circuit:
 * LED attached from pin 13 to ground 
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 
 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.
 
 
 created 2005
 by DojoDave <http://www.0j0.org>
 modified 17 Jun 2009
 by Tom Igoe
 
  http://www.arduino.cc/en/Tutorial/Button
 */

// 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

//alexus
const int buttonPin2 = 3;     // the number of the pushbutton pin
const int ledPin2 =  12;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int buttonState2 = 0; 

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

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

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
  
  if (buttonState2 == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin2, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin2, LOW); 
  }
}

well as they say ‚Äúwanna something done, doi it by yourself‚ÄĚ
So I did. Code attached below …

Now im new to arduino, but to me this looks tooooo bulky to have this type of code just to ontroll ON/OFF switch for LED…

So maybe some one would advice on optimization of the code?

TNX!

/*
  Button
 
 Turns on and off a light emitting diode(LED) connected to digital  
 pin 13, when pressing a pushbutton attached to pin 7. 
 
 
 The circuit:
 * LED attached from pin 13 to ground 
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 
 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.
 
 
 created 2005
 by DojoDave <http://www.0j0.org>
 modified 17 Jun 2009
 by Tom Igoe
 
  http://www.arduino.cc/en/Tutorial/Button
 */

// 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

//alexus
const int buttonPin2 = 3;     // the number of the pushbutton pin
const int ledPin2 =  12;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int buttonState2 = 0; 
int buttonTugle2 = 0;        //tugle variable
int button2_RelaseLock =0;       // so that LED will not pulse while we are holding it down

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

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

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
  
  
  if (buttonState2 == HIGH) {     
    // button 2 pressed    
    
    if(button2_RelaseLock==0){
      //there is no lock, button was released previously or ot is our first run
      if(buttonTugle2==0){
        digitalWrite(ledPin2, HIGH);
        buttonTugle2 = 1;
      }else{
        digitalWrite(ledPin2, LOW);
        buttonTugle2 = 0;  
      }
      //lock button so it will not pulse
      button2_RelaseLock = 1;
      delay(100); 
    }
     
  }else{
    //buttton 2 released
    //unlock button for future use
    button2_RelaseLock =0;
    delay(100);
  }  
  
  
}

Good that you made your own solution, that is the best way to learn.

const byte buttonpin = 3;
const byte ledpin = 12;

byte state = 0;

void setup()
{   
    pinMode(ledpin, OUTPUT);
    pinMode(buttonpin, INPUT);
}

void loop()
{
    if (digitalRead(buttonpin) == HIGH) {
        state = !state;  // toggle state
        digitalWrite(ledpin, state);
        delay(10);  // debounce
        while (digitalRead(buttonpin) == HIGH) {}
        delay(10);  // debounce
    }
}

Hey there,

There was something posted the other week which looked very neat.

const byte buttonpin = 3;
const byte ledpin = 12;

void setup()
{  
    pinMode(ledpin, OUTPUT);
    pinMode(buttonpin, INPUT);
}

void loop()
{
    static int8_t state = 0; //remember what state led's in...

    while (!digitalRead(buttonpin)) {    // do nothing}
    state = 1 - state;                          //toggle state
    digitalWrite(LED1, state==0);        //yeilds true/false
    delay(40);  // debounce                //wait for bouncing to stop
    while (digitalRead(buttonPin)) {}   //and button released...
}

tnx for rely! So I guess what I was missing was:

while (digitalRead(buttonpin)

this is used in both cases and ofcaurse makes life much easier :)

I will try both scripts just to see which one is better ;-)

Thank you!