Flasher Light Circuit Version 1 Completed

Electronics, Trains

WP_20140209_12_24_54_Pro

I got my light circuit completed, it’s fairly simple and doesn’t do much other than host the appropriate resistors and transistors. It get’s four lines from the Arduino: 5V, GND, and Digital Pins 12 & 13. The flasher lights are common cathode so the 5V goes straight to the lights. To control the flashing I use the transistors, when I apply power to the transistor via one of the digital pins which connects the ground on the lights. In essence the transistor is acting as a solid state relay.

After this I started working on activating the lights, I’m looking to do a couple of options for redundancy. It seems that the time things like this don’t work is when you are at a show or showing somebody so I want to start with a push button that can be used to toggle it on and off.

WP_20140209_13_21_52_Pro

It’s basically just a simple normally open push button hooked up to Digital Pin 2, the only complicated bit is the debounce to prevent weird behavior. I basically took the Arduino Debounce tutorial (http://arduino.cc/en/Tutorial/Debounce) for this. Next up I’m going to work on using a couple of light sensors as another way to activate them.

Current state of the code:

const int ledRight = 13;
const int ledLeft = 12;
const int btnOverride = 2;

int ledState = LOW;
long previousMillis = 0;
int flasherState = LOW;

long interval = 500;

int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

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

void setup()
{
  pinMode(ledRight, OUTPUT);
  pinMode(ledLeft, OUTPUT);
  pinMode(btnOverride, INPUT);
}

void loop()
{
  // read the state of the switch into a local variable:
  int reading = digitalRead(btnOverride);

  // check to see if you just pressed the button 
  // (i.e. the input went from LOW to HIGH),  and you've waited 
  // long enough since the last press to ignore any noise:  

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  } 

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        flasherState = !flasherState;
      }
    }
  }

  if(flasherState ==  HIGH){
    unsigned long currentMillis = millis();

    if(currentMillis - previousMillis > interval)
    {
      previousMillis = currentMillis;

      ledState = !ledState;
      digitalWrite(ledRight, ledState);
      digitalWrite(ledLeft, !ledState);
    }
  }
  else
  {    
      digitalWrite(ledRight, LOW);
      digitalWrite(ledLeft, LOW);
  }

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;
}
Advertisements

2 thoughts on “Flasher Light Circuit Version 1 Completed

  1. True story, I spent at least 5 minutes staring at the email notification for your comment trying to figure out what “morning after guy” meant… it turns out I picked out this theme without looking at it’s name 🙂

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