I am building a remote control robot with two aruinos(UNO), and a RF link. You put serial data in the transmitter and it comes out the reciever. I need to control two motors(with PWM). Any ideas on how to control two motors with PMW capabilities through a serial interface?
N314:
Any ideas on how to control two motors with PMW capabilities through a serial interface?
There are tons of tutorials on how to control motors and how to communicate over serial. So what is your specific question?
I know how to control motors. I need to send two numbers, one for each of the motors, but how can I make code to tell which number(from serial connection), goes to which motor. Like... Left motor PWM 200, and right 100. How can I send serial data and differentiate the two values for the two motors?
Think about how you'd tell another person two values.
I'd say, "The first value is 38 and the second value is 97." So, why don't you have your arduino do it, but with more concise phrases than "the first value is" (maybe "1" or "a")
It's also usuallly a good idea to send a signal at the start and end of each message - so your message may look like "b1[motor1value]2[motor2value]e"
Then you'd send it with Serial.write(), and read it, one letter at a time, with Serial.read().
I understand that part. I need to know how to interpret and then make use of the serial data.
Simple test code for two servos that could be adapted for PWM for two motors with drivers.
// zoomkat 11-22-10 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550
#include <Servo.h>
String readString, servo1, servo2;
Servo myservo1; // create servo object to control a servo
Servo myservo2;
void setup() {
Serial.begin(9600);
myservo1.attach(6); //the pin for the servo control
myservo2.attach(7);
Serial.println("servo-test-21"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(1);
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
}
}
if (readString.length() >0) {
Serial.println(readString); //see what was received
// expect a string like 07002100 containing the two servo positions
servo1 = readString.substring(0, 4); //get the first four characters
servo2 = readString.substring(4, 8); //get the next four characters
Serial.println(servo1); //print to serial monitor to see results
Serial.println(servo2);
int n1; //declare as number
int n2;
char carray1[6]; //magic needed to convert string to a number
servo1.toCharArray(carray1, sizeof(carray1));
n1 = atoi(carray1);
char carray2[6];
servo2.toCharArray(carray2, sizeof(carray2));
n2 = atoi(carray2);
myservo1.writeMicroseconds(n1); //set servo position
myservo2.writeMicroseconds(n2);
readString="";
}
}
Well, the easiest way to convert a single character to a number, 0 to 9, (if you're SURE it's a number) is to subtract '0' from the character. Since, in ASCII, all of the numbers are in order, when you subtract the numeric representation of the character '0', you'll get 0 if the character is '0', 1 if the character is '1', and so on. From there, you can multiply the higher characters by multiples of 10 and add them together to get a final, multidigit number.