Pages: [1]   Go Down
Author Topic: [Solved] LED's blinks anyways  (Read 571 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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
« Last Edit: January 31, 2013, 03:06:59 am by Mats2006 » Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 988
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 118
Posts: 4407
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19367
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  if (digitalRead(button == HIGH)) {


Bracket in wrong place.
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you,
with all your help it works smiley
[Solved]
Logged

Pages: [1]   Go Up
Jump to: