Greetings.
I built a Rubik's Cube robot, had to incorporate a Raspberry Pi for the brain work. The Pi sends a command code to the Mega, Mega sends a character back to Pi when the stepper motors are done, Pi thinks about it, sends the next command code, etc, etc, ad nauseum. It worked great, but now, suddenly, the serial communication is weird.
I swapped out Mega boards, but get the same result. I switched to my Linux laptop in case the Pi board is whacked. Same result. (I hope the Pi isn't whacked, and screwed up both of my Mega boards.) I also had a terrifically bad time uploading to both Mega's, and finally got success by going into the Sketch menu and hitting Upload from there, on the laptop at least. Weird...
I wrote a simple test program in Python and Arduino. The Mega should send back the letters T-E-S-T-I-N-G, one at a time, and keep doing so because it is in the void() loop. Mega sends back 1-2-5-9-2, then the letters, from a random place in the loop, but then it continues as normal. I tried different baud rates, but no difference. In the Rubik's program, the Mega sends those exact 5 numbers, but then just keeps sending numbers, and odd letters.
Here is the Python code:
import serial
import time
import sys
robot = serial.Serial('/dev/ttyACM0',9600)
time.sleep(5)
def main():
robot.write(b'17D')
time.sleep(1)
time.sleep(1)
tileColor = 0
while(tileColor == 0):
tileColor = robot.read()
time.sleep(1)
print(tileColor)
tileColor = 0
robot.write(b'17D')
time.sleep(1)
return
############## START OF PROGRAM!!! ##########################
main()
Here is the Arduino code:
char command;
void setup() {
Serial.begin(9600);
while (!Serial){
;
}
delay(1000);
Serial.print('10');
delay(1000);
}
void loop() {
command = 0;
if (Serial.available()) {
command = Serial.read();
delay(1000);
Serial.print('t');
delay(1000);
}
if (Serial.available()) {
command = Serial.read();
delay(1000);
Serial.print('e');
delay(1000);
}
and so on until t-e-s-t-i-n-g are all sent, then it loops
And here is what the Mega sends back:
====== RESTART: /media/mark/USB STICK/Correct Files To Run/test_serial.py ======
b'1'
b'2'
b'5'
b'9'
b'2'
b'n'
b'g'
b't'
I stopped it here, but it continues sending the proper letters, no more numbers.
I started building this robot over 10+ years ago, one of those evolving projects (still is), and it worked great, until now.
I have done a lot of researching about this, especially why those exact 5 numbers every time, with no success.
And now I am here! :oD