'Beginning C for Arduino by Jack Purdum' book errors? Modified Blink program

Im on pages 61-62 which has code instruction for adding if' functions to the void loop().
Sketch:

// Alternate blink program

int led1 = 13;
int led2 = 12;

void setup() {
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
}

//loopruns forever

void loop() {
  if (counter % 2 == 1) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    delay (1000);
  }
if (counter % 2 == 0) {
  digitalWrite(led1, HIGH);
  digitalWrite(led2, LOW);
  delay(1000); 
}
counter = counter + 1;
}

edit by mod: please include the code using the CODE tags

end sketch

The error message: error: 'counter' was not declared in this scope.
Has anyone worked through the 'Beginning C for Arduino' by Jack Purdum with this issue?

Never seen the book, but easy enough to fix...

int led1 = 13;
int led2 = 12;
int counter = 0;  //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< add this

Thank you JimboZA, my program and circuit works now.

int counter = 0; is definitely missing from my book.

vertamps:
Thank you JimboZA, my program and circuit works now.

int counter = 0; is defiantly missing from my book.

You're welcome.

I'd be surprised if nobody already told them, but maybe you should email them....

By the way, makes it easier to read code if you put it in tags, then it:

looks
{
like this
}

Just select the text you want to show as code, and hit the # button above the :wink: :sweat_smile: smilies.

If you read page 62, first paragraph from the top. it says:

"...where the new variable, counter, is a long data type that is initialized to 0 and defined just after the data definitions for led1 and led2."

Making count a long is an error. It only needs to be 1 byte long.

Mark

Making count a long is an error. It only needs to be 1 byte long.

No, it's not an error. It may be inefficient, but it's not an error, especially if you are writing for people who have no prior programming experience and you haven't discussed all of the data types yet.

I would think that in this context an int would be appropriate as that's the sort of defacto "standard" for a general-purpose counter.


Rob

JimboZA:
By the way, makes it easier to read code if you put it in tags, then it:

looks

{
like this
}

Uhm.. I prefer this way:

code {
  block
}

I know there isn't the "best" way to use the braches but the latter saves a line for each block of code

Noooo, don't start a brace-location war :slight_smile:

BTW, that's my favourite method as well, and for the same reason.


Rob

leo72:
I know there isn't the "best" way to use the braches but the latter saves a line for each block of code

Are lines expensive?

leo72:

JimboZA:
By the way, makes it easier to read code if you put it in tags, then it:

looks

{
like this
}

Uhm.. I prefer this way:

code {

block
}



I know there isn't the "best" way to use the braches but the latter saves a line for each block of code

That wasn't the point of my post, which was that code tags are better than not using code tags. Regardless of the way you place braces, .....

code that
is in
this font and in 
a box
analogWrite(5, 8);

.... is better than ....

code that
is in
this font and not
in a box
analogWrite(5, 8);

Yes. They cost vertical real estate on my display. :slight_smile:

^^, yep, the more lines of code I can see at once the better.


Rob

Graynomad:
Noooo, don't start a brace-location war :slight_smile:

Mea culpa, mea maxima culpa.... :sweat_smile:

if you are writing for people who have no prior programming experience and you haven't discussed all of the data types yet.

then the best thing to do is to get them in the right habits from the very beginning.

Mark

holmes4:

if you are writing for people who have no prior programming experience and you haven't discussed all of the data types yet.

then the best thing to do is to get them in the right habits from the very beginning.

Mark

So, get them toggling a single bit.

Yes, if you believe that direct port manipulation is better than digitalWrite().

Mark

Who mentioned ports?
We're talking about variables here.

First, the post's tag line is incorrect. There is no error in the code. However, some of the comments here are different views of how the same thing should be done. The problem is that everyone commenting knows C cold, but some readers do not, including the OP, and that's who this book is trying to reach. Clearly, I would not write the statement:

   counter = counter + 1;

if I had already introduced the increment operator by this point in the text. There are a host of other operators and constructs I could have used (e.g., bit operators, port manipulation, the not operator), but they had not been introduced by this point in the book. The comments being made are done so out of the context in which the exercise was presented. The real problem, as I pointed out in an earlier post, is that the reader didn't read the text that is tied to the exercise, but rather simply copied the code from the book, ignoring the text telling them to add the variable named counter. Also, the point of the exercise was to show the reader the modulo operator and how it could be used with a counter to blink an LED.

It's frustrating to see someone say there's an "error" in the book when in fact the error is in their not reading the text that accompanies the code. Indeed, the OP says:

Thank you JimboZA, my program and circuit works now.

int counter = 0; is definitely missing from my book.

yet the very first line below the code the OP talks about says:

"...where the new variable, counter, is a long data type that is initialized to 0 and defined just after the data definitions for led1 and led2."

Normally, I would just sit in the background and dismiss the comment by the OP as someone who doesn't read the book carefully, but because so many experts have weighed in without understanding the context in which the exercise was presented, I felt I needed to say something.