Arduino 4: Counting Events

Image made with Fritzing (http://fritzing.org/)

A common requirement is to count digital input signals, like how many times a button is pressed. This program uses a button, one button pin connected to +5V, the other button pin connected to both Arduino pin 8 and a 10K resistor to ground. You need the resistor to ground so that when the button is not pressed, the Arduino pin reads LOW (0V, which is ground) and not some random voltage. The code follows:

int state=LOW;
int lastState=LOW;
int count=0;
void setup(){
Serial.begin(9600);
pinMode(8, INPUT);
state=digitalRead(8);
}
void loop(){
if (state==HIGH && lastState==LOW){
count++;
Serial.println(count);
}
lastState=state;
state=digitalRead(8);
}

Notice you can test two statements at once in an IF statement. The && symbol means “and”, so only if both “state is HIGH” and “lastState is LOW” will the variable count increase. Note also the notation

count++;

A double-plus sign after an integer variable adds one to the number. Similarly a double-negative (count–) would subtract one from the number.

Modulo division

A variation on this idea is to repeatedly count through a sequence of numbers, like 1-2-3-1-2-3-1-2-3, perhaps to cycle through different input or output ports. You can do this easily using the idea of modulo division, which divides one integer by another, and returns the remainder of the division. The symbol for modulo division is %, so

x=9%4;

takes 9/4 and returns 1, the remainder. The following program cycles between turning on three LED’s every time you push a button.  The button is now on pin 3, again with a 10K resistor to ground, and the other pin to +5V. LED’s are on pins 5,6 and 7, each to a 220-ohm resistor (red-red-brown) to ground. The code follows the diagram.

int state=LOW;
int lastState=LOW;
int count=0;
int remainder=0;
int lastPin=5;
void setup(){
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7,OUTPUT);
pinMode(3, INPUT);
state=digitalRead(3);
digitalWrite(lastPin,HIGH);
}
void loop(){
if (state==HIGH & lastState==LOW){
count++;
remainder=count%3;
digitalWrite(lastPin, LOW);
digitalWrite(5+remainder, HIGH);
lastPin=5+remainder;
}
lastState=state;
state=digitalRead(3);
}

4 responses to “Arduino 4: Counting Events

  1. Hi, I can’t understand the usage of “digitalWrite(5+remainder, HIGH);” and ” lastPin=5+remainder; “. Would you mind telling me the meaning of these two statements, plz

  2. Hallo, can not asebled, why error? Thanks.
    Hallo, warum bringt beim asemblieren fehler?
    danke und Grüße.

  3. Hi Paul,

    There is a typing mistake in the first program, it make the program can’t compile

    if (state==HIGH && LastState==LOW){

    The “LastState” shuld be “lastState”

    Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s