Hello,
I am interested in using Max messages to control multiple servo motors, each independently.
I was able to successfully get a system (hardware, programming using the arduino's Servo library) to control multiple motors from the same Max slider value send over serial (usb).
However, I am a relatively inexperienced programmer, and am having trouble coming up with a protocol to parse the serial data in the Arduino code to send one value to one motor, and a second value to another.
I have done a little bit of research and know that the solution probably calls for an index or address character that I would assign to each position value in Max, and that would be parsed and sent to the various motor positions in Arduino. However, I am struggling to figure out how to format this address character (ASCII vs Integer, etc) and how to code in Arduino to read this address and send it to the particular motor it addresses.
Below I've attached an abridged Arduino 2 code of my general motor set up, and a sample Max patch assigning possible address labels.
Any hints/advice/suggestions would be greatly appreciated!
Thanks!
JCR
//concept sketch in Arduino 2 code for controlling 2 independent motors,
#include <Servo.h> //load servo library
Servo servo1; // create servo object to control a servo (part of servo library protocol)
Servo servo2;
unsigned char temp_data = 0; //value for the entire serial read -some combo of address and position values
unsigned char address = 0; //the address value indicating which servo to send position data (1, 2, 3, etc.)
unsigned char pos = 0; //the motor position value, which follows the address (0-179)
unsigned char Max1; // value for servo1 position
unsigned char Max2; // value for servo2 position
//HERE IS WHERE I WOULD DEFINE SOME FUNCTION TO PARSE THE ADDRESS AND POSITION DATA
void setup()
{
servo1.attach(3); // attaches the output on pin 3 to the servo1
servo2.attach(4); // attaches the output on pin 4 to the servo2
Serial.begin(9600);
}
void loop()
{
if (Serial.available()) { //check if data sent from cpu
temp_data = Serial.read(); //assign value for entire serial read
}
if (address = 1) //is for servo1 position -HERE WILL BE THE IF STATEMENT READING THE ADDRESS
{
pos = Max1;
servo1.write(Max1); // sets the servo1 position according to value
}
if (address = 2) //is for servo2 position
{
pos = Max2;
servo2.write(Max2); // sets the servo2 position according to value
}
delay(15); // waits for the servo to get there
}
----------begin_max5_patcher----------
1155.3oc2Y0zaiaCD8r8uBBcoI.dMHoDsjJJZQOt2JPO1rXAiDiM2UlTPj1I
sK1+6keIamrRdcxRgj1KRlTTRy6wYdyLxeY9rjakOvTIfeF7WfYy9x7YybSY
mXVX7rjszG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