Relay Boards Issue With Arduino

jaydenmiller

New member
Hello,

I am currently trying to use 4 separate 16 channel Sainsmart relay boards (currently only using 55 of the 64 channels) to control basic incandescent Christmas light strings. The sketch I'm using below is one I found on another thread on this forum. It was working great for a few days and all of a sudden none of the relays are working properly with Vixen/the sketch. When I first uploaded the sketch to the Arduino when it was working, all of the relays would fire on and then off sequentially, and then wouldn't come back on until I started a show in Vixen. Now, what happens is only one relay fires when uploading the sketch and nothing responds to anything on Vixen. Please let me know if there is something I am missing. Any help is greatly appreciated! (Also, I'm not sure what the best way to format code is so I just copied and pasted it)

// This code was written by Victor Perez for doityourselfchristmas.com based on the code from Zparticle, Si_champion and Neil Tapp.
// To adapt the code to your case, just change this top section, with the #define lines.
// how many channels will vixen be sending. Can be set to any number from 1 to 48 for Arduino Mega, and 1 to 18 for Arduino Uno.

#define CHANNEL_COUNT 55

// speed for the com port for talking with vixen. From 9600 to 115200. Use the same speed as set in Vixen.
#define VIXEN_COM_SPEED 57600

// Timeout waiting for serial input before going to random mode (in milliseconds).
#define TIME_OUT 2000

// If the relays turn On and Off opposite to Vixen sequence, change "#define MODE NOT_INVERTED" for "#define MODE INVERTED"
#define NOT_INVERTED 0
#define INVERTED 1
#define MODE NOT_INVERTED

// which pins control which channels
// You can change these assignment to use different pins, but be very careful to not repeat the same pin number for 2 channels.
#define CH01 2 // PWM Pin 2
#define CH02 3 // PWM Pin 3
#define CH03 4 // PWM Pin 4
#define CH04 5 // PWM Pin 5
#define CH05 6 // PWM Pin 6
#define CH06 7 // PWM Pin 7
#define CH07 8 // PWM Pin 8
#define CH08 9 // PWM Pin 9
#define CH09 10 // PWM Pin 10
#define CH10 11 // PWM Pin 11
#define CH11 12 // PWM Pin 12
#define CH12 13 // PWM Pin 13
// digital pin - connects to relay board
#define CH13 14 // DIGITAL Pin 14
#define CH14 15 // DIGITAL Pin 15
#define CH15 16 // DIGITAL Pin 16
#define CH16 17 // DIGITAL Pin 17
#define CH17 18 // DIGITAL Pin 18
#define CH18 19 // DIGITAL Pin 19
#define CH19 20 // DIGITAL Pin 20
#define CH20 21 // DIGITAL Pin 21
#define CH21 22 // DIGITAL Pin 22
#define CH22 23 // DIGITAL Pin 23
#define CH23 24 // DIGITAL Pin 24
#define CH24 25 // DIGITAL Pin 25
#define CH25 26 // DIGITAL Pin 26
#define CH26 27 // DIGITAL Pin 27
#define CH27 28 // DIGITAL Pin 28
#define CH28 29 // DIGITAL Pin 29
#define CH29 30 // DIGITAL Pin 30
#define CH30 31 // DIGITAL Pin 31
#define CH31 32 // DIGITAL Pin 32
#define CH32 33 // DIGITAL Pin 33
#define CH33 34 // DIGITAL Pin 34
#define CH34 35 // DIGITAL Pin 35
#define CH35 36 // DIGITAL Pin 36
#define CH36 37 // DIGITAL Pin 37
#define CH37 38 // DIGITAL Pin 38
#define CH38 39 // DIGITAL Pin 39
#define CH39 40 // DIGITAL Pin 40
#define CH40 41 // DIGITAL Pin 41
#define CH41 42 // DIGITAL Pin 42
#define CH42 43 // DIGITAL Pin 43
#define CH43 44 // DIGITAL Pin 44
#define CH44 45 // DIGITAL Pin 45
#define CH45 46 // DIGITAL Pin 46
#define CH46 47 // DIGITAL Pin 47
#define CH47 48 // DIGITAL Pin 48
#define CH48 49 // DIGITAL Pin 49
#define CH49 50 // DIGITAL Pin 50
#define CH50 51 // DIGITAL Pin 51
#define CH51 52 // DIGITAL Pin 52
#define CH52 53 // DIGITAL Pin 53
#define CH53 54 // DIGITAL Pin 54
#define CH54 55 // DIGITAL Pin 55
#define CH55 56 // DIGITAL Pin 56

int channels[] = {CH01,CH02,CH03,CH04,CH05,CH06,CH07,CH08,CH09,
CH10,CH11,CH12,CH13,CH14,CH15,CH16,CH17,CH18,CH19,CH20,CH21,CH22,
CH23,CH24,CH25,CH26,CH27,CH28,CH29,CH30,CH31,CH32,CH33,CH34,CH35,
CH36,CH37,CH38,CH39,CH40,CH41,CH42,CH43,CH44,CH45,CH46,CH47,CH48,
CH49,CH50,CH51,CH52,CH53,CH54};

int incomingByte[CHANNEL_COUNT];

int i = 0; // Loop counter

volatile unsigned long timer_a = 0; // new line

//setup the pins/ inputs & outputs
void setup() {

// specifically for the UNO
//sei();

// initalize PWM Channels / Pins
for (i = 0; i < CHANNEL_COUNT; i++) {
pinMode(channels, OUTPUT);
}

// set all the relays to off to start with
if (MODE == NOT_INVERTED) {
for (i = 0; i < CHANNEL_COUNT; i++) {
digitalWrite(channels, LOW);

}
}

else {
for (i = 0; i < CHANNEL_COUNT; i++) {
digitalWrite(channels, HIGH);
}
}

testSequence();

// set up Serial according to the speed defined above.
Serial.begin(VIXEN_COM_SPEED);
}

void loop() {
if (Serial.available() >= (CHANNEL_COUNT + 2)) {
timer_a = millis(); // new line
int uno = Serial.read();
if (uno == 126) {

int dos = Serial.read();
if (dos == 33) {

for (i = 0; i < CHANNEL_COUNT; i++) {
// read each byte
incomingByte = Serial.read();
}
if (MODE == NOT_INVERTED) {
for (i = 0; i < CHANNEL_COUNT; i++) {
int value = incomingByte;
if (value <= 127) {
digitalWrite(channels, LOW);
} else {
digitalWrite(channels, HIGH);
}
}
} else {
for (i = 0; i < CHANNEL_COUNT; i++) {
int value = incomingByte;
if (value <= 127) {
digitalWrite(channels, HIGH);
} else {
digitalWrite(channels, LOW);
}
}
}

}
}
}
// Random mode code. Random mode starts if no serial input has been received in TIME_OUT millisenconds
else {
//turn them all on
unsigned long diff = millis() - timer_a;
if (diff >= TIME_OUT) {
timer_a = millis ();
turnLightsOn();
}
}
}

void testSequence() {

if (MODE == INVERTED) {
for (i = 0; i < CHANNEL_COUNT; i++) {
digitalWrite(channels, HIGH);
delay(120);
digitalWrite(channels, LOW);

}
}

else {
for (i = 0; i < CHANNEL_COUNT; i++) {
digitalWrite(channels, LOW);
delay(500);
digitalWrite(channels, HIGH);
}
}
}

void turnLightsOn()
{
for (i = 0; i < CHANNEL_COUNT; i++) {
digitalWrite(channels, HIGH);
}
}
 
Actually, this is a good sketch for mechanical or "cheap SSR". It is using digital.write and has start coding.

This sketch uses 2 bytes for start coding. Did you set the two bytes in the Serial controller (Vixen side). The two bytes are ~!

The other thing to know about this sketch is that it splits your on/off at 127. So say you are doing a fade (which you really shouldn't with relays), you will get solid on while the value is 127 or greater and off on anything lower.

Another thing to know...at 57 bytes of data, you are just inside the arduino serial buffer (usually 64 bytes). I wouldn't wait for that much data if I was you. I would read for the UNO and DOS bytes and loop for 55 values in rapid succession.
 
I do have the two bytes in the serial controller in Vixen. I had them in there when it was working. Haven't changed a thing and suddenly not working.
 
Run a basic relay test. Make sure you are talking properly to all your relays. Take your existing sketch, rename it relay55test, take out everything in Loop and just put turnLightsOn(); followed by TurnLightsOff();

I would probably put a delay(500) in the turnLightsOn loop so you can follow which are working and which aren't.

Which type of relay are you using?
 
Make sure you have sufficient power to each of your boards. That is a problem I had. If the controller boards don't have enough power they are very erratic.
 
Back
Top