Making Things Interactive

February 4, 2008

Switch Counter

Filed under: 4: Counting Sensor Input,Assignments,Thomas Hendrickson — tphendrickson @ 6:25 pm

So after a while of not getting my wiring/coding correct, I finally was able to put together a simple switch that increments a counter and flashes an LED everytime the switch is activated.

The counter value is displayed everytime the switch is activated. This simple switch could be replaced with any other sensor, such as an impact sensor that counts how many people walk on a rug.


int ledPin = 13;              //designates LED pin
int switchPin = 4;            //designates probe pin
int delayAmount = 700;
int switchStatus = 0;         //starts the switch status at LOW
int counter = 0;              //the counter is set at 0

void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);    //LED is the output
pinMode(switchPin, INPUT);  //the switch feeds in the information
}

void loop()
{

switchStatus = digitalRead(switchPin);  //switch gets an initial value

if (switchStatus == HIGH)    //this if statement checks to increment the counter
{
counter++;
Serial.print("The circuit has been completed ");
Serial.print(counter);
Serial.println(" times.");

}

while (switchStatus == HIGH)  //this while loop keeps the LED blinking while switch is activated
{

digitalWrite(ledPin, HIGH);
delay(delayAmount);
digitalWrite(ledPin, LOW);
delay(delayAmount);
switchStatus = digitalRead(switchPin);   //the switch status is checked before flashing the LED again

}

}
Advertisements

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:

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

Create a free website or blog at WordPress.com.

%d bloggers like this: