Pages: [1]   Go Down
Author Topic: Button and LED with different functions.  (Read 431 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've been trying to familiarize myself with the Arduino. I'm trying to modify the code for turning on/off an LED with a button from the Getting Started with Arduino book (pg 49). What I want it to do is cycle through different outputs. I want it to be off when it starts. On the first button press turn the LED on. Second press to make the LED blink. Then to make it fade from off to on and back to off and then finally turn itself back off. When I upload my code, it goes straight to the last sequence (in the posted code it goes straight to blinking LED). I've used my googlefu and searched this forum for something similar and have run into a dead end. I thank you in advance for your responses.

Code:
const int LED = 9;
const int BUTTON = 7;
int val = 0;
int old_val = 0;
int state = 0;

void setup()
{
  pinMode(LED, OUTPUT); //led output
  pinMode(BUTTON, INPUT); //buttin input
}

void loop()
{
  digitalRead(BUTTON); //button press add state
  if ((val == HIGH) && (old_val == LOW))
  {
    state++;
    delay(10);
  }
 
  old_val = val;
 
  if (state = 0)
  {
    digitalWrite(LED, LOW); //first state LED off
  }
 
  old_val = val;
 
  if (state = 1)
  {
    digitalWrite(LED, HIGH); //second state LED on
  }
 
  old_val = val;
 
  if (state = 2)
  {
    digitalWrite(LED, HIGH); //third state blinking LED
    delay(1000);
    digitalWrite(LED, LOW);
    delay(1000);
  }
  //fourth state is going to be a fade from off to on and back to off
}
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your code, as posted, does not compile.

So what kind of help are you looking for?
Logged

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

Weird. When I verify it accepts it. Initially the led should be off. When i press the button the first time I want it to turn on. When I press it the second time I want the led to blink. The third time fade in and out. The fourth time for the program to cycle back through.

Edit: what i think i need help with is having the program cycle through the phases of the led. When I upload that code it starts at the last one (in this code the blinking)
« Last Edit: March 15, 2013, 10:52:08 pm by tetra » Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My mistake I apparently didn't grab the first 2 lines!

I will point you at another thread with roughly similar goal -

>http://arduino.cc/forum/index.php/topic,144366.msg1085559.html#msg1085559>
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Did you mean for this -

Code:
digitalRead(BUTTON); //button press add state

to be -

Code:
val = digitalRead(BUTTON); //button press add state


Code:
const int LED = 9;
const int BUTTON = 7;
int val = 0;
int old_val = 0;
int state = 0;

void setup()
{
    pinMode(LED, OUTPUT); //led output
    pinMode(BUTTON, INPUT); //buttin input
}

void loop()
{
    digitalRead(BUTTON); //button press add state
    if ( (val == HIGH) && (old_val == LOW) )
    {
        state++;
        delay(10);
    }

    old_val = val;

    if ( state = 0 )    // <<<--- assignment not simply a test
    {
        digitalWrite(LED, LOW); //first state LED off
    }

    old_val = val;

    if ( state = 1 )    // <<<--- assignment not simply a test
    {
        digitalWrite(LED, HIGH); //second state LED on
    }

    old_val = val;

    if ( state = 2 )    // <<<--- assignment not simply a test
    {
        digitalWrite(LED, HIGH); //third state blinking LED
        delay(1000);

        digitalWrite(LED, LOW);
        delay(1000);
    }

    //fourth state is going to be a fade from off to on and back to off
}
« Last Edit: March 15, 2013, 11:05:07 pm by lloyddean » Logged

France
Offline Offline
Edison Member
*
Karma: 38
Posts: 1012
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

2 things I noticed, first:
Code:
digitalRead(BUTTON); //button press add state
This does nothing, you probably want to store the result of this function call into "val", so do:
Code:
val = digitalRead(BUTTON); //button press add state

2nd, you do:
Code:
if (state = 1)
This is not a test for equality, it's a value assignment. To test equality you have to use
Code:
if (state == 1)

Edit: beaten by lloyddean smiley

You have some logic problems in your code, try to understand what it does.
« Last Edit: March 15, 2013, 11:11:36 pm by guix » Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey, I didn't touch him!
Logged

Pages: [1]   Go Up
Jump to: