I bought an Arduino Uno yesterday and the book "Getting started with Arduino" by Massimo Banzi. I follow the examples in the book but having some problems understand the programming language. As always I need to understand why and how things happens.
Guess i'm gonna spend alot of time on this forum posting stupid questions
Stupid question #1:
delay(1000)
Why the use of 1000 milliseconds for one second and not just 1?
Wouldn't that be much easier?
Why the use of 1000 milliseconds for one second and not just 1?
Wouldn't that be much easier?
The delay() function was written that way. It counts in milliseconds.
If it were to count in seconds there wound be problems with delay half a second since you would have to write delay(0.5), but this is a no go since delay() only accepts integer numbes as input.
You might wonder - how do I delay half a millisecond?
delayMicroseconds(500);
Just one final note - once you figured out how the blink example works. The next think you do is look at the blink with out delay example. delay() is not your best friend since it hangs the processer - kinda like putting the kids infront of Disney channel
Why the use of 1000 milliseconds for one second and not just 1?
Wouldn't that be much easier?
You need to start thinking on a different timescale.
The Arduino processor executes a single machine instruction in the time it takes a beam of light to travel about 20 metres.
One second is sixteen million such cycle times - that's a lot of instructions.
Even a millisecond is a long time to a microcontroller.
After playing around with the code I found out the I could type 1 instead of HIGH and 0 instead of LOW and still get my LED to work.
So why use HIGH or LOW in the code?? Does it any other purposes? Or is it only to make things easier for noobs like me? (actually I found it more confusing with the HIGH and LOW)
I am now at example 03C in the book "Getting started with Arduino". When a button is pushed and released the LED should stay on. The setup works, but I can't seem to wrap my mind around the code
Could someone please re-write the authours comments so I perhaps can see things from a different angle?
The lines I have problem with starting at: void loop(); and down to the bottom.
#define LED 13 //the pin for the LED #define BUTTON 7 //the input pin where the pushbutton is connected
int val = 0; //val will be used to store the state of the input pin
int old_val = 0; //this variable stores the previous value of "val"
int state = 0; //0 = LED off and 1 = LED on
void setup() {
pinMode(LED, OUTPUT); //tell Arduino LED is an output
pinMode(BUTTON, INPUT); //and BUTTON is an input
}
void loop(){
val = digitalRead(BUTTON); //read input value and store it
//yum, fresh
//check if there was an transition
if ((val == HIGH) && (old_val == LOW)){
state = 1 - state;
delay(10);
}
old_val = val; //val is now old, let's store it
if (state == 1) {
digitalWrite(LED, HIGH); //turn LED on
} else{
digitalWrite(LED, LOW);
}
}
void loop(){
 int val = digitalRead(BUTTON); //read input value and store it
               //no need for this to be a global.
 //check if there was an transition
 if ((val == HIGH) && (old_val == LOW)){
  state = 1 - state; // if state was zero, make it one. If it was one, make it zero
  delay(10);      // Wait 1/100th of a second.
 }
 old_val = val; //val is now old, let's store it
 digitalWrite(LED, state);}
The code is checking whether the switch is pressed, on each pass through loop. If it is, val will be HIGH.
The current value in val is compared to HIGH. If it matches, the switch is pressed. So, now we want to know if this represents a change. If it does the switch is now pressed, but was not before, is not now pressed but was before. Since the check is only performed when the switch is pressed, the result is true if the switch is pressed now, but was not pressed last time we checked.
If that is the case, the state variable is assigned a new value - whatever it was not. If it was HIGH, ir will become LOW. If it was LOW, it will become HIGH.
The if test that follows, that checks state is unnecessary. One could simply call digitalWrite(LED, state).
int val = 0; //val will be used to store the state of the input pin
int old_val = 0; //this variable stores the previous value of "val"
int state = 0; //0 = LED off and 1 = LED on
Question:
On line 2 the author says: "this variable stores the previous value of val"
Does he refer to line 1?
if ((val == HIGH) && (old_val == LOW)){
state = 1 - state; // if state was zero, make it one. If it was one, make it zero
delay(10); // Wait 1/100th of a second.
If the button is pressed (val = high) but it wasn't pressed a 1/100th of a seconds ago, the button was pressed for sure, now turn that LED on!
Yes, that's correct.
As I said earlier, there is no need for "val" to be stored as a global, but "old_val" does need to be stored as a global, because you need to retain the value from every iteration of "loop".
If you declared "old_val" inside "loop", (unless you declared it "static") it would be destroyed each time "loop" exited.
You need to store the old value and compare it to the new value to detect a change.
(and yes, it was difficult learning to program, not least because it took days to get your results back from the local university's computer centre, having typed your program onto paper tape, got on the bus, handed the tape over at the centre, gone back to school, slept on it, got back on the bus...)
So you're saying that I could delete the line "int val = 0; " and only use "int old_val = 0;" ?
It would be something like this: (like if I were a 5-year old)
Loop starts
check if old_val is 0
Button have now been pressed making old_val 1
how was it 1/100th a second ago?
1/100th of a second ago it was 0
turn LED on
loop starts again
if old_val is 0
how was it 1/100th a second ago?
1/100th of a second ago it was 1
and since it was 1 (if (old_val = HIGH))
keep the LED on (since we compare it to HIGH)
loop starts again
check if old_val is 0 // but this time the button have been pressed to turn LED off
the old_val is now 0
1/100th of a seconds ago it was 1
since it is now 0
turn the LED off
Hmm
"Global" is a new term for me. I haven't been introduced to that yet.
How often does the "loop" run? Is it like like the refresh rate on a television set? (updates constant and so fast you cant see it)
I have two projects I would like to do with the Arduino and I really want to learn this coding stuff.
(yes, that seem like a real pain. I guess you have been coding for some time?)
"Global" is a new term for me. I haven't been introduced to that yet.
Global means defined outside of a function. If it defined like this, then it is visible in all functions.
If you declare a variable inside a function, it is only visible inside that function, and is destroyed when the function exits, unless declared with the qualifier "static", in which case, its value "survives", but is still only visible within the function.
Have a look at the reference and the rules for "scope".
There are good reasons not to have too many variables with global scope.
Since "val" changes every time through "loop", it is better to declare it inside "loop", whereas "old_val" must be maintained when "loop" exits.
How often does the "loop" run?
That depends on how much code there is in loop.
But mostly, many thousands of times a second.
This is getting way over my head... (like it wasn't already :()
So how would you write the code?
Maybe if I can see your code I could put 1 and 1 together. Right now I have bits and pieces but can't fit them together.
Ok, so I put the book aside for a while and went with this Arduino tutorial instead: Arduino Tutorial - Lesson 5.
Here I found another solution on the problem regarding the flaky results one can get from the small switch button that comes with the Arduino kit. In this example a pull-down resistor (10K Ohm) is used. For me this solution was much easier to understand than doing the same thing using code. Guess i'm more of the "hands-on" kind of guy.
Now, what solution is the best one to use?
Should I go with pull-down resistors or code in the future?
If someone feels like answering, please remember i'm a noob. If possible, explane like you would to a 5-year old because there are hundreds of things spinning inside my head right now regarding the Arduino hardware and coding