Wednesday, January 28, 2009

Unfortunately, I lost my camera cord, so there's no pictures, but essentially I made a sign that connects to a toilet-mounted-switch; when the toilet seat is up, it flashes "caution - not suitable for female use" in red, and when the seat is down, "bathroom OK" in green.

initially, it was pretty seamless, but i ran into some trouble when i wanted to make the red (and only the red) blink. I worked it out, and it all seems to work. I'll bring it in.

here's the code:

//set variables:
int switchPin = 2; // digital input for switch to pin 2
int redledPin = 3; // digital output for red LED to pin 3
int greenledPin = 4; // digital output for green LED to pin 4
int switchState = 0; // the state of the switch (open/off)


void setup() {
pinMode(switchPin, INPUT); //set the switch pin to be input
pinMode(redledPin, OUTPUT); //set the red LED pin to be output
pinMode(greenledPin, OUTPUT); //set the green LED pin to be output
switchState = digitalRead(switchPin); //read the input from the switch
}




void loop() {

if (switchState == 0) {
digitalWrite(greenledPin, HIGH); //green LED high if the switch is open
}

else if (switchState == 1) { //if the switch is on

digitalWrite(greenledPin, LOW); //turns off the green LED
digitalWrite(redledPin, HIGH); //turns on the red LED
delay(750); //wait .75sec
digitalWrite(redledPin, LOW); //turns off red LED
delay(250); //wait .25 secs
}
}

No comments:

Post a Comment