Making Things Interactive

February 2, 2008

counting code: where is everyone?

Filed under: 4: Counting Sensor Input,Assignments,Gee Kim — gskim @ 6:17 pm

This program has a input switch that counts how many people are in the room, and depending on the number of people in the room, the red and green LED will light up, and the computer will print a certain phrase.

The red LED stays lit and the computer prints “WHERE IS EVERYONE?!?” when the switch is hit 5 or less times.

The red LED turns off, the green LED lights up, and the computer prints “HOORAH! everyone is here.” when the switch is hit for the 6th to 10th time.

The green LED turns off, the red LED turns back on, and the computer prints “GET OUT! there are too many ppl here.” when the switch is hit more than 10 times.


* Where is everyone? (count code)

int switchPin = 2;          // switch connected to digital pin 2
int count = 0;
int ledPin = 8;             // red led connected to digital pin 8
int ledPin2 = 10;           // green led connected to digital pin 10

void setup()                // run once, when the sketch starts
Serial.begin(9600);         // set up Serial library at 9600 bps
pinMode(switchPin, INPUT);  // sets the digital pin as input to read switch
pinMode(ledPin, OUTPUT);    // sets the digital pin as output to read pin
pinMode(ledPin2, OUTPUT);   // sets the digital pin as output to read pin 2

void loop()                                         // run over and over again
int newCount = count + digitalRead(switchPin);      // show count only when +1 than previous count
if( (newCount == count + 1)) {                      // if newCount is +1 than previous count
Serial.print("Number of ppl in the room: ");        // than print "Number of ppl in the room:"
Serial.println(newCount);                           // print the value of new count
count = newCount;                                   //
if (count <= 5) {                                   // if count <= 5
digitalWrite(ledPin, HIGH);                         // than turn on red led
Serial.println("WHERE IS EVERYONE?!?");             // and print "WHERE IS EVERYONE?!?"
else if ((count >= 6) && (count <= 10)) {           // if count is >= 6 AND <= 10
digitalWrite(ledPin, LOW);                          // than turn off red led
digitalWrite(ledPin2, HIGH);                        // and turn on green led
Serial.println("HOORAH! everyone is here.");        // and print "HOORAH! everyone is here."
else if (count >=11) {                              // if count is >= 11
digitalWrite(ledPin2, LOW);                         // than turn off green led
digitalWrite(ledPin, HIGH);                         // and turn on red led
Serial.println("GET OUT! too many ppl here!");      // and print "GET OUT! too many ppl here!"
delay(2000); // read every 2 seconds

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment: Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

Create a free website or blog at

%d bloggers like this: