Emergency-light controller

Hi,

As I´m progressing and continuing my endavours with Arduino, I recently had a problem with an project. I have written some (extremely easy) code to control relays turning ON/OFF LED blue lights. The usage is for a certain recreational hobby. Before we connected everything to the car, I tested it with single LED diodes on each output and everything worked fine. After the hookup, however, something is very off. Sometimes the lights won´t turn on and there doesn´t look like there is a pattern in the behaviour. When I first soldered up the relays, I glued them together 4 and 4. I thought maybe that had an effect, but after releasing them from another nothing new happended. This leeds me to believe one thing, and I´m pretty certain it is this; There are too many delays in the code!

void setup() 
{                
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);  
}

void loop() 
{
  digitalWrite(7, HIGH);
  delay(34);
  digitalWrite(7, LOW);
  delay(5);
  digitalWrite(12, HIGH);
  delay(45);  
  digitalWrite(12, LOW);
  delay(5);
  digitalWrite(5, HIGH);
  delay(37);
  digitalWrite(5, LOW);
  delay(5);
  digitalWrite(10, HIGH);       
  delay(29);
  digitalWrite(10, LOW);
  delay(5);
  digitalWrite(4, HIGH);       
  delay(29);
  digitalWrite(4, LOW);
  delay(5);
  digitalWrite(13, HIGH);
  delay(19);
  digitalWrite(13, LOW);
  delay(5);
  digitalWrite(6, HIGH);
  delay(33);
  digitalWrite(6, LOW);
  delay(5);
  digitalWrite(11, HIGH);
  delay(22);
  digitalWrite(11, LOW);
  delay(5);
  digitalWrite(7, HIGH);
  delay(55);
  digitalWrite(7, LOW);
  delay(5);
  digitalWrite(12, HIGH);
  delay(19);  
  digitalWrite(12, LOW);
  delay(5);
  digitalWrite(5, HIGH);
  delay(12);
  digitalWrite(5, LOW);
  delay(5);
  digitalWrite(10, HIGH);       
  delay(29);
  digitalWrite(10, LOW);
  delay(5);
  digitalWrite(4, HIGH);       
  delay(29);
  digitalWrite(4, LOW);
  delay(5);
  digitalWrite(13, HIGH);
  delay(45);
  digitalWrite(13, LOW);
  delay(5);
  digitalWrite(6, HIGH);
  delay(33);
  digitalWrite(6, LOW);
  delay(5);
  digitalWrite(11, HIGH);
  delay(22);
  digitalWrite(11, LOW);
  delay(5);
  digitalWrite(7, HIGH);
  delay(15);
  digitalWrite(7, LOW);
  delay(5);
  digitalWrite(12, HIGH);
  delay(19);  
  digitalWrite(12, LOW);
  delay(5);
  digitalWrite(5, HIGH);
  delay(34);
  digitalWrite(5, LOW);
  delay(5);
  digitalWrite(10, HIGH);       
  delay(29);
  digitalWrite(10, LOW);
  delay(5);
  digitalWrite(4, HIGH);       
  delay(29);
  digitalWrite(4, LOW);
  delay(5);
  digitalWrite(13, HIGH);
  delay(9);
  digitalWrite(13, LOW);
  delay(30);
  digitalWrite(6, HIGH);
  delay(33);
  digitalWrite(6, LOW);
  delay(5);
  digitalWrite(11, HIGH);
  delay(22);
  digitalWrite(11, LOW);
  delay(5);
  digitalWrite(7, HIGH);
  delay(15);
  digitalWrite(7, LOW);
  delay(5);
  digitalWrite(12, HIGH);
  delay(19);  
  digitalWrite(12, LOW);
  delay(5);
  digitalWrite(5, HIGH);
  delay(12);
  digitalWrite(5, LOW);
  delay(5);
  digitalWrite(10, HIGH);       
  delay(29);
  digitalWrite(10, LOW);
  delay(5);
  digitalWrite(4, HIGH);       
  delay(29);
  digitalWrite(4, LOW);
  delay(5);
  digitalWrite(13, HIGH);
  delay(9);
  digitalWrite(13, LOW);
  delay(5);
  digitalWrite(6, HIGH);
  delay(33);
  digitalWrite(6, LOW);
  delay(5);
  digitalWrite(11, HIGH);
  delay(22);
  digitalWrite(11, LOW);
  delay(5);
  digitalWrite(7, HIGH);
  delay(15);
  digitalWrite(7, LOW);
  delay(5);
  digitalWrite(12, HIGH);
  delay(19);  
  digitalWrite(12, LOW);
  delay(5);
  digitalWrite(5, HIGH);
  delay(12);
  digitalWrite(5, LOW);
  delay(5);
  digitalWrite(10, HIGH);       
  delay(29);
  digitalWrite(10, LOW);
  delay(5);
  digitalWrite(4, HIGH);       
  delay(29);
  digitalWrite(4, LOW);
  delay(5);
  digitalWrite(13, HIGH);
  delay(9);
  digitalWrite(13, LOW);
  delay(5);
  digitalWrite(6, HIGH);
  delay(33);
  digitalWrite(6, LOW);
  delay(5);
  digitalWrite(11, HIGH);
  delay(22);
  digitalWrite(11, LOW);
  delay(5);
  digitalWrite(7, HIGH);
  delay(15);
  digitalWrite(7, LOW);
  delay(5);
  digitalWrite(12, HIGH);
  delay(19);  
  digitalWrite(12, LOW);
  delay(5);
  digitalWrite(5, HIGH);
  delay(12);
  digitalWrite(5, LOW);
  delay(5);
  digitalWrite(10, HIGH);       
  delay(29);
  digitalWrite(10, LOW);
  delay(5);
  digitalWrite(4, HIGH);       
  delay(29);
  digitalWrite(4, LOW);
  delay(5);
  digitalWrite(13, HIGH);
  delay(9);
  digitalWrite(13, LOW);
  delay(5);
  digitalWrite(6, HIGH);
  delay(33);
  digitalWrite(6, LOW);
  delay(5);
  digitalWrite(11, HIGH);
  delay(22);
  digitalWrite(11, LOW);
  delay(5);
  digitalWrite(7, HIGH);
  delay(15);
  digitalWrite(7, LOW);
  delay(5);
  digitalWrite(12, HIGH);
  delay(19);  
  digitalWrite(12, LOW);
  delay(5);
  digitalWrite(5, HIGH);
  delay(12);
  digitalWrite(5, LOW);
  delay(5);
  digitalWrite(10, HIGH);       
  delay(29);
  digitalWrite(10, LOW);
  delay(5);
  digitalWrite(4, HIGH);       
  delay(29);
  digitalWrite(4, LOW);
  delay(5);
  digitalWrite(13, HIGH);
  delay(9);
  digitalWrite(13, LOW);
  delay(5);
  digitalWrite(6, HIGH);
  delay(33);
  digitalWrite(6, LOW);
  delay(5);
  digitalWrite(11, HIGH);
  delay(22);
  digitalWrite(11, LOW);
  delay(7);
  digitalWrite(13, LOW);
  digitalWrite(12, LOW);
  digitalWrite(11, LOW);
  digitalWrite(10, LOW);
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);
  digitalWrite(5, LOW);
  digitalWrite(4, LOW);
  delay(55);
  //starts right side
  digitalWrite(13, HIGH);
  digitalWrite(12, HIGH);
  digitalWrite(11, HIGH);
  digitalWrite(10, HIGH);
  delay(55);
  digitalWrite(13, LOW);
  digitalWrite(12, LOW);
  digitalWrite(11, LOW);
  digitalWrite(10, LOW);
  delay(55);
  digitalWrite(13, HIGH);
  digitalWrite(12, HIGH);
  digitalWrite(11, HIGH);
  digitalWrite(10, HIGH);
  delay(55);
  digitalWrite(13, LOW);
  digitalWrite(12, LOW);
  digitalWrite(11, LOW);
  digitalWrite(10, LOW);
  delay(55);
  digitalWrite(13, HIGH);
  digitalWrite(12, HIGH);
  digitalWrite(11, HIGH);
  digitalWrite(10, HIGH);
  delay(55);
  digitalWrite(13, LOW);
  digitalWrite(12, LOW);
  digitalWrite(11, LOW);
  digitalWrite(10, LOW);
  delay(55);  
  // Starts left side
  digitalWrite(7, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(4, HIGH);
  delay(55);
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);
  digitalWrite(5, LOW);
  digitalWrite(4, LOW);
  delay(55);
  digitalWrite(7, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(4, HIGH);
  delay(55);
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);
  digitalWrite(5, LOW);
  digitalWrite(4, LOW);
  delay(55);
  digitalWrite(7, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(4, HIGH);
  delay(55);
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);
  digitalWrite(5, LOW);
  digitalWrite(4, LOW);
  delay(55);
  //ends left side, starts right side
  digitalWrite(13, HIGH);
  digitalWrite(12, HIGH);
  digitalWrite(11, HIGH);
  digitalWrite(10, HIGH);
  delay(55);
  digitalWrite(13, LOW);
  digitalWrite(12, LOW);
  digitalWrite(11, LOW);
  digitalWrite(10, LOW);
  delay(55);
  digitalWrite(13, HIGH);
  digitalWrite(12, HIGH);
  digitalWrite(11, HIGH);
  digitalWrite(10, HIGH);
  delay(55);
  digitalWrite(13, LOW);
  digitalWrite(12, LOW);
  digitalWrite(11, LOW);
  digitalWrite(10, LOW);
  delay(55);
  digitalWrite(13, HIGH);
  digitalWrite(12, HIGH);
  digitalWrite(11, HIGH);
  digitalWrite(10, HIGH);
  delay(55);
  digitalWrite(13, LOW);
  digitalWrite(12, LOW);
  digitalWrite(11, LOW);
  digitalWrite(10, LOW);
  delay(55);
  //ends right side, starts left side
  digitalWrite(7, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(4, HIGH);
  delay(55);
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);
  digitalWrite(5, LOW);
  digitalWrite(4, LOW);
  delay(55);
  digitalWrite(7, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(4, HIGH);
  delay(55);
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);
  digitalWrite(5, LOW);
  digitalWrite(4, LOW);
  delay(55);
  digitalWrite(7, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(4, HIGH);
  delay(55);
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);
  digitalWrite(5, LOW);
  digitalWrite(4, LOW);
  delay(55);  
  //starts right side
  digitalWrite(13, HIGH);
  digitalWrite(12, HIGH);
  digitalWrite(11, HIGH);
  digitalWrite(10, HIGH);
  delay(55);
  digitalWrite(13, LOW);
  digitalWrite(12, LOW);
  digitalWrite(11, LOW);
  digitalWrite(10, LOW);
  delay(55);
  digitalWrite(13, HIGH);
  digitalWrite(12, HIGH);
  digitalWrite(11, HIGH);
  digitalWrite(10, HIGH);
  delay(55);
  digitalWrite(13, LOW);
  digitalWrite(12, LOW);
  digitalWrite(11, LOW);
  digitalWrite(10, LOW);
  delay(55);
  digitalWrite(13, HIGH);
  digitalWrite(12, HIGH);
  digitalWrite(11, HIGH);
  digitalWrite(10, HIGH);
  delay(55);
  digitalWrite(13, LOW);
  digitalWrite(12, LOW);
  digitalWrite(11, LOW);
  digitalWrite(10, LOW);
  delay(55);  
  // Starts left side
  digitalWrite(7, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(4, HIGH);
  delay(55);
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);
  digitalWrite(5, LOW);
  digitalWrite(4, LOW);
  delay(55);
  digitalWrite(7, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(4, HIGH);
  delay(55);
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);
  digitalWrite(5, LOW);
  digitalWrite(4, LOW);
  delay(55);
  digitalWrite(7, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(4, HIGH);
  delay(55);
  digitalWrite(7, LOW);
  digitalWrite(6, LOW);
  digitalWrite(5, LOW);
  digitalWrite(4, LOW);
  delay(55);
}

Cars are an electrically noisy environment - you may be experiencing spikes & interference. Another possibility is inadequate power. Does your device work when powered by something other than the car?

  digitalWrite(7, LOW);
  digitalWrite(6, LOW);
  digitalWrite(5, LOW);
  digitalWrite(4, LOW);
  delay(55);

That block of code appears more than once. Time for a function.

Naming the pins - L1, L2, L3, L4 and R1, R2, R3, R4 - would make it easier to keep the operations straight. Using arrays (L[4] and R[4]) would be even better.

The usage is for a certain recreational hobby.

Likely illegal in some areas.

After the hookup, however, something is very off. Sometimes the lights won´t turn on and there doesn´t look like there is a pattern in the behaviour. When I first soldered up the relays, I glued them together 4 and 4. I thought maybe that had an effect, but after releasing them from another nothing new happended. This leeds me to believe one thing, and I´m pretty certain it is this; There are too many delays in the code!

Not likely. You have the same number of delays as then the hardware worked, don't you?

Cars are an electrically noisy environment - you may be experiencing spikes & interference. Another possibility is inadequate power. Does your device work when powered by something other than the car?

It failed tremendesly when powering with USB from the computer. I have 12V in on the DC port. This can cause problems ? I thought it was rated between 9-16V ?

Likely illegal in some areas.

Yes, in my country too. But, there is NO way to execute this from inside the car without getting out, opening the hood, activating power to the switch inside the car and then operating it from inside the car. It´s not like I WAN´T to loose my licence. Our site where we have games is also far from citizens.

Not likely. You have the same number of delays as then the hardware worked, don't you?

Yes, that is correct. I´m using solenoid relays, btw.

How is it wired up? I would not expect it to work if you were trying to power multiple solenoids from the USB port.

I´ll draw a schematic. One moment, please.

Here.

There are, ofcourse, 8 relays in total. They are called SPDT.

So it sounds like the Arduino can't supply the power your relays need. Take a look at the relay example circuits in the playgound here http://arduino.cc/playground/Main/InterfacingWithHardware#Output. Notice that you usually need a transistor of some sort to provide enough current to switch the relay.

It's fairly unusual to find relays that will operate with the 20-or-so mA you should be drawing from an Arduino pin. Are those resistor or diodes across the relay coils?

I think its resistors.

So your suggestion it to output to transistors, that then outputs to the relays ? How do you go about getting enough mA to the relays then ?

You can use a simple npn transistor On your relay wire one side of the coil to the 12v+, the other side of the coil to the collector of the transistor. The emittter goes to ground, and the base goes to the arduino output pin through a resistor, between 500 and 1000 ohms works for me usually

You just need to size the transistor appropriately and it will switch sufficient current. What solenoids (model) are you using? Just note that the power doesn't come from the Arduino pins - you need an external supply, even if it's the supply to the arduino, which in this case is apparently the car's 12V. Are the solenoid coils rated for this?

It´s now I wish I was paying attention to what my electronics teacher was talking about ;)

http://www.sparkfun.com/products/100

That´s the SPDT relay I´m using.

That's a 12v relay, so use the solution given by winner10920, but add a diode in parallel with the relay coil (cathode to +12v, anode to collector of the transistor). Just about any small NPN transistor and a 1K base resistor should do.

Ok, thank you. I´ll draw a schematic and post it here, to prohibit errors on my part ;)

So, does this seem about right ? I don´t understand completely how the NPN works, because I thought it had to have a common GND with the Arduino and the car.

Yes, you need to connect Arduino ground to the car ground (i.e. negative side of the 12v battery) as well. If you're running your arduino from the 12v car supply, you must be doing this already.

Don't forget the diode across the relay coil.

Yeah, It´s all connected to the car battery. Then i have a straight-forward way of doing things! Thank you all so much, love this community!

I have some 2N3904 (NPN´s) and some 1N4148 diode´s on hand, so I´ll just use that.

I have also done a a test run on the Arduino and the clicking from the SPDT seems much "firmer" now, so I´m quite confident :)

This is actually working and turning out awesome. Thanks a bunch!