I'm stepping through Simon Monk's excellent book "Programming Arduino", trying many of the sketches provided.
On page 54 he says of Sketch 4-03: "This sketch is broken. It will not work, because every time loop is run. the variable count will be given the value 0 again, so count will never reach 20 and the LED will just keep flashing forever."
But it works fine here, giving a 3 s delay after every 20 flashes!
// sketch 04-03
const int ledPin = 13;
const int delayPeriod = 250;
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
static int count = 0;
digitalWrite(ledPin, HIGH);
delay(delayPeriod);
digitalWrite(ledPin, LOW);
delay(delayPeriod);
count ++;
if (count == 20)
{
count = 0;
delay(3000);
}
}
Terry, East Grinstead, UK
Verified here. IDE 1.8.10
The static keyword causes the count variable to retain its previous value next time through loop(). Without it count would be reset to zero each time through loop() as he describes.
Have you copied the correct sketch ?
aarg:
Verified here. IDE 1.8.10
Thanks. Just to be clear, do you mean the code works or doesn't work?
UKHeliBob:
The static keyword causes the count variable to retain its previous value next time through loop(). Without it count would be reset to zero each time through loop() as he describes.
Have you copied the correct sketch ?
Are you confirming that the LED repeats indefinitely for you, with no delay after the 20th flash, as the author says and as the logic implies? It does not do that for me. I pasted the code from my post into a new sketch (using 1.8.10 ) and it behaves as I described, with a repeated 3 s delay in each cycle.
There should be a 3 second delay. Try removing the 'static' keyword and see what happens.
aarg:
There should be a 3 second delay. Try removing the 'static' keyword and see what happens.
There is a 3 s delay! Did you run it?
The code I posted for sketch 4-03 is included here
It's identical to the text on page 53 of the book.
Yes, I am running it. I see a 3 second delay. What is your question? Did you try my suggestion in reply #5?
My question was as described my opening post. The sketch simply does not do what its author says!
Although I found 'Programming Arduino' well written and readable for a novice like me, I conclude that Simon's sketches may not have been carefully edited. This is obviously one of them. I now see 4.03 is identical to 4.04, in which he introduced the static variable.
The code in the zip file that you linked to does not have the static keyword hence will not work as required
void loop()
{
int count = 0;
digitalWrite(ledPin, HIGH);
delay(delayPeriod);
digitalWrite(ledPin, LOW);
delay(delayPeriod);
count ++;
if (count == 20)
{
count = 0;
delay(3000);
}
}
From where did you copy the code with the static keyword ?
It sounds to me, as has been hinted already, that that code is from a section of the book intended to illustrate one use of the static keyword.
The code you have posted looks like the non-broken version of the sketch. Are you sure you have the right one? Perhaps the publisher made a mistake; the two sketches would likely look identical to a non programmer.
Reminds me of a picture in a firefighting training manual that tells you how to place your feet to brace a ladder. The picture was apparently a bit too big to fit neatly so the publisher cut a piece off the bottom, thereby removing the firefighter's feet from the image.
UKHeliBob:
The code in the zip file that you linked to does not have the static keyword hence will not work as required
See my conclusion in post 8.
void loop()
{
int count = 0;
digitalWrite(ledPin, HIGH);
delay(delayPeriod);
digitalWrite(ledPin, LOW);
delay(delayPeriod);
count ++;
if (count == 20)
{
count = 0;
delay(3000);
}
}
From where did you copy the code with the static keyword ?
From the original source, which is identical to both that in the ZIP I gave and to the code in the book.
wildbill:
It sounds to me, as has been hinted already, that that code is from a section of the book intended to illustrate one use of the static keyword.
Yes, as I conclude in post 8.
The code you have posted looks like the non-broken version of the sketch. Are you sure you have the right one?Perhaps the publisher made a mistake; the two sketches would likely look identical to a non programmer.
Reminds me of a picture in a firefighting training manual that tells you how to place your feet to brace a ladder. The picture was apparently a bit too big to fit neatly so the publisher cut a piece off the bottom, thereby removing the firefighter's feet from the image.