| shanecy (2) | |||
First off I want to list my code that I've typed up on the Arduino interface.
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? | |
|
|
|