NEWBIE here. 1 button to control 2 leds differently.


hi
i've used example , trying 1 button control 2 leds.
what want happen have ledpin turn on , off instantly button pressed. want a_ledpin light 2 seconds later, , turn off 2 seconds later (after button pressed).
forgive extreme nubeness

what i'd know how make delay function work next part , not affect entire loop , hard me explain right now

code: [select]
const int buttonpin = 2;     // number of pushbutton pin
const int ledpin =  13;      // number of led pin
const int a_ledpin = 12;

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

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

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

 // check if pushbutton pressed.
 // if is, buttonstate high:
 if (buttonstate == high) {    
   // turn led on:    
   digitalwrite(ledpin, high);
  delay(2000);
   digitalwrite(a_ledpin, high);
    }
 else {
   // turn led off:
   digitalwrite(ledpin, low);
   delay(2000);
   digitalwrite(a_ledpin, low);
 }
}

so i've edited code still cant figure out put delay because delay affects both leds

const int buttonpin = 2;     
const int ledpin =  13;     
const int a_ledpin = 12;


int buttonstate = 1;         
int ledpinstate = 1;

void setup() {

  pinmode(ledpin, output);     
  pinmode(a_ledpin, output); 

  pinmode(buttonpin, input);     
}

void loop(){

  buttonstate = digitalread(buttonpin);
  ledpinstate = digitalread(ledpin);


  if (buttonstate == high) {     
   
    digitalwrite(ledpin, high);
     }
         
  else {

    digitalwrite(ledpin, low);
   }
   
if (ledpinstate == high)  {
    digitalwrite(a_ledpin, high);
    }
    else {digitalwrite(a_ledpin, low);}


Arduino Forum > Using Arduino > Project Guidance > NEWBIE here. 1 button to control 2 leds differently.


arduino

Comments

Popular posts from this blog

Convierte tu Raspberry en un NAS. Firmware fvdw-sl 15.3 - Raspberry Pi Forums

How to format a Get Request

avrdude: verification error, first mismatch at byte 0x0000 0x0c != 0x62