I'm trying to take the position data generated from this servo sweep, and send over serial to another Arduino. The position data is stored as pos
in the code I included, what would be the best method for doing this?
/* Sweep
by BARRAGAN <http://barraganstudio.com>
This example code is in the public domain.
modified 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Sweep
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
-
How about to just send it over serial? You seem to already have decided, that you want to use serial, so why don't you just do it? Is there a specific problem?chrisl– chrisl2018年03月29日 21:15:17 +00:00Commented Mar 29, 2018 at 21:15
-
would it just be a basic serial.read() and serial.write command?Sneaky– Sneaky2018年03月29日 21:17:45 +00:00Commented Mar 29, 2018 at 21:17
-
Yes that's all you would need. You'd be sending two bytes over, so the receive side would have to deal with that.CrossRoads– CrossRoads2018年03月30日 02:57:31 +00:00Commented Mar 30, 2018 at 2:57
-
you posted code that has nothing to do with your question ... your question is about sending data from one arduino to another arduino ... you posted code that positions a servojsotola– jsotola2018年03月30日 05:19:38 +00:00Commented Mar 30, 2018 at 5:19
-
When using bytes for location (0-180 degrees), you can simply use: myservo.write(pos);myserial.write(pos); and on the receiving side myservo.write(receivedValue); (check some tutorials on how to receive a value.aaa– aaa2018年03月31日 12:50:38 +00:00Commented Mar 31, 2018 at 12:50
1 Answer 1
Yes, sending data over Serial is as simple as using Serial.write()
or it's siblings. When sending data over Serial you have to consider, how you want to send it.
- Raw binary data: You can simply use the
Serial.write()
command, which will send the variable as binary data. If you whatch this on the serial monitor of your PC, you will not see readable numbers, because what is shown there is interpreted as ASCII-encoded data, where every byte value has a corresponding character (for example the value 97 is the letter 'a'). On the other Arduino you just useSerial.read()
to write the byte data directly to a variable. This is practical, if you are sending always the same amount of data (like the value of one sensor). If the length of a message is variable, you have to use a delimiter, to mark the end of a message. Here the second way is more save. - ASCII encoded data: You can send your data human readable as ASCII encoded data. The value 128 then would consist of 3 bytes, one for each character. Here you always have to use a delimiter, so that the receiver can tell the values apart. A common message delimiter is the newline character '\n'. On Windows you also often have newline and carriage return '\r' together. For decoding you can use one of the many functions, that convert a string/character array to a number with the wanted format (in your case integer), for example
atoi()
.
Refer to the Serial examples in the Arduino IDE to see the code in action.
Note: Since the value of pos
only varies between 0 and 180, you can use one byte to save and transmit it, instead of the 2 bytes the integer uses.
-
2Actually, that simple binary method is limited not to fixed-length messages, but rather to single-word messages. If you want to send longer messages, regardless if they are fixed length or variable, you need a mechanism to achieve at least initial synchronization to indicate the start of a word. Time gap is the weakest such method, reserved or escaped codes etc are more robust but complex. ASCII encoding with a delimiter (especially a newline) does indeed become attractive, and is readily observed in a terminal as well.Chris Stratton– Chris Stratton2018年03月31日 21:05:36 +00:00Commented Mar 31, 2018 at 21:05