Red Sox victory hat

I updated a hat to blink a red LED when the wearer presses the “B,” and to turn it off when the “B” is pressed again.  This could used by a fan to display when his or her favorite team wins, which, since it’s the Red Sox, means the hat would be blinking a lot.

Code:

int inPin = 2;         // the number of the input pin
int ledPin = 13;       // the number of the output pin

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long’s because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
pinMode(inPin, INPUT);
pinMode(ledPin, OUTPUT);
}

void loop()
{
reading = digitalRead(inPin);

// if the input just went from LOW and HIGH and we’ve waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() – time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;

time = millis();
}

digitalWrite(ledPin, state);  //Light or turn off LED based on state
delay(100);
digitalWrite(ledPin, LOW);    //turn LED off, causes blinking of state=HIGH
delay(100);

previous = reading;          //update previous to current reading
}

Advertisements

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