[SOLVED] Arduino Due, cant control servos, bugged Servo.read() ?

Hello World!
So I have two regular standard servos, and I wrote a little class which calls Servo.h library. Class has methods GripperGo and PitchGo which would send servo 1 and servo 2 respectively to a specified position.
My class was controlling servos fine with Arduino Uno, Fio, and Mega.

Today I tested it out with Due. First, my code wouldnt compile because i was using an old Servo.h library which didnt have a right timer support or something. I got a latest Servo.h library from Github, the code compiled, I tested it out with my Mega, and it still worked. However, it didnt work right on Due. With Due, my program seems to be getting stuck permanently while calling my class. If i do Serial.println("foo") right after i call my class, it never gets there.
Could you guys check out my header and source file and tell me whats wrong?
Also, Im able to run a servo using Due with a servo Sweep example, so there must be something about my class thats incompatible with Due

Header:

header: Header

/*
	This class sets up controls for the arm
*/

#ifndef Arm_h
#define Arm_h

#include "Arduino.h"
#include <Servo.h>

class Arm
{
	public:
		Arm(int pwmPitch, int pwmGrip);
		void GripperGo(int pos1);
		void PitchGo(int pos2);
        void Attach();

	private:
        int _pwmPitch;
		int _pwmGrip;
        Servo pitch; //declare a pitch  object
        Servo gripper; //declare a gripper object
};




#endif

Source:

#include "Arduino.h"
#include <Servo.h>
#include "Arm.h"

Arm :: Arm(int pwmPitch, int pwmGrip)
{
_pwmPitch=pwmPitch;
_pwmGrip=pwmGrip;

int pos1=90;
int pos2=180;
}

void Arm :: Attach(){
gripper.attach(_pwmGrip);
  pitch.attach(_pwmPitch);
}


void Arm :: GripperGo(int pos1)
{
  //gripper->attach(_pwmGrip);
  if(pos1 > gripper.read())
  {
    while(gripper.read() != pos1)
    {
      gripper.write(gripper.read()+1);
      delay(10);    
    }
  }
  if(pos1 < gripper.read())
  {
   while(gripper.read() != pos1)
    {
      gripper.write(gripper.read()-1);
      delay(10);    
    } 
  }
}

void Arm :: PitchGo(int pos2)
{
  if(pos2 > pitch.read())
  {
    while(pitch.read() != pos2)
    {
      pitch.write(pitch.read()+1);
      delay(10);    
    }
  }
  if(pos2 < pitch.read())
  {
   while(pitch.read() != pos2)
    {
      pitch.write(pitch.read()-1);
      delay(10);    
    } 
  }
}

Thank you!

Hi, please post all your sketch, also what pins are you using for the servo.
Please tell us what your sketch is supposed to do.
What are you inputting, what you want the controller to do, what you want output.

Tom..... :slight_smile:

PS, one of the hardest things to do is read someone else's sketch, the more info about what you are trying to accomplish the better.

Hello Tom!
I am using PWM pins to run my servos, pins 2 and 3. I have a robot, and im trying to drive it around and move an arm actuated by two servos.

Here is a sketch

#include "MotorBoard.h";   //loads a custom library to set up drive motors
#include "Arm.h"     //loads a custom library to provide easy arm control command
#include <Servo.h>        //loads a servo library, also included inside Arm.h so this line is redundant and here as a reminder

// --- drive motor control40
#define ph1 36     // phase  1 from motor control board. Right motor.
#define en1 5     // enable 1 from motor control board. Right motor. (pwm)
#define ph2 34     // phase  2 from motor control board. Left  motor.
#define en2 7     // enable 2 from motor control board. Left  motor. (pwm) //pin 4 is stuck in on
// --- arm servo control
#define PitchPin 2 //servo motor actuating arm
#define GripPin  3 //servo motor actuating gripper 10

MotorBoard Drive(ph1,en1,ph2,en2); //sets up motor drive, calling class MotorBoard to create an object "Drive"
Arm Arm(PitchPin,GripPin);   //sets up arm control, calling class DiggerARm  to create an object "Arm"

void setup(){
Serial.begin(9600); //Establishes Serial communication at a specified baud rate. This can be moved inside of the Ant Comm class
Serial.println("Ready"); //debug line
Arm.Attach();       //hook up servos to pwm pins
Serial.println("foo ");
Arm.PitchGo(110);    //puts an arm in a default configuration. Arm is extended and parallel to ground
Serial.println("foo 1");
Arm.GripperGo(90);  //opens up a gripper. Closed position is 180
Serial.println("foo 2");

DriveAroundAndDoOtherStuff(); //more stuff and code, but the program never makes it this far
}
void loop(){
//more stuff here
}

When i run the code on Mega, Uno, or Fio, everything works fine. When I run the code on Due, here is what happens:
I pop up serial monitor, I see "Ready", i see "foo", and but not foo1. I do have my servo pins giving out pwm signal, checked on oscilloscope.

UPDATE:
I just put more print-to-consolue statements in my Arm.cpp file in PitchGo() method. Here is a section of that code (full code is posted in a previous post)

void DiggerArm :: PitchGo(int pos2)
{
Serial.println("f1");
  if(pos2 > pitch.read())
  {
Serial.println("f2");
  
    while(pitch.read() != pos2)
    {
      pitch.write(pitch.read()+1);
      delay(10);
Serial.println("f3");	  
    }
  } ......... [code continues ]

f1 prints, f2 prints, and f3 seems to be printing forever and ever and ever.... any ideas why? Something must be wrong with pitch.read()...

I think I solved it.

It appears that read() method in a Servo.h library is not working correctly with Arduino Duo
instead of relying on .read() method to get a last servo position, i defined a private variable to track the last angle and increment/decrement it in for loops, and everything seems to run like it should!

Thanks for the support!