### Problem Understanding Arduino Code

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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` ``````// 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.

when you push the button, are you holding it down or just pushing and then releasing?
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.
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?
closed account (3qX21hU5)
No he want it so when you press and hold the button the LED lights up, but the second you release the button it turns off I believe. Atleast that is what I got from it.
Shanecy I am puzzled.
Where did you get the code from?
You think it's not working but you say it is working.
So what makes you think it is working and what makes you think it isn't working?

The loop() code runs continuously until you remove power or upload a different sketch so it's always checking for a button press

The delay is only 10 milliseconds which is a minute amount of time in human terms so I imagine it reads the value several times while the button is pressed however quickly you press and release it.
You might like to change the delay to something like 500 or 1000 so you get a better idea as to what is happening.

Do you mean that if you press the button and the LED was off it turns on and stays on until you let go?

Or that it stays on until you press it again?

If you just want it to be on while you are pressing the button try this:
 ``12345678910111213141516171819202122`` ``````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() { while(digitalRead(BUTTON)) { digitalWrite(LED, HIGH); //Turn on LED and keep it on while you are pressing the button } digitalWrite(LED, LOW); //Turn LED off } ``````
Last edited on

This is a block of code from the Arduino site. My question was more of the idea of how the void loop was executing. I was having a hard time understanding how the code was going through the motions as I explained.

I think I have a better understanding now as I have played around with my code and then implemented your code as well. But thanks for the reply as it contributed greatly in clarification to my questions.
@shanecy
Oh the Arduino. I'm such an Arduino whore as of late.

Even though you say you have a better understanding, I'm going to give my take on it (step-by-step) in the event that maybe something will help you more.

Although, I have to agree with buteman, it does seem like a weird way to implement that .

So, pretty much, initially, the variables 'value' and 'old_value' are initialized to LOW (which is 0), and 'state' is initialized to 0, which I interpret as "LED off."

so the loop portion of the code.

The very fist bit, it reads whether BUTTON is HIGH or LOW, and that is stored in 'value'.
if value == HIGH, and old_value == LOW, that means that you have just clicked the button, and the LED state needs to be toggled.
On the other hand, if value == HIGH and old_value == HIGH, the LED is already on, and you're still holding the button; the state doesn't need to be updated. Likewise, if value == LOW, and oldvalue == HIGH, you've just released the button, and nothing needs to be done.

So, if value == HIGH, and old_value == LOW, state = 1 - state.
This part confused me for a while, because I couldn't figure out exactly what that was trying to accomplish.

so, if state = 0, which it does initially, state = 1 - state, so state = 1 - 0 = 1.
Later in the implementation, if state == 1, which, later in the loop, writes HIGH to the LED pin.

So every time you click the button, that if condition will be met (value changed from LOW to HIGH), and state will change from 1 to 0, or 0 to 1.
So upon clicking the button, then the light should turn on.
You click the button again, then the light should turn off.

Is that what is happening? If not, I cannot explain the results.

I think it would seem more intuitive if 'state' was a boolean variable instead of an int.
Last edited on
Topic archived. No new replies allowed.