I need to ask a question to see if anyone knows what is going on with my Arduino Mega 2560.
I have 1 byte of Serial HEX data (it changes between these 2 HEX values 0xFE and 0xFF continuously. It gets these values out my other microprocessor connected to Arduino on Serial1. The data is coming in on Serial1 running at 9600 bps and after processing the data I am sending it back out to Serial2 9600 bps.
** Serial1 running at 9600 bps cannot change as external microproceesor only talks at 9600 baud**.
What I am trying to get to happen as efficient as possible in the code is:
(FYI, the Serial1 data runs continuously)
change byte Serial1 0xFF to 0x01 .
change byte Serial1 0xFE to 0x00.
3 output result in Serial2.write (0x00 or 0x01).
byte microData[0]; // Incoming Serial1 byte to store
byte tempbyteData; // byte storage to make equal to microData byte
byte newbyteData; // new converted byte to store
void setup()
{
Serial1.begin(9600);
Serial2.begin(9600); // *tried changing this to 20% (19200 bps) more than Serial 1 but gave ALL erroneous data*
}
void loop()
{
if (Serial1.available())
{
for (int i=0, i<1; i++)
{
while (Serial1.available() == 0)
{
// do nothing
}
microData[i] = Serial1.read();
}
for (int i=0, i<1, i++)
{
Serial1.print(" microData that is coming in on Serial1 is: ");
Serial1.print(microData[0], HEX); // DEBUG :print microData coming in
tempbyteData = microData[0]; // assign tempbyteData byte to microData byte
newbyteData = map(tempbyteData, 254, 255, 0, 1); // if tembyteData is equal to 254 change to 00, if tempbyteData is
// equal to 255 change to 01. Then assign to newbyteData.
Serial1.print(" tempbyteData is: ");
Serial1.println(tempbyteData, HEX); //DEBUG: verify tempbyteData is equal to microData coming in
Serial1.print(" newbyteData is: "); // DEBUG: verify what the new value of newbyteData is
Serial1.println(newbyteData, HEX);
Serial2.write(newbyteData); // send newbyteData out Serial2 as a hex byte
}
}
}
I get this out on Serial2 (shown to a smaller degree, as there is a lot of data constantly coming in at 9600 bps. Serial1 shows the same data going out on Realterm):
01 01 01 01 00 00 00 00 00 << erroneous bytes >> <------ *** starts off right but then it get crazy
Any ideas on a better method to change the bytes or why I am getting these erroneous bytes? I am not so sure that the map command is the best way to do this. :~
hurley:
Thanks for the response. After removing the code you said to remove, it has the same issue.
Any other ideas?
So you changed the code but you're not going to post the new code that doesn't work? How exactly are we supposed to know you did it correctly (which obviously, you didn't)?
byte microData; // Incoming Serial1 byte to store
byte tempbyteData; // byte storage to make equal to microData byte
byte newbyteData; // new converted byte to store
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600); // *tried changing this to 20% (19200 bps) more than Serial 1 but gave ALL erroneous data*
}
void loop()
{
if (Serial1.available())
{
microData = Serial1.read();
Serial.print(" microData that is coming in on Serial1 is: ");
Serial.println(microData, HEX); // DEBUG :print microData coming in
tempbyteData = microData; // assign tempbyteData byte to microData byte
newbyteData = map(tempbyteData, 254, 255, 0, 1); // if tembyteData is equal to 254 change to 00, if tempbyteData is
// equal to 255 change to 01. Then assign to newbyteData.
Serial.print(" tempbyteData is: ");
Serial.println(tempbyteData, HEX); //DEBUG: verify tempbyteData is equal to microData coming in
Serial.print(" newbyteData is: "); // DEBUG: verify what the new value of newbyteData is
Serial.println(newbyteData, HEX);
Serial2.write(newbyteData); // send newbyteData out Serial2 as a hex byte
delay(100);
}
}
It really seems that the Arduino can't keep up with the data coming into Serial1, as I just get hex bytes 0x00 repeatedly after about 2 seconds. Although when it starts processing the data it works and output is as expected.
hurley:
newbyteData = map(tempbyteData, 254, 255, 0, 1); // if tembyteData is equal to 254 change to 00, if tempbyteData is
That's a... creative ... way to do it. Why not simply this:
ouput = input - 0xFE;
It really seems that the Arduino can't keep up with the data coming into Serial1, as I just get hex bytes 0x00 repeatedly after about 2 seconds. Although when it starts processing the data it works and output is as expected.
Well this isn't exactly helping you process data as fast as possible:
byte microData; // Incoming Serial1 byte to store
byte tempbyteData; // byte storage to make equal to microData byte
byte newbyteData; // new converted byte to store
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600); // *tried changing this to 20% (19200 bps) more than Serial 1 but gave ALL erroneous data*
}
void loop()
{
if (Serial1.available())
{
microData = Serial1.read();
Serial.print(" microData that is coming in on Serial1 is: ");
Serial.println(microData, HEX); // DEBUG :print microData coming in
tempbyteData = microData; // assign tempbyteData byte to microData byte
newbyteData = tempbyteData - 0xFE; // if tembyteData is equal to 254 change to 00, if tempbyteData is
// **Tried: newbyteData = (tempbyteData & 0x01); // both work, but data still gets lost after it starts with erroneous hex bytes**
Serial.print(" tempbyteData is: ");
Serial.println(tempbyteData, HEX); //DEBUG: verify tempbyteData is equal to microData coming in
Serial.print(" newbyteData is: "); // DEBUG: verify what the new value of newbyteData is
Serial.println(newbyteData, HEX);
Serial2.write(newbyteData); // send newbyteData out Serial2 as a hex byte
delay(1);
}
}
I am beginning to think this is how fast arduino is processing the data before it passes the data to the other serial port. Even with the code changes I am still getting getting erroneous data after about 2 seconds.
microData = Serial1.read();
...
tempbyteData = microData; // assign tempbyteData byte to microData byte
...
newbyteData = tempbyteData - 0xFE; // if tembyteData is equal to 254 change to 00, if tempbyteData is
Why waste time with stuff like this? If you're concerned about the Arduino not being able to process the data quickly enough, don't give it erroneous tasks:
newbyteData = Serial1.read() - 0xFE;
delay(1);
Why?
I am beginning to think this is how fast arduino is processing the data before it passes the data to the other serial port. Even with the code changes I am still getting getting erroneous data after about 2 seconds.
How about printing the results of Serial.available()? See if, in fact, the Arduino can't keep up with what is sending the data (which you didn't answer my previous question about).
I had heard in a forum that it was good to add some delay on Serial write and print functions to give the serial buffer time to complete write and prints. Not in this case.
hurley:
I had heard in a forum that it was good to add some delay on Serial write and print functions to give the serial buffer time to complete write and prints. Not in this case.
That's typically only the case when you are printing things on every iteration of loop(); you're only printing data when Serial data is received.