0% found this document useful (0 votes)
69 views

Arduino Code

This code controls 4 servos (myservoA, myservoB, myservoC, myservoD) by receiving direction and angle commands over serial. It initializes the servos, sets up serial communication to receive formatted data (e.g. <A,90>), parses the data into a direction and angle, and uses the direction to determine which servo to move to the angle position.

Uploaded by

Bien
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Arduino Code

This code controls 4 servos (myservoA, myservoB, myservoC, myservoD) by receiving direction and angle commands over serial. It initializes the servos, sets up serial communication to receive formatted data (e.g. <A,90>), parses the data into a direction and angle, and uses the direction to determine which servo to move to the angle position.

Uploaded by

Bien
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <Servo.

h>
int Angle = 0;
Servo myservoA; // create servo object to control a servo
Servo myservoB; // create servo object to control a servo
Servo myservoC; // create servo object to control a servo
Servo myservoD; // create servo object to control a servo

const byte numChars = 7;


char receivedChars[numChars]; // an array to store the received data

char Dir[numChars] = {0};


int Mspeed = 200;
boolean newData = false;

void setup() {
Serial.begin(9600);
myservoA.attach(5); // attaches the servo on pin 5 to the servo object
myservoB.attach(6); // attaches the servo on pin 6 to the servo object
myservoC.attach(7); // attaches the servo on pin 7 to the servo object
myservoD.attach(8); // attaches the servo on pin 8 to the servo object
}

void loop() {
recvWithStartEndMarkers();
if (newData ==true){
parseData();
Motoraction();
newData = false;
}
}

void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;

while (Serial.available() > 0 && newData == false) {


rc = Serial.read();

if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}

else if (rc == startMarker) {


recvInProgress = true;
}
}
}

void parseData() {
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(receivedChars,","); // get the first part - the string
strcpy(Dir, strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(NULL, ","); // this continues where the previous call left
off
Angle = atoi(strtokIndx); // convert this part to an integer
//strtokIndx = strtok(NULL, ",");
//floatFromPC = atof(strtokIndx); // convert this part to a float

void Motoraction() {
if (Dir[0] == 'A') {
if (Angle>0){
myservoA.write(Angle); // sets the servo position according to
the scaled value
Serial.println(Angle);
delay(15); // waits for the servo to get there
}
}
if (Dir[0] == 'B') {
if (Angle>0){
myservoB.write(Angle); // sets the servo position according to
the scaled value
Serial.println(Angle);
delay(15); // waits for the servo to get there
}
}
if (Dir[0] == 'C') {
if (Angle>0){
myservoC.write(Angle); // sets the servo position according to
the scaled value
Serial.println(Angle);
delay(15); // waits for the servo to get there
}
}
if (Dir[0] == 'D') {
if (Angle>0){
myservoD.write(Angle); // sets the servo position according to
the scaled value
Serial.println(Angle);
delay(15); // waits for the servo to get there
}
}
}

You might also like