Go Down

Topic: Arduino and RoboRealm (Read 999 times) previous topic - next topic


I've got RR tracking an object and sending it's X position down the serial to the Arduino to move a servo. The Arduino is getting the info, but I'm not converting it to something the servos can use. I've tried a few examples I've found on the web, but nothing seems to work. RR is sending a value from 0 to 640. From what I gather RR is sending Ascii to the Arduino (I could be wrong on this). My sketch uses atoi to get an interger and maps it from 0 to 180 for the servo, but the servo just twitches wildly. I'm new to programming, so please speak slowly ;)


#include <Servo.h>
#define servoPin 9  // control pin for servo motor

Servo Scan;  //The servo        
char incomingData; // To hold value of data coming through COM port

int pos;
void setup()
 pinMode(servoPin, OUTPUT);
 Scan.attach(servoPin); //Attach servo

void loop()
 while(Serial.available() > 0) // Wait for data
   incomingData = Serial.read();
   int numericVal = atoi(&incomingData); //convert Ascii to interger

   pos = map(numericVal, 0, 640, 0, 180); //map RoboRealm COGX position to servo range
   Scan.write (pos);
   //Serial.print (pos);


Suppose that RoboRealm sent a value like 514. The data on the serial port will consist of three characters - '5', '1', '4'.

You are currently reading just the first character, the '5'. You convert that to an integer, 5, and map that value. The result is probably a 1, so you move the servo to 1.

You then immediately read the next character, a '1', convert that to an integer, 1, map it, resulting in a 0, move the servo, and go on to read the next character.

You need to create an array, and an index into that array. Read all the serial data into the array, keeping the array NULL terminated, and then convert the string into an integer.
The art of getting good answers lies in asking good questions.

Go Up