Arduino Mega use Serial 1 to control device

Hi,

I am recently working on a summer project in controlling a serial device.
It uses a Hex command, CC01, to turn it on and off.
Which means if I use a serial tester to send a hex command "CC01" then it is on.
If I send again it will be off.

I write the following, but couldn't get it on but only off.

void setup() {
// initialize serial ports:
Serial1.begin(38400);
}

void loop() {
byte lightonoff = {1100, 1100, 0000, 0001};
Serial1.write(lightonoff, sizeof(lightonoff));
delay(10000);
}

WendyLee:
Thank you so much in advance. :slight_smile:

are you absolutely sure that "1100 1100 0000 0001" is used to toggle off and ON?

Hi Lowell,

Thanks.
I have converted it from Hex CC01 to Binary.
It reads that way.

Wen

sorry... you are telling us that the binary transmission of "1100 1100 0000 0001" will work to change the lamp's state from ON to OFF, but the next time it gets transmitted (10seconds later) it will not change the state of the lamp from OFF to ON, yes?

Yes, the binary transmission of "1100 1100 0000 0001" will work to change the lamp's state from ON to OFF.
It's weird.

that is weird.

you may want to double check that "CC01" is the right code to toggle lamp on.

What is on the other end of the transmission?

Hi WendyLee

Try prefixing the constant values with "0b" (zero b) to make them binary.

For example 0b1100.

Regards

Ray

Hi Ray,

Thank you. :slight_smile:
Like this?
void loop() {
byte lightonoff = {0b, 1100, 1100, 0000, 0001};
Serial1.write(lightonoff, sizeof(lightonoff));
delay(10000);
}

{0b1100, 0b1100, 0b0000, 0b0001}

good eye Ray

Hi Ray,

I tried but can't. I will try more combination first.
Thank you anyway.

Wen

No, 0b on the front of each value ...

0b1100, 0b1100, 0b0000, 0b0001

Not essential for the last two, since they are the same in binary and decimal.

You could also do it in hex ...

0xC, 0xC, 0x0, 0x1

Oh, I miss the last post.
I try it now. :slight_smile:

Ray, Hackscribble, BDLowell,

ALL work!! :stuck_out_tongue:
But it now works the opposite way.
If it is turned off, it will light up.
But it can't turn off again after 10 sec.
If I turn it on at the beginning, then it can't be switched off.

void loop() {
// read from port 1, send to port 0:
byte lighton = {0b1100, 0b1100, 0000, 0001};
OR
byte lighton = {0xC, 0xC, 0x0, 0x01};
Serial1.write(lighton, sizeof(lighton));
delay(10000);

Wen

you made some other changes to your initial sketch?

// read from port 1, send to port 0:

maybe post the code you are using right now.

Can you confirm that, when sent from your tester, CC01 turns the lamp on and then off OK?

What speed, data bits and parity but settings do you have on the tester?

This line is deleted. Sorry.

Hi,

I test the light with the following codelines.

void setup() {
// initialize serial ports:
Serial1.begin(38400);
}

void loop() {
byte lighton = {0b1100, 0b1100, 0000, 0001};
Serial1.write(lighton, sizeof(lighton));
}

If it is off, then it will turn on a second and then goes off.
If it is on, then it will turn off.

BaudRate 38400
DataBit 8
StopBit 1
Verify None
FlowControl None
Send Hex Yes

WendyLee:
Hi,

I test the light with the following code lines.

So, you lost your delay. Right now you are sending thousands and thousands of commands per second.

put the delay back in and see what happens...

void setup() 
{
  // initialize serial ports:
  Serial1.begin(38400);
}

void loop() 
{
  byte lighton[] = { 0b1100, 0b1100, 0b0000, 0b0001 };
  Serial1.write(lighton, sizeof(lighton));
  delay(10000);
}

Hi,

I use the software, SSCOM, on my computer to test the light first before using Arduino Mega to control.
It works that way but not Mega.
@@

Wen