Problem Understanding Arduino Code

shanecy (2)
First off I want to list my code that I've typed up on the Arduino interface.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//  Programmer:  Shane Yost
//  Date:        February 9, 2013
//  Project:     Push Button LED
//  Model:       SY_UNO

const int LED = 12;               //Digital Pin is 12
const int BUTTON = 7;             //Input pin for Push Button

int value = 0;                    //Variable to store the state of the input pin 7 Push Button
int old_value = 0;                //Variable to be compared to in the following inerations
int state = 0;                    //Varialbe that is remembered and turns the light on/off constantly

void setup() 
{
 pinMode(LED, OUTPUT);            //Tell Arduino the LED is an output
 pinMode(BUTTON, INPUT);          //Tell Arduino the BUTTON is an input
}

void loop()
{                                

 value = digitalRead(BUTTON);     //read input value and store it to value
 
 if((value == HIGH) && (old_value == LOW))                //Check for a transition
 {                               
  state = 1 - state;
  delay(10);
 }                              
 
 old_value = value;               //value is now old so store it
 
 if(state == 1)
 {                                
  digitalWrite(LED, HIGH);         //Turn on LED
 }
 
 else
{                                 
 digitalWrite(LED, LOW);
}                                

}            



This is the code I'm using to make a simple LED interact with a Push-Button switch. The code works so no questions on that as it's right out of the book anyways. My question is about how it's going through the loop iterations.

Lets say I press the switch on. From what I understand the following is now assigned:

value = 1
old_value = 0
state = 0

Since the first "if" statement is true the expression

state = 1 - state

is executed. After it executes we know see the following as:

value = 1
old_value = 0
state = 1 //state was "0" and after 1-0 executes we have 1.

Now we have the two statements

old_value = value
if(state == 1)
digitalWrite(LED, 1)

I'm assuming now the following is assigned:

value = 1
old_value = 1
state = 1

Now for my main question. When we execute this loop again I'm assuming we are starting out with the latest assigned values:

value = 1
old_value = 1
state = 1

If this is the case the first "if" statement doesn't execute since old_value isn't equal to zero anymore. But in order for this second iteration to work the expression:

state = 1 - state

has to be executed turning the state to zero. This will execute the "else" statement. However, I thought the above expression only executes if the first "If" statement is met or seen as true. Therefore, I'm having a hard time making sense of how this loop statement is actually executing.

Thanks for any guidance/information you have.





palauan73 (19)
when you push the button, are you holding it down or just pushing and then releasing?
shanecy (2)
I'm just pushing it down and then releasing it. It's not a button that when it gets pressed "once" creates a closed loop. It only creates a closed loop if you keep it pressed. But somehow when I enter this code and press the button it makes the LED behave like it's really creating a closed loop all the time until pressed again. Therefore i'm confused how this loop is actually working.
buteman (1)
This seems to be a very complicated way of doing what you want to me.
To make sure I am understanding this correctly I believe that what you want is :-

Press and release the button and the LED lights up.
Press and release it again and the LED turns off.

Is that correct?
Registered users can post here. Sign in or register to post.