First of all i shall introduce myself short my name is wizard, i bought the arduino uno and received it last week. I already made a couple examples from the book: getting started with arduino.
For the question I feel a bit dumb to ask cause in my current education I have mathematics and i don't even understand the state function. As the writer tells the formula doesn't make any sense in math.
who could explain it to me, cause i want't to know the full understanding rather then knowing the function.
As far i can see right now:
For example we take a button and an LED. state= 1- state. We didn't pushed the button we are starting the arduino. So that means state = 0 so that means 1-0 =1. So LED output would be HIGH then?
We push the button and state becomes 1 instead of 0. that means 1-1=0 so the LED would be LOW.
I know the other code you can use for pushbutton controlled LED that's old_val. and then we use old_val=val. But this way of controlling the LED i understand better i just want to know my first question.
const int LED = 13; //Pin for the LED
const int BUTTON = 7; //Input pin where the
//pushbutton is connected
int val = 0; //Val will be used to store the state
//of the input pin
int state = 0;
void setup() {
pinMode(LED, OUTPUT); //tell aurduino LED is an output
pinMode(BUTTON, INPUT); //and BUTTON is an input
}
void loop(){
val = digitalRead(BUTTON); //read input value and store it
//check wether the input is HIGH (button pressed)
if (val == HIGH) {
state = 1 - state;
}
if (state == 1) {
digitalWrite(LED, HIGH); //turn on LED
} else {
digitalWrite(LED, LOW);
}
}
cause val is stored as 0 the adjective is 1 and makes 1-1= state is zero. And that will cause the arduino to execute the else function and digitalWrite(LED, LOW). right?
Personally I think this is "too clever". But taking it at face value...
state is initially 0.
So 1 - state will be 1.
So now state is 1.
Then next time you have "1 - state" which is "1 - 1" which is zero again. And so it repeats.
I think it is better as a boolean:
boolean state = false;
...
if (val == HIGH)
state = ! state; // toggle state
// turn LED on or off depending on state
if (state)
digitalWrite(LED, HIGH); //turn on LED
else
digitalWrite(LED, LOW);
You forget - this is a tutorial - a learning exercise. While it may not be (i.e., definitely isn't) the best solution to the problem, it is one which introduces concepts to the novice programmer.
[quote author=Nick Gammon link=topic=109986.msg825883#msg825883 date=1339666549]
Personally I think this is "too clever". But taking it at face value...
You forget - this is a tutorial - a learning exercise. While it may not be (i.e., definitely isn't) the best solution to the problem, it is one which introduces concepts to the novice programmer.
[/quote]
At least I have a better understanding about the state function now. It seems to be silly from me asking about such a simple code, but in any case thanks for answering my question.
This is horrible code, since it does not have a defined outcome.
What you need to understand is that the arduino is cabable of executing close to 16 million instructions per second. Although your loop probably compiles to a couple hundred or even thousand assembler instructions, the loop will still execute thousands of times per second.
The loop checks if the button is pushed, and then inverses the state, and then shows the state through the LED. With no delay, even if you tap the button as fast as you can, the arduino would probably read the button as pushed at least a couple of hundred times, each time inversing the state. The LED is flashing, albeit faster than what the eye can see, but on a scope you could probably observe this. You are probably finding that after releasing the button, the LED is sometimes on and sometimes off, and that this is a random function, because it really depends on how many times the state was inversed during your last button press.
TRex:
This is horrible code, since it does not have a defined outcome.
What you need to understand is that the arduino is cabable of executing close to 16 million instructions per second. Although your loop probably compiles to a couple hundred or even thousand assembler instructions, the loop will still execute thousands of times per second.
The loop checks if the button is pushed, and then inverses the state, and then shows the state through the LED. With no delay, even if you tap the button as fast as you can, the arduino would probably read the button as pushed at least a couple of hundred times, each time inversing the state. The LED is flashing, albeit faster than what the eye can see, but on a scope you could probably observe this. You are probably finding that after releasing the button, the LED is sometimes on and sometimes off, and that this is a random function, because it really depends on how many times the state was inversed during your last button press.
Ah oke, and I did find out that the LED sometimes is on and sometimes off. The getting started guide teached me how to resolve that problem. And i understand that the state isn't a function but a variable so I won't call it wrong next time.