1

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
  }
}
6
  • 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
    Mar 29, 2018 at 21:15
  • would it just be a basic serial.read() and serial.write command?
    – Cody Helms
    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
    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 servo
    – jsotola
    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.
    – Paul
    Mar 31, 2018 at 12:50

1 Answer 1

0

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.

  1. 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 use Serial.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.
  2. 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.

1
  • 2
    Actually, 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. Mar 31, 2018 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.