Go Down

Topic: Button and LED with different functions. (Read 450 times) previous topic - next topic

tetra

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: [Select]
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
}

lloyddean

Your code, as posted, does not compile.

So what kind of help are you looking for?

tetra

#2
Mar 16, 2013, 04:50 am Last Edit: Mar 16, 2013, 04:52 am by tetra Reason: 1
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)

lloyddean

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>

lloyddean

#4
Mar 16, 2013, 05:03 am Last Edit: Mar 16, 2013, 05:05 am by lloyddean Reason: 1
Did you mean for this -

Code: [Select]

digitalRead(BUTTON); //button press add state


to be -

Code: [Select]

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



Code: [Select]

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
}


guix

#5
Mar 16, 2013, 05:06 am Last Edit: Mar 16, 2013, 05:11 am by guix Reason: 1
Hello,

2 things I noticed, first:
Code: [Select]

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: [Select]

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


2nd, you do:
Code: [Select]

if (state = 1)

This is not a test for equality, it's a value assignment. To test equality you have to use
Code: [Select]

if (state == 1)


Edit: beaten by lloyddean :)

You have some logic problems in your code, try to understand what it does.

lloyddean


Go Up