Pushbutton as Tugle Switch Code


ok simple , stupid question :)
how can code push button switch / toggle switch...

like when press button led 13 stays on until press button next time?

im playing w/ following example far have no guesses :(

code: [select]
/*
 button

turns on , off light emitting diode(led) connected digital  
pin 13, when pressing pushbutton attached pin 7.


circuit:
* led attached pin 13 ground
* pushbutton attached pin 2 +5v
* 10k resistor attached pin 2 ground

* note: on arduinos there led on board
attached pin 13.


created 2005
dojodave <http://www.0j0.org>
modified 17 jun 2009
tom igoe

 http://www.arduino.cc/en/tutorial/button
*/

// constants won't change. they're used here
// set pin numbers:
const int buttonpin = 2;     // number of pushbutton pin
const int ledpin =  13;      // number of led pin

//alexus
const int buttonpin2 = 3;     // number of pushbutton pin
const int ledpin2 =  12;      // number of led pin

// variables change:
int buttonstate = 0;         // variable reading pushbutton status
int buttonstate2 = 0;

void setup() {
 // initialize led pin output:
 pinmode(ledpin, output);  
 pinmode(ledpin2, output);  
 // initialize pushbutton pin input:
 pinmode(buttonpin, input);  
 pinmode(buttonpin2, input);    
}

void loop(){
 // read state of pushbutton value:
 buttonstate = digitalread(buttonpin);
 buttonstate2 = digitalread(buttonpin2);

 // check if pushbutton pressed.
 // if is, buttonstate 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 "wanna done, doi yourself"
so did. code attached below ...

now im new arduino, me looks tooooo bulky have type of code ontroll on/off switch led...

so maybe 1 advice on optimization of code?

tnx!

code: [select]
/*
 button

turns on , off light emitting diode(led) connected digital  
pin 13, when pressing pushbutton attached pin 7.


circuit:
* led attached pin 13 ground
* pushbutton attached pin 2 +5v
* 10k resistor attached pin 2 ground

* note: on arduinos there led on board
attached pin 13.


created 2005
dojodave <http://www.0j0.org>
modified 17 jun 2009
tom igoe

 http://www.arduino.cc/en/tutorial/button
*/

// constants won't change. they're used here
// set pin numbers:
const int buttonpin = 2;     // number of pushbutton pin
const int ledpin =  13;      // number of led pin

//alexus
const int buttonpin2 = 3;     // number of pushbutton pin
const int ledpin2 =  12;      // number of led pin

// variables change:
int buttonstate = 0;         // variable reading pushbutton status
int buttonstate2 = 0;
int buttontugle2 = 0;        //tugle variable
int button2_relaselock =0;       // led not pulse while holding down

void setup() {
 // initialize led pin output:
 pinmode(ledpin, output);  
 pinmode(ledpin2, output);  
 // initialize pushbutton pin input:
 pinmode(buttonpin, input);  
 pinmode(buttonpin2, input);    
}

void loop(){
 // read state of pushbutton value:
 buttonstate = digitalread(buttonpin);
 buttonstate2 = digitalread(buttonpin2);

 // check if pushbutton pressed.
 // if is, buttonstate 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 no lock, button released or ot our first run
     if(buttontugle2==0){
       digitalwrite(ledpin2, high);
       buttontugle2 = 1;
     }else{
       digitalwrite(ledpin2, low);
       buttontugle2 = 0;  
     }
     //lock button not pulse
     button2_relaselock = 1;
     delay(100);
   }
   
 }else{
   //buttton 2 released
   //unlock button future use
   button2_relaselock =0;
   delay(100);
 }  
 
 
}


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Pushbutton as Tugle Switch Code


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums