I am doing a project controlling 2 servo motors with a PS3 controller. I am fairly new to programming so my idea might be way off. I was basically sending a letter to establish which servo in front of the 1-179 value to be sent to that particular servo. But honestly I don't have a clue how to take that information into arduino. Is this even the best way to distinguish between the two servos? Any help is appreciated!
You have not given us much to go on.
If you want to send two values for two servos why just send <123,57> and use the third example in serial input basics. You can tell your Arduino always to treat the first number as intended for servoA and the second number for servoB.
...R
Code for controlling several servos.
//zoomkat 11-22-12 simple delimited ',' string parse
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added
// Powering a servo from the arduino usually *DOES NOT WORK*.
String readString;
#include <Servo.h>
Servo myservoa, myservob, myservoc, myservod; // create servo object to control a servo
void setup() {
Serial.begin(9600);
//myservoa.writeMicroseconds(1500); //set initial servo position if desired
myservoa.attach(6); //the pin for the servoa control
myservob.attach(7); //the pin for the servob control
myservoc.attach(8); //the pin for the servoc control
myservod.attach(9); //the pin for the servod control
Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}
void loop() {
//expect single strings like 700a, or 1500c, or 2000d,
//or like 30c, or 90a, or 180d,
//or combined like 30c,180b,70a,120d,
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
if (readString.length() >1) {
Serial.println(readString); //prints string to serial port out
int n = readString.toInt(); //convert readString into a number
// auto select appropriate value, copied from someone elses code.
if(n >= 500)
{
Serial.print("writing Microseconds: ");
Serial.println(n);
if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
}
else
{
Serial.print("writing Angle: ");
Serial.println(n);
if(readString.indexOf('a') >0) myservoa.write(n);
if(readString.indexOf('b') >0) myservob.write(n);
if(readString.indexOf('c') >0) myservoc.write(n);
if(readString.indexOf('d') >0) myservod.write(n);
}
readString=""; //clears variable for new input
}
}
else {
readString += c; //makes the string readString
}
}
}
zoomkat:
Code for controlling several servos.
Different code for controlling several servos from serial (untested):
// Control several servos with serial commands like "a75 b20 c134 d150"
#include <Servo.h>
byte servoPins[]={6,7,8,9};
#define NUMSERVOS sizeof(servoPins)
Servo servos[NUMSERVOS];
char servoChars[]="abcd"; // char identifier for each servo
void doServoCommand()
{ // reads commands from serial and sets servo positions
static int servoIndex=-1; // -1 is invalid index initially
static int servoAngle;
if (Serial.available())
{
char c = Serial.read();
if (strchr(servoChars,c)!=NULL) // if char is a, b, c, d is found
{
servoIndex=strchr(servoChars,c)-servoChars; // remember the servo index
servoAngle=0; // reset angle to zero
}
else if (isdigit(c)) // char is an ASCII number
{
servoAngle= 10*servoAngle + c-'0'; // calculate servo angle from new digit
if (servoAngle>180) servoAngle=180; // limit angle to 180 degrees
}
else if (servoIndex>=0) // command is ready
{
Serial.print("set servo ");
Serial.print(servoChars[servoIndex]);
Serial.print(" to angle ");
Serial.println(servoAngle);
servos[servoIndex].write(servoAngle);
servoIndex=-1; // clear command after execution
}
else servoIndex=-1; // clear command after invalid char
}
}
void setup() {
Serial.begin(9600);
for (int i=0;i<NUMSERVOS;i++) servos[i].attach(servoPins[i]);
Serial.println("Servo control");
}
void loop()
{
doServoCommand();
}
Control code characters are case sensitive. If you'd prefer uppercase, change to:
char servoChars[]="ABCD"; // char identifier for each servo