/* Turn Signal This simple program simulates a car's turn signal using two LEDS and a double throw switch with a center off position. When the switch is thrown to the left, the LED embedded in the left side of an object will blink until the switch is returned to the center position. This code adapts the "Blink without delay" code, found under File>Sketchbook>Examples>Digital */ int ledPinL= 3; // Pin for Left LED int ledPinR = 2; // Pin for Right LED int inputPinL = 1; // Input pin for when switch is activated to the left int inputPinR = 0; // Input pin for when switch is activated to the right int valL = 0; // variable for reading the pin status int valR = 0; // variable for reading the pin status long timer = 0; // variable to store last time an LED was updated long interval = 300; // blinking interval (milliseconds) int onoff = LOW; void setup() { pinMode(ledPinL, OUTPUT); // declare Left LED as output pinMode(inputPinL, INPUT); // declare right side of switch as input //(directions are reversed here since flipping the switch // to the left connects the switch's center pin to the right pin pinMode(ledPinR, OUTPUT); // declare Right LED as output pinMode(inputPinR, INPUT); // declare left side of switch as input } void loop(){ valL = digitalRead(inputPinL); // read input value if (valL == HIGH) { // check if the input is HIGH digitalWrite(ledPinL, LOW); // Turn LED off } else{ if(millis() - timer > interval){ // check to see if LED should blink timer = millis(); if(onoff == LOW) // if LED is on, turn it off, and vice versa onoff = HIGH; else onoff = LOW; digitalWrite(ledPinL, onoff); } } valR = digitalRead(inputPinR); // read input value if (valR == HIGH) { // check if the input is HIGH digitalWrite(ledPinR, LOW); // Turn LED off } else{ if(millis() - timer > interval){ // check to see if LED should blink timer = millis(); if(onoff == LOW) // if LED is on, turn it off, and vice versa onoff = HIGH; else onoff = LOW; digitalWrite(ledPinR, onoff); } } }