Hi! So for some reason, my "Claw" servo is not working as intended. I have already tested the servo and it works, it just trips up over the logic. Just some background, the Arduino is supposed to take a code and deconstruct the string in order to control a motor/servo. For example, ""; "<" and ">" signify the start and end of the message. The "B" tells the program which motor/servo to control, and the "200" signifies the speed/mapping of the servo/motor. The reason this is necessary is that I have a Processing code running in the background taking inputs from my controller and sending mapping data over the Serial port. The "Claw" servo seems to work at the beginning where I map it so that it does not stall at the close, but completely stops working after that. Does anyone have any idea why that might be? Any help would be appreciated.
Main Code:
#include <Wire.h>
#include <Servo.h>
#include <Adafruit_MotorShield.h>
//set up motorhsield
Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x60);
//assign motors
Adafruit_DCMotor *RailMotor = AFMS.getMotor(1);
Adafruit_DCMotor *Swivel = AFMS.getMotor(2);
Adafruit_DCMotor *Screw = AFMS.getMotor(3);
//call the servo library
Servo DrillArm, ClawArm, Claw, Elbow;
const byte number_of_chars = 32;
char receivedChars[number_of_chars];
boolean new_data = false;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
// create with the default frequency 1.6KHz
AFMS.begin();
// Attach a servo to pin #10 and pin #9
DrillArm.attach(11);
ClawArm.attach(10);
Claw.attach(9);
Elbow.attach(12);
}
void loop() {
// put your main code here, to run repeatedly:
Claw.write(map(75, 0, 180, 0, 180));//prevent servo from stalling
receivePacket();
if (new_data == true) {
parseData();
executeParsedData();
new_data = false;
Serial.println(receivedChars);
char buf[4] = {receivedChars[1], receivedChars[2], receivedChars[3], receivedChars[4]};
int mapp = atoi(buf);
Serial.println(mapp);
if (receivedChars[0] == 'A'){ //claw arm control
Serial.println(receivedChars);
ClawArm.write(map(mapp, 0, 180, 0, 180));
}
else if(receivedChars[0] == 'B'){ //elbow control
Serial.println(receivedChars);
Elbow.write(map(mapp, 0, 180, 0, 180));
}
else if(receivedChars[0] == 'C'){ //claw open or close
Serial.println(receivedChars);
Claw.write(map(mapp, 0, 180, 0, 180));
}
else if(receivedChars[0] == 'D'){ //drill arm control
Serial.println(receivedChars);
DrillArm.write(map(mapp, 0, 180, 0, 180));
}
else if(receivedChars[0] == 'E'){ //rail control
Serial.println(receivedChars);
if (mapp > 20){
RailMotor->setSpeed(mapp);
RailMotor->run(FORWARD);
}
else if (mapp < -20){
RailMotor->setSpeed(abs(mapp));
RailMotor->run(BACKWARD);
}
else {
RailMotor->run(RELEASE);
}
}
else if(receivedChars[0] == 'F'){ //swivel control
Serial.println(receivedChars);
if (mapp > 15){
Swivel->setSpeed(mapp);
Swivel->run(FORWARD);
}
else if (mapp < -15){
Swivel->setSpeed(abs(mapp));
Swivel->run(BACKWARD);
}
else {
Swivel->run(RELEASE);
}
}
else if(receivedChars[0] == 'G'){ //screw control
Serial.println(receivedChars);
if (mapp > 15){
Screw->setSpeed(mapp);
Screw->run(FORWARD);
}
else if (mapp < -15){
Screw->setSpeed(abs(mapp));
Screw->run(BACKWARD);
}
else {
Screw->run(RELEASE);
}
}
}
}
2nd Tab:
#include <SoftwareSerial.h>
#include <Wire.h>
//SoftwareSerial mySerial(11, 12); // RX, TX
int red_led = 3;
int green_led = 6;
int blue_led = 5;
int int_1 = 0;
int int_2 = 0;
int int_3 = 0;
void receivePacket() {
static boolean receiving = false;
static byte index = 0;
char start_mark = '<';
char end_mark = '>';
char rc;
while (Serial.available() > 0 && new_data == false) {
rc = Serial.read();
if (receiving == true) {
if (rc != end_mark) {
receivedChars[index] = rc;
index++;
if (index >= number_of_chars) {
index = number_of_chars-1;
}
} else {
receivedChars[index] = '\0';
receiving = false;
index = 0;
new_data = true;
}
} else if (rc == start_mark) {
receiving = true;
}
}
}
void parseData() {
char * split;
split = strtok(receivedChars, ",");
int_1 = atoi(split);
split = strtok(NULL, ",");
int_2 = atoi(split);
split = strtok(NULL, ",");
int_3 = atoi(split);
}
void executeParsedData() {
if (int_3 == 0) {
analogWrite(red_led, 0);
analogWrite(green_led, 0);
analogWrite(blue_led, 0);
} else if (int_3 == 1) {
analogWrite(red_led, 255);
analogWrite(green_led, 255);
analogWrite(blue_led, 255);
} else if (int_3 == 2) {
if (int_2 == 0) analogWrite(red_led, int_1);
if (int_2 == 1) analogWrite(green_led, int_1);
if (int_2 == 2) analogWrite(blue_led, int_1);
}
}