Go Down

Topic: Using the Arduino serial monitor to control multiple servos (Read 1 time) previous topic - next topic

chuy125

I am making a 6-axis robotic arm that uses multiple servo motors. I have just started programing with the Arduino so I need help debugging a program that I have piece together from guides through out the internet. The program will compile and upload but when I input values into the serial monitor the servo will not move. Furthermore, I kept the program for one servo just for testing I know that I will have to create an int motorA=0; motorB=1; etc. Any help will be appreciated

reference links:
https://forum.arduino.cc/index.php?topic=242458.0
https://www.youtube.com/watch?v=y8X9X10Tn1k

Hardware:
Arduino Uno
16 channel Arduino Servo shield
5v external power supply
MG995 Servo


Program:

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
String readString;

// called this way, it uses the default address 0x40
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

#define SERVOMIN  650 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  2350 // this is the 'maximum' pulse length count (out of 4096)

// our servo # counter
uint8_t servonum = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("16 channel Servo test!");

  pwm.begin();
 
  pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz updates

  yield();
}

int angleToPulse(int ang){
   int pulse = map(ang,0, 180, SERVOMIN,SERVOMAX);// map angle of 0 to 180 to Servo min and Servo max
   Serial.print("Angle: ");Serial.print(ang);
   Serial.print(" pulse: ");Serial.println(pulse);
   return pulse;
}

void loop()
{

  //expect single strings 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 angle = readString.toInt();  //convert readString into a number
 
        Serial.print("writing Angle: ");
        Serial.println(angle);
        if(readString.indexOf('a') >0) pwm.setPWM(0, 0, angleToPulse(angle) );
       
        readString=""; //clears variable for new input
      }
    } 
    else
    {     
      readString += c; //makes the string readString
    }
  }
}

vinceherman

Quote
the servo will not move
Have you run the servo sweep tutorial to confirm that you have the servo wired up properly?

Edit:  I see you are using a servo shield.  Rather than the servo sweep tutorial, you will need to use whatever sample source the shields library comes with.

Go Up