Blinking Beanie

Here, I put LED into the back of my beanie and made it blink whenever I pressed on a button that placed in the side of the beanie. The rate LED blinks increase and decreases as the button is pressed longer.

Code

int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for a pushbutton)
int val = 0;                    // variable for reading the pin status
long randNumber;
int blinkDuration;
int decrease;
void setup() {
pinMode(ledPin, OUTPUT);      // declare LED as output
pinMode(inputPin, INPUT);     // declare pushbutton as input
blinkDuration = 1000;         // default blinking duration
decrease = 1;                 // indicator where 1 = blinking rate will decrease and 0 = blinking rate will increase
}

void loop(){
val = digitalRead(inputPin);  // read input value

if (val == HIGH) {            // check if the input is HIGH
digitalWrite(ledPin, LOW);  // turn LED OFF
} else {
if (decrease == 1) { //Decrease the blinking rate
if (blinkDuration <= 100) { //Next time loop comes around, increase blinking rate.
decrease = 0;
blinkDuration = 100;
}
else {
blinkDuration = blinkDuration – 50;
}
}
else { //Increase the blinking rate
if (blinkDuration >= 1000) { //Next time loop comes around, decrease blinking rate.
decrease = 1;
blinkDuration = 1000;
}
else {
blinkDuration = blinkDuration + 50;
}
}

digitalWrite(ledPin, HIGH);   // sets the LED on
delay(blinkDuration);                  // waits for a second

digitalWrite(ledPin, LOW);    // sets the LED off
delay(200);        // waits for a second

}
}

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