Problem with "Blinkers" for bike trailer?

Hello,
I have made a trailer for my push bike and have added blinkers (2 LEDs on each side) as a little novelty thing and three switches, left, right, hazard. I have them connected to my arduino board like this:

LED1 LED2
| |--------------------| |
| \ |
| \ |
| \ |
| \ |
pin 13 GND Pin 12

| |
| |
| Arduino Board |
| |
| |
=======================
Pin 10 Pin 9 Pin 10 GND <-- Tried +5.5v as well
| | \
| | \
| | \
Switch 1 Switch 2 Switch 3
|-------------|-------------|-------|

The thing is no matter what switch I activate, the LEDs will just randomly blink. (at least at a second apart :stuck_out_tongue: )

So what I want to do is:

  1. flick switch one - left blinker flashes.
  2. flick switch two - right blinker flashes.
  3. flick switch three - both blinkers flashing.

What happens is:
Both LEDs randomly flash at 1 second intervals.

Thanks heaps!
Jabelone.

/ MY CURRENT CODE /

/*Jabelone's Bike Blinkers Sketch
The Circuit:
1) Left LED connected to ground and pin 12
2) Right LED connected to ground and pin 10
3) Left blinker switch connected to ground and pin 8
4) Right blinker switch connected to ground and pin 6
5) Hazard lights switch connected to pin 4

*/

//Blinker LED pins
int leftled = 13;  //left blinker
int rightled = 12; //right blinker

//Pins of Switches
int switcha = 9;  //left blinker
int switchb = 10;  //right blinker
int hazardswitch = 8; //hazard switch

//Status of the switch
int statusa = 0;  //left blinker
int statusb = 0;  //right blinker
int statushazard = 0; //hazard switch

void setup() {                

  pinMode(leftled, OUTPUT); 
  pinMode(rightled, OUTPUT);
  pinMode(switcha, INPUT);
  pinMode(switchb, INPUT);
  pinMode(hazardswitch, INPUT);  
}


void loop() {

  statusa = digitalRead(switcha);
  statusb = digitalRead(switchb);
  statushazard = digitalRead(hazardswitch);
  
  if (statusa == HIGH) leftblink();
  if (statusb == HIGH) rightblink();
  if (statushazard == HIGH) hazardfunction();
  
}

void leftblink() {
 digitalWrite(leftled, HIGH);
 delay(1000);
 digitalWrite(leftled, LOW);
 delay(1000);
}

void rightblink() {
 digitalWrite(rightled, HIGH);
 delay(1000);
 digitalWrite(rightled, LOW);
 delay(1000);
}

void hazardfunction() {
 digitalWrite(rightled, HIGH);
 digitalWrite(leftled, HIGH);
 delay(1000);
 digitalWrite(rightled, LOW);
 delay(1000);
}

Divide the problem up. Can you determine the position of each of the switches? Just print out the state of each switch, no need to try to do anything with LEDs at this point.

Get each LED to come on under your control. LEDs need to have a current limiting resister in series with them. Some LEDs have these built in and are advertised as suitable to run directly off 5V, 12V or whatever Voltage the resister is designed to suit. In my experience, most LEDs do not have this and need to be wired up with an external current limiting resister in series. If you try to run an LED without a current limiting resister directly off the Arduino, it'll draw as much current as the Arduino can supply and that is liable to overload the output circuit on the Arduino. So, read up on the circuit needed to power an LED and sort the hardware out for that, then write a sketch that does nothing but blink the LEDs under your control. You can use the Blink and Blink Without Delay example sketches as inspiration for that. (I recommend using the Blink Without Delay approach, but it would be possible to get something working using the cruder approach shown in the Blink sketch.)

Greetings,

I did not see anything in your Sketch to take care of switch bounce. A mechanical switch (actually, any switch) will likely create multiple signal changes due to the contacts vibrating slightly as they close.

There are several ways to fix this problem. Adding some simple code to read the switch twice, with a small delay, to check that the switch did in fact change and stay changed. The delay can be around 10ms to 50ms for most switches.

Google 'debounce switches' to learn more. Also this should help you: http://arduino.cc/en/Tutorial/Debounce

sorry, I forgot about the resistors in the circuit. Also thanks peter, but the LEDs are fine making them blink and stuff. I have also previously made an LED cube which is cool.

Resistors (R1 and R2) are 15 ohms.

LED1 LED2
| |--------------------| |
| \ |
R1 \ R2
| \ |
| \ |
pin 13 GND Pin 12

| |
| |
| Arduino Board |
| |
| |
=======================
Pin 10 Pin 9 Pin 10 GND
| | \
| | \
| | \
Switch 1 Switch 2 Switch 3
|-------------|-------------|-------|

I notice that your code doesn't start the pullup resistors on the switch pins. Do you have pullup resistors wired in? If not your inputs are probably floating which could result in random behaviour.

I'd also recommend using a SPDT switch with a center off position for the signal light. It won't change your sketch but it will put the signal lights on a single switch. Run the the center to ground and each throw to a pin.

thanks jimmy,
But I am not allowed to go to jaycar because its so far away and it takes to long from ebay aswel. I have thought about using a SPDT switch with a center off position. Also pull-up resistors are just normal resistors that pull the line up aren't they? Also wouldn't you need pull-down resistors because the code detects the switch and blinks when it is High?
thanks heaps,
Jabelone

jabelone:
Also pull-up resistors are just normal resistors that pull the line up aren't they? Also wouldn't you need pull-down resistors because the code detects the switch and blinks when it is High?
thanks heaps,
Jabelone

It looks as if the switches tie the inputs to ground. In that case you need to use pull-up resistors - your Arduino has ones that can be enabled by the software. The idea is that when the switch is open the resistors hold the input high; when the switch is closed it overrides the resistors and pulls the input low.

Whether you consider the 'high' or 'low' states to be active in terms of the behaviour of your sketch is a completely separate issue - you can code it either way round.

Ok thanks heaps!
So when I get home from school I should try my original code (but add a bit to enable internal pull-up resistors) and hook up the circuit like this:
Also I understand about the pull-up resistors now :slight_smile: .

Resistors (R1 and R2) are 15 ohms.

LED1 LED2
| |--------------------| |
| \ |
R1 \ R2
| \ |
| \ |
pin 13 GND Pin 12

| |
| Arduino Board |
| |
| Internal 20k Pull-up |
| Resistors |
=======================
Pin 10 Pin 9 Pin 10 GND
| | \
| | \
| | \
Switch 1 Switch 2 Switch 3
|-------------|-------------|-------|