import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
ser.write('3')
This is for sending 1 variable but what if I would like to send multiple variables? Should I make one package with all the variables and unpack this package in arduino?
Another question I have is:
Can I tell the raspberry pi that the arduino has finished "doing something"
e.g.
-raspberry pi tells arduino to move servo1 to 100 and servo2 to 120.
-arduino tells raspberry pi it is done
-raspberry pi continuous with its loop
I think I would have the program on the PI make up a string like this "S1b S2b X" and send that to the Arduino. In both cases b is a byte containing the value of the angle for the servo.
The Arduino program would then know that byte 2 (counting from 0) would be the number for servo 1 and byte 5 would be the number for servo 2.
This could be simplified to SbbX. The S and the X are useful so that the Arduino knows when the string starts and ends.
The PI would then wait for the Arduino to send something back to signify it was ready for another command.
Thanks for your help! I think I understand your point but how would this look in code? is there an example available anywhere? I googled but couldn't find any.
I also noticed that I can not send anything from 10 and above and it will seperate 1 and 0. How can I then send angles above these values??
sorry for the newbie questions but I am completely new to serial communication.
I also noticed that I can not send anything from 10 and above and it will seperate 1 and 0. How can I then send angles above these values??
That is because you are printing them as a string. You get round this in two ways:-
Have the receiver look for a C string with a terminating character.
Send the data as a byte using the Serial.write command.
but how would this look in code
The usual, if statements, variable assignments and loops.
I have succeeded to send multiple variables over usb serial with raspberry pi and arduino. I used the technique of sending special characters after each piece of data such as a $ or # to terminate the value reading at the arduino side.
the if statements variable assignments and loops seemed to have done the trick!