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:
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.)
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.
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.
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 .