# I'm new, Can you explain this to me? "counter"

Hello all, first post.

I’m very new to arduino and programming in general. I began reading a book- “Beginning C for Arduino” (by Jack Purdam) .

I have been doing somewhat ok until I got to a certain point, now i feel stuck. Screenshots of the 2 pages are attached (i hope).

basically, when I typed this code, it didn’t do anything. almost seems like IDE didn’t reconize it or I missed a step that the Author maybe assumed i should know.

I tried searching the forum for “counter” and the results seem over my head.

Let me know any more info I can give. I don’t understand the lingo yet, still trying to sort out what functions, statements, operations are… take it easy on me Thanks! Paul

Post YOUR code. Also use the code tag.

I'll work on getting that up. It'll be a few mins.

counter in those examples is just a number. Most probably a variable of type int or char.

It is simply incremented at the end of the loop() function, which is called in a endless loop...

The if statement is effectively checking whether the counter variable is even or odd. (% is the modulus operator ), also in C/C++ 0 naturally converts to false, whereas any other number (positive or negative) converts to true.

E.g

0 % 2 == 0 == false
1 % 2 == 1 == true
2 % 2 == 0 == false
3 % 2 == 1 == true
4 % 2 == 0 == false

Personally I'd just check to see if bit zero is set. If its not then the number is even. (counter & 0x01)

Here is an attachment of the sketch

As for the code tag… I don’t know what that means. I’ll look into it.

counter2_15_15.ino (352 Bytes)

pYro_65:
counter in those examples is just a number. Most probably a variable of type int or char.

It is simply incremented at the end of the loop() function, which is called in a endless loop...

The if statement is effectively checking whether the counter variable is even or odd. (% is the modulus operator ), also in C/C++ 0 naturally converts to false, whereas any other number (positive or negative) converts to true.

E.g

0 % 2 == 0 == false
1 % 2 == 1 == true
2 % 2 == 0 == false
3 % 2 == 1 == true
4 % 2 == 0 == false

Personally I'd just check to see if bit zero is set. If its not then the number is even. (counter & 0x01)

Thankyou, that makes sense. I didn't even recognize % as an operator.

In your code, you have duplicated the if, they can be combined using the else explained in that article.
Also you need to declare 'counter'. I added it as a local static, however you could declare it outside the functions as a global variable (like led1, led2).

This appears to work, (no LED in pin 12, but 13 flashes fine.)

``````int led1 = 13;
int led2 = 12;

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

void loop() {

static unsigned char counter = 0;

if (counter % 2 ==1) {
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
delay(500);
}else{
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
delay(500);
}
counter = counter + 1;
}
``````

liudr:
Post YOUR code. Also use the code tag.

ok, code tag- let's see if this works...

``````int led1 = 13;
int led2 = 12;

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

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

You can also put int counter=0; outside loop or setup, where led is defined.

from low(pull down(to the ground) with a 10k resistor = low) to press the button ( 5v = high)
= counter ++ = counter = counter + 1

% = modulo
press the pushbutton four times = four (first low then high low then high ...)

four % 4 == 0?

is it 0? ( no remainder)

4 divided by 4 the remainder is zero of course

turn on the led = ledpin(high)

pYro_65:
In your code, you have duplicated the if, they can be combined using the else explained in that article.
Also you need to declare 'counter'. I added it as a local static, however you could declare it outside the functions as a global variable (like led1, led2).

This appears to work, (no LED in pin 12, but 13 flashes fine.)

Cool! thankyou. Another question..

why is the variable (counter) exactly the same as the (counter) in operator?

counter = counter + 1;

This question may not make sense but i'm not sure how to word it.

liudr:
You can also put int counter=0; outside loop or setup, where led is defined.

Does it matter where you define variables? whether outside or inside either function?

counter = counter + 1

is not an equation. It is not an algebraic problem for you to solve. Indeed, as an algebra problem, it would be impossible to solve in any normal kind of arithmetic.

it is an assignment statement. Calculate the value on the right hand side, and assign that value to the variable on the left hand side.

Does it matter where you define variables? whether outside or inside either function?

Yes, it does matter.

I suggest you buy an elementary C/C++ textbook, and read it from cover to cover. That will save your time and ours.

Here is why

michinyon:
counter = counter + 1

is not an equation. It is not an algebraic problem for you to solve. Indeed, as an algebra problem, it would be impossible to solve in any normal kind of arithmetic.

it is an assignment statement. Calculate the value on the right hand side, and assign that value to the variable on the left hand side.

Ah, ok. Thankyou.

michinyon:
Yes, it does matter.

I suggest you buy an elementary C/C++ textbook, and read it from cover to cover. That will save your time and ours.

lol, sorry. I shouldn't be asking such fundamental questions. Just figured i was here and typing and got a little carried away.. lol

A book is good since it is systematic. Online tutorials are not too good for building foundations. They are mostly isolated knowledge points.