Go Down

Topic: [Solved] LED's blinks anyways (Read 593 times) previous topic - next topic

Mats2006

Jan 30, 2013, 11:32 pm Last Edit: Jan 31, 2013, 09:06 am by Mats2006 Reason: 1
Hey,
I have a prob with the control of 2 LED's...
After a button is pressed, the first LED should glow for 5 sec then the second.
This can be repeated everytime the button is pressed.
But as I uploaded the sketch, the LED's blink anyways.
The first for five second, then the second for five seconds and so on...
Here's my code:

Code: [Select]
#define STATE_IDLE 0
#define STATE_INIT 1
#define STATE_RUN        2
#define STATE_EXITING        3


int Zustand;
int led1 = 2;
int led2 = 3;
int button = 8;

void setup()
{
 Zustand = STATE_IDLE;
 pinMode(led1, OUTPUT);
 pinMode(led2, OUTPUT);
 pinMode(button, INPUT);
}

void loop()
{
 switch( Zustand )
 {
   case STATE_IDLE: Idle();
   break;
   case STATE_INIT: Init();
   break;
   case STATE_RUN: Run();
   break;
   case STATE_EXITING: Exiting();
   break;
 }
}

void Idle(){
 if (digitalRead(button == HIGH)) {
   Zustand = STATE_INIT;
 }
}

void Init()
{
 delay(1000);
 Zustand = STATE_RUN;
}

void Run()
{
 digitalWrite(led1, HIGH);
 digitalWrite(led2, LOW);
 delay(3000);
 digitalWrite(led1, LOW);
 digitalWrite(led2, HIGH);
 delay(3000);
 digitalWrite(led1, LOW);
 digitalWrite(led2, LOW);
}
 
void Exiting()
{
delay(1000);
Zustand = STATE_IDLE;
}



I'm sorry for my bad English.
Greeting,
Mathis

billroy

Is it possible the button has a floating input?  (Is there a pulldown resistor in your wiring?)

Shouldn't Run() set Zustand before it exits?

-br

UKHeliBob

The state is not updated by the Run() function so once it is STATE_RUN it will not change so it keeps on calling Run() and it goes on for ever.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Mats2006

Thank you for the Answer, but the LED's still blinks (now with my given delay) in a loop, without pressing anything.
Yes I have a pulldown resistor for the button.

Nick Gammon

Code: [Select]

  if (digitalRead(button == HIGH)) {



Bracket in wrong place.

tuxduino

...and while you're at it, you should definitely look at "blink without delay" example to replace all those delay()s ;)

Mats2006

Thank you,
with all your help it works :)
[Solved]

Go Up