Please help with co...
 
Notifications
Clear all

Please help with code

11 Posts
4 Users
0 Reactions
4,385 Views
Posts: 11
Topic starter
(@jdolish)
Active Member
Joined: 12 years ago
[#34853]

Need your help with this code - All my relays are on until I start running the sequence and find they go off when they should be on. I have been trying to figure it out for the last 4 hours to no avail. Can you take a look at this code and let me know what wrong??
I am using the Arduino Mega with 4 - 8 Channel SSR relays (32 Channels) & Vixen 3

Thanks in advance!

// which pins control which channels
#define CHANNEL01 22
#define CHANNEL02 23
#define CHANNEL03 24
#define CHANNEL04 25
#define CHANNEL05 26
#define CHANNEL06 27
#define CHANNEL07 28
#define CHANNEL08 29
#define CHANNEL09 30
#define CHANNEL10 31
#define CHANNEL11 32
#define CHANNEL12 33
#define CHANNEL13 34
#define CHANNEL14 35
#define CHANNEL15 36
#define CHANNEL16 37
#define CHANNEL17 38
#define CHANNEL18 39
#define CHANNEL19 40
#define CHANNEL20 41
#define CHANNEL21 42
#define CHANNEL22 43
#define CHANNEL23 44
#define CHANNEL24 45
#define CHANNEL25 46
#define CHANNEL26 47
#define CHANNEL27 48
#define CHANNEL28 49
#define CHANNEL29 50
#define CHANNEL30 51
#define CHANNEL31 52
#define CHANNEL32 53

int channels[] = {CHANNEL01,CHANNEL02,CHANNEL03,CHANNEL04,CHANNEL05,CHANNEL06,CHANNEL07,CHANNEL08,CHANNEL09,
CHANNEL10,CHANNEL11,CHANNEL12,CHANNEL13,CHANNEL14,CHANNEL15,CHANNEL16,CHANNEL17,CHANNEL18,CHANNEL19,CHANNEL20
,CHANNEL21,CHANNEL22,CHANNEL23,CHANNEL24,CHANNEL25,CHANNEL26,CHANNEL27,CHANNEL28,CHANNEL29,CHANNEL30
,CHANNEL31,CHANNEL32};

// how many channel will vixen be sending
#define CHANNEL_COUNT 32

// speed for the com port for talking with vixen
#define VIXEN_COM_SPEED 57600

void setup()
{
Serial.begin(VIXEN_COM_SPEED);

// set the channel pins to output mode
for(int channelIndex=0;channelIndex<CHANNEL_COUNT;channelIndex++){
pinMode(channels[channelIndex],OUTPUT);
}

turnLightsOff();
powerOnSelfTest();
}

// !!!! note the PWM values that need to be sent to the relay board are reversed from the
// values comming in from Vixen. Vixen 0-255 (off-on), Relays 255-0 (off-on)
void loop()
{
readFromVixen();
}

void powerOnSelfTest()
{
for(int channelIndex=0;channelIndex<CHANNEL_COUNT;channelIndex++){
analogWrite(channels[channelIndex], LOW); // turn on one channel at a time
delay(500); // wait .5 seconds
analogWrite(channels[channelIndex], HIGH);
}
turnLightsOff();
}

void turnLightsOff()
{
//turn them all off
for(int channelIndex=0;channelIndex<CHANNEL_COUNT;channelIndex++)
analogWrite(channels[channelIndex], HIGH);
}

void outputToLights(unsigned char* buffer)
{
for(int channelIndex=0;channelIndex<CHANNEL_COUNT;channelIndex++)
analogWrite(channels[channelIndex], buffer[channelIndex]);
}

void readFromVixen()
{
char *footer="VIXEN_END";
unsigned char buffer[CHANNEL_COUNT];
char buffer2[CHANNEL_COUNT];
int index=0;
unsigned long time = millis();

waitForVixenHeader();
while (true) {
int inByte = Serial.read();
if(inByte==-1){
if(index==0 && millis()-time>1000) // we haven't read anything in a second
return;
continue;
}
time = millis();
int lightByte = map(inByte, 0, 255, 255, 0);
buffer[index] = lightByte;
buffer2[index] = inByte;
buffer[index+1] = 0;
buffer2[index+1] = 0;
index++;
if(index==9 && strcmp(footer,buffer2)==0){
return;
}
else if(index==CHANNEL_COUNT){
outputToLights(buffer);
index=0;
}
}
Serial.println("");
}

void waitForVixenHeader()
{
char *header="VIXEN_START";
char buffer[12];
int index = 0;
unsigned long time = millis();

while (true) {
int inByte = Serial.read();
if(inByte==-1){
if(index==0 && millis()-time>1000) // we haven't read anything in a second
return;
continue;
}
time = millis();
buffer[index] = inByte;
if(buffer[index]!=header[index]) {// not the right sequence restart
index=-1;
}
buffer[index+1] = 0; // add null
index++;
if(index==11 && strcmp(header,buffer)==0){
return;
}
}
}


10 Replies
Posts: 164
(@victor-pv)
Estimable Member
Joined: 12 years ago

You can try changing this line:
analogWrite(channels[channelIndex], buffer[channelIndex]);

For this:
analogWrite(channels[channelIndex], ~buffer[channelIndex]);

You can also go to a thread called "Simple code for Arduino... blah blah" that I started and has code based on what was being used over here, but simplified and with comments and options at the beginning, so hopefully it is easier to use for people with little knowledge about programming.
You set your channels numbers, the total of channels, and change one line to reverse the relay outputs like you need here. The advantage is that those things are done with #define lines, without having to touch the actual loops much.


Reply
Posts: 11
Topic starter
(@jdolish)
Active Member
Joined: 12 years ago

Tried the above line did not work - Went to the Simple Code thread and input the info to the arduino (set the channel #'s, total channels and changed the line to reverse the relay outputs). The board performs the self test and then goes into random mode but as soon as I open Vixen all relays freeze and you can not play the sequence. Any ideas?


Reply
Posts: 2541
(@lightup)
Famed Member
Joined: 17 years ago
jdolish wrote:Tried the above line did not work - Went to the Simple Code thread and input the info to the arduino (set the channel #'s, total channels and changed the line to reverse the relay outputs). The board performs the self test and then goes into random mode but as soon as I open Vixen all relays freeze and you can not play the sequence. Any ideas?

The code you posted works on Vixen2 only because it uses too long a footer for vixen3. Maybe victor_pv already has code that works on Vixen3.
This issue has come up many times for beginners. Perhaps we could "standardize" on a general sketch that has a shorter footer so that it will work with both Vixen2 and Vixen3.

Some sketches here won't work on the UNO because of using a 2nd serial port, which the MEGA does support.


Reply
Posts: 164
(@victor-pv)
Estimable Member
Joined: 12 years ago
LightUp wrote:The code you posted works on Vixen2 only because it uses too long a footer for vixen3. Maybe victor_pv already has code that works on Vixen3.
This issue has come up many times for beginners. Perhaps we could "standardize" on a general sketch that has a shorter footer so that it will work with both Vixen2 and Vixen3.

Some sketches here won't work on the UNO because of using a 2nd serial port, which the MEGA does support.

In vixen did you set the header to ~! and no footer?
Besides that, make sure the serial port settings match (57600, 8 bits, no parity 1 stop bit). Also check that serial port setting in the Windows Device Manager, and make sure it is set the same way.


Reply
Posts: 2541
(@lightup)
Famed Member
Joined: 17 years ago
victor_pv wrote:In vixen did you set the header to ~! and no footer?
Besides that, make sure the serial port settings match (57600, 8 bits, no parity 1 stop bit). Also check that serial port setting in the Windows Device Manager, and make sure it is set the same way.

Hi, victor_pv. You're replying to me, while I was replying to jdolish. 🙂


Reply
Posts: 164
(@victor-pv)
Estimable Member
Joined: 12 years ago

I meant to reply to him 😉


Reply
Posts: 11
Topic starter
(@jdolish)
Active Member
Joined: 12 years ago
victor_pv wrote:In vixen did you set the header to ~! and no footer?
Besides that, make sure the serial port settings match (57600, 8 bits, no parity 1 stop bit). Also check that serial port setting in the Windows Device Manager, and make sure it is set the same way.

Thank you!
set the header and got rid of the footer - all work great....

Merry Christmas everyone!
(and my home controller are the SSR relays....;)


Reply
Posts: 2541
(@lightup)
Famed Member
Joined: 17 years ago
jdolish wrote:Thank you!
set the header and got rid of the footer - all work great....

Merry Christmas everyone!
(and my home controller are the SSR relays....;)

Good to hear that. Have a great Christmas.....


Reply
IaRoadKill
Posts: 586
(@iaroadkill)
Honorable Member
Joined: 12 years ago

I'm having the same problem as you were. Did you use your script and fix it, or did you use the simple script?


Reply
Page 1 / 2
Share: