Go Down

Topic: One button 2 functions with delay (Read 206 times) previous topic - next topic

Abcparty

Hi
Please help me.
My project is for Arduino uno, is only one button for 2 led's command.
When i press button first time,led 1 must be on 1 sec and off.
For secund time pressed button, led 2 must be 1 secund and off as well.
Only 2 states.
I am in dificult with delay function.

My code is:

int buttonPin = 13;

int led1 = 2;
int led2 = 3;


int buttonPushCounter = 0;   
int buttonState = 0;         
int lastButtonState = 0;   

void setup()
{
 
  pinMode(buttonPin, INPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
 
}

void loop()


{
// read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

 
  if (buttonState != lastButtonState) {
   
    if (buttonState == HIGH) {
     
      buttonPushCounter++;
      if(buttonPushCounter == 3){ buttonPushCounter = 1;}
     
    }
    else {
     
     
    }
  }
 
  lastButtonState = buttonState;
  switch(buttonPushCounter)
  {
    case 1:
      digitalWrite(led1, HIGH);
      delay(500);
      digitalWrite(led1, LOW);
      digitalWrite(led2, LOW);
     

 
    break;

 
     
   
     
    case 2:
      digitalWrite(led1, LOW);
      digitalWrite(led2, HIGH);
      delay(500);
      digitalWrite(led2, LOW);

      break;


  }
             
   
  }
 

PaulS

Quote
I am in dificult with delay function.
Your code does something that you have not explained.
You want the code to do something. If what it does is what you want, it seems unlikely that you would have posted here. So, I think it is safe to assume that what the code does is not what you want. But, the differences between what it does and what you want are not defined.
The art of getting good answers lies in asking good questions.

wvmarle

Change delay(500) to delay(1000), then you'll have your full second instead of half a second.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Go Up