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

}

}

toilet seat

Filed under: 4: Counting Sensor Input, Nadeem Haidary — nhaidary @ 12:52 pm
Tags: , ,

After originally planning on doing something with push pins, a little group brainstorming lead me to this idea: a toilet seat that tells you how many people have sat on it. A switch embedded in the seat counts, while three LED’s display the number to each new sitter by blinking each digit. Red is the hundreds place, green is tens, and blue is ones, so the video shows the 439th toilet user. The device shows how close we all really are in a very neutral way, its up to you to find it unsettling or not. I can’t figure out the source code formatting, if anyone can help me out!


/*
* Toilet seat counter
* by Nadeem Haidary
*/

int switchPin = 2;              // Switch connected to digital pin 2
int onesPin = 12;               // Led that shows one's place
int tensPin = 11;               // Led that shows ten's place
int hundredsPin = 10;           // Led that shows hundred's place
int buttonState;                // variable to hold the last button state
int val;                        // variable for reading the pin status

// counts how many people have sat on the toilet seat
// these are random starting numbers
int onesCounter = 6;
int tensCounter = 3;
int hundredsCounter = 4;

//----------------------------------------------------------------

void setup() {
pinMode(onesPin, OUTPUT);      // Set the LED pin as output
pinMode(switchPin, INPUT);     // Set the switch pin as input

Serial.begin(9600);            // Set up serial communication at 9600bps
buttonState = digitalRead(switchPin);   // read the initial state
}

//----------------------------------------------------------------

void loop(){
val = digitalRead(switchPin);   // read input value and store it in val

if (val != buttonState) {
if (val == LOW) {               // check if the button is pressed

//COUNTER
//counts each digit and resets each to zero after 9
if (onesCounter == 9) {
onesCounter = 0;
if (tensCounter == 9) {
tensCounter = 0;
if (hundredsCounter == 9) {
hundredsCounter == 0;
}
else{
hundredsCounter++;
}
}
else {
tensCounter++;
}
}
else{
onesCounter++;
}

// BLINK DIGITS
// blink hundreds digit (red LED)
for (int i = 0; i < hundredsCounter; i++) {
digitalWrite(hundredsPin, HIGH);   // turn LED on
delay (500);
digitalWrite(hundredsPin, LOW);   // turn LED off
delay (500);
}
// blink tens digit (green LED)
for (int i = 0; i < tensCounter; i++) {
digitalWrite(tensPin, HIGH);   // turn LED on
delay (500);
digitalWrite(tensPin, LOW);   // turn LED off
delay (500);
}
// blink ones digit (blue LED)
for (int i = 0; i < onesCounter; i++) {
digitalWrite(onesPin, HIGH);   // turn LED on
delay (500);
digitalWrite(onesPin, LOW);   // turn LED off
delay (500);
}
}
}
buttonState = val;                 // save the new state in our variable
}

Blog at WordPress.com.