I am new to microprocessors including the Arduino. I am also new to robots. The reason I need to do this is that I have joined the Robot Club (FRC) at the High School. I have an electronics(10 yrs), and programming(30) but a bit rusty and NO Robot background. My intension is to learn about Robots, MicroProcessors so I can help next years students when the current experts go off to college. To learn robots I am building a robot with as many sensors as I can use. Thye Arduino UNO R3 does not have enought Digital PWM ports. So the Arduino UNO will have the Motor Controller, three Ultrasound range finders... The Arduino Mega will have three passive sensors to start with. The Arduino Mega will update the Arduino UNO via binary messages over the Serial Port about the state of all sensors connected to it. The Arduino may need to send messages to the Arduino Mega. Later I will add the "SerialEvent" so the main code can execute without waiting on incoming data. The memset of the buffer is to insure I am not reading old data.
Because I have NO experience with Robots and Microprocessors each time I add a sensor or feature like communications I write code to learn each sensor or feature without any other functionality. That is what this code is. It is to help me understand how to do Serial communications between these two Arduinos. You are right is does nothing more than bounce a message back and forth. The Arduino Mega is the original sender. Once I learn the Arduino I can move on to the cRIO & FRC code provided to the students to build Robots.
I added the numbers at the left because that is what I thought you wanted. I have made the changes you have suggested. It is still not working. Nothing but trash. You can see bits and pieces of the original message amoung the trash. It looks as thou they are not communicating at the same speed or with different communication confguration parameters. (which is why I added the SERIAL_8N1 even tho it is the default)
Here is the updated code on the Arduino UNO R3
#define BUFLEN 200
char buf[BUFLEN];
void setup()
{
delay(3000);
Serial.begin(9600);
Serial.setTimeout(100);
}/* setup */
void loop()
{
int cnt = 0;
cnt = Serial.readBytesUntil('\n', buf, BUFLEN-50);
if (cnt > 0) Serial.write((uint8_t*) buf, cnt);
memset(buf,0,BUFLEN);
delay(3000);
} /* loop */
Here is the updated code on the Arduino Mega
#define BUFLEN 200
char buf[BUFLEN];
void setup()
{
Serial.begin(9600);
Serial2.begin(9600);
Serial.println("ready to exit setup");
Serial2.setTimeout(100);
delay(3000);
}/* setup */
void loop()
{
int cnt = 0;
Serial2.write("This is a message from Arduino Mega to the Arduino UNO R3 via the serial port\n");
cnt = Serial2.readBytesUntil('\n', buf, BUFLEN-40);
Serial.print(cnt); Serial.print(" -- ");
if (cnt > 0 ) Serial.println(buf);
memset(buf,0,BUFLEN);
delay(3000);
} /* loop */
Here is the output at the Arduino Mega
ready to exit setup
49 -- is ió mesó froí.VWËë??ÿ¡?AþWËëþ?Í ÿthe ó+±ðø
46 -- Z.JÍ mesû2ɽµVWËëYVô¡?Aþ]ZëÿR3 vi¡?sÿX?½
49 -- is ió mesû2ɽµñ?Õ¥¹ý??ÿ¡?AþWËëÿ?Í þthe ó+±ðá
49 -- is ió mesû froí.VWËë??ÿ¡?AþWËëþ?Í vi¡?sÿX?½
50 -- is ió mesó2ɽµVWËëÿega ô¡?AþWËëþR3 vi¡?sÿX?½ø
49 -- is ió mesó2ɽµ?Õ¥¹ý??ÿ¡?AþWËëþ?Í ÿthe ó+±ðá
49 -- is ió mesó froí.VWËë??ô¡?AþWËëþ R3 ¢¡?sÿX?½
48 -- is ió mesó froírduinª?¡?AþWËëþ?Í ÿthe ó+±ðá
49 -- ¥Íiûmesû2ɽÿÉ?Õ¥¹ª?ÿ¡?AþWËëú?Í þthe sZX?á
48 -- ¥Íiûmesû2ɽÿÉ?Õ¥¹ª?¡?AþWËëú?Í öthe s?±ð)
47 -- ¥Íiûmesû2ɽÿÉ?Õ¥¹ª?¡?AþWËëþ?Í ¢¡?ó+±ð)
52 -- ¥Íiûmesû2ɽÿÉ?Õ¥¹ª?ü¡?AþWËëþ R3 ÿ theà¥?±þ?ø
52 -- ¥Íiûmesû2ɽÿÉ?Õ¥¹ÿª?ÿ¡?AúWËëþ R3 theørialàrt
47 -- ¥Íiûmesû2ɽÿÉ?Õ¥¹ª?¡?AþWËëþ?Í ¢¡?ó+±ð)
53 -- +kiûmesû2ɽõÉ?Õ¥¹ÿª?ÿ¡?AúWËëþ R3 theürialàÉÑ)
52 -- +kiûmesû2ɽÿÉ?Õ¥¹ÿª?ÿ¡?AþWËëþR3 X¢¡?eriaüort
51 -- +kiûmesûY2ÉýÉ?Õ¥¹ÿª?ÿ¡?AþWËëþR3 X¢¡?á?¥?±KÑ)
52 -- +kiûmesó2ɽÿÉ?Õ¥¹ÿª?ô¡?AþWËëþR3 X¢¡?eriaüort
52 -- +kiûmesó2ɽÿÉ?Õ¥¹ÿª?ÿ¡?AþWËëþR3 X¢¡??É¥?ùort
52 -- +kiûmesó2ɽõÉ?Õ¥¹ÿª?ÿ¡?AþWËëþR3 X¢¡??É¥?ùort
36 -- +kiûmesó2ɽõÉ?Õ¥¹ýª?ÿ¡?Aþ]Zëÿ
In response to your statement:
Take it easy with the aggressive tone. We are trying to help. You aren't being very responsive.
I really did not find the tone of PaulS's comment to be pleasant.
You really don't understand how NULL terminated strings work, do you? Nor do you seem to even
have a concept what a NULL terminator is.
Think of it as a stop sign. You stop when you get to one, right? Is there any reason to put 2
00 of them on one sign post? Do that make you more inclined to stop? Or does it make the signpost
look less than intelligent?
It came accross to me as condescending, but lets put all that behind us, and let me take advantage of y'all's knowlege of the Arduino.
Thanks for pointing out the error in the ZERO macro, I now see the error in it.
thanks...pat
I just saw PaulS's new comment. Thanks. The only difference now is that I do the memset instead of just inserting the NULL at position 0. I will try that additional change. I do not think it should make a difference, but sometimes thing become clear after you understand the error. If that does not fix it then it is likely something electrial or with the boards.