Different Setup/code for a Parallax Servo vs a GWS s35 STD?

Hello! I was using the GWS S35 STD continuous rotation motor but eventually it wore itself out with the load that I had. I swapped it for a Parallax motor and it runs great - the only problem is that the new motor doesn’t respond to my code - it just runs continuously instead of the start/stop/pause that I have in my code . So now I’m at a loss about what do to get it to pay attention to the code. I’m assuming I need to change something in my language (i.e. the parallax requires a different language than the S35)? The serial monitor shows that the code is doing what its supposed to (i.e. no weird values) but the motor doesn’t respond to any of the commands. I am fairly new to this game and my troubleshooting skills are fairly limited. Any help is welcome! Thank-you! :slight_smile:

I’ve included my code which works great with the old motor (no problems). Note: the code for the light gets written out with the bool declaration at the start.

#include "Servo.h" 

// if you are putting this code in the light piece, set to true
// if you are putting this code in the motor piece, set to false
bool lightPiece = false;


// 20 and 80 for motor
// 10 and 25 for light
int trigger = 35; // This is how close someone needs to be to set off
int resetTrigger = 80; // This is arbitrary and only matters for resetting after someone has 
// triggered maxRepeat times 
//  could be anything above trigger

// variables declared here will be global and accesible by all loops(scopes) from here down
// I always set loop or sensor variables to zero unless antother default is needed, why? 
// Because it ensures that they are set to something initially and not some random value 
// in memory.
// Motor code
Servo exMotor;
const int motorPin    = 9;  // motor on pin 9 
const int PWPinMotor  = 5;  // sensor on analog 5
const int motorStop   = 90; // may need some adjusting to find stop-point.
const int motorFwd    = 45; // or 135 for the opposite direction

// LED code
const int LED = 9; //LED on pin 9
const int PWPinLED = A0; // sensor on analog 0

// code for logic
int i=0;
int samples = 0;
int sdist = 0;      // sensor distance, raw value returned fron the sensor
int nsamp = 20;   // number of samples to read an average
int pause = 3000;  // pause for 3 seconds before reading any more values, should delay constant going off if standing in front of sensor
bool empty = true;

// Constants
int maxRepeat = 5; // The maximum number of times you want to set off before shutting down
int nRepeat = 0; // A counter for how many times it has been set off

void setup()
{
  if (lightPiece)
  {
    setupLight();
  }
  else
  {
    setupMotor();
  }
  delay(3000); 
  // gives you 3 seconds to get your posterior out of the way of the sensor before I calibrate
  // default readings are hard coded 

}

/////////////////////////////
// Setup the motor
////////////////////////////
void setupMotor()
{
  exMotor.write (motorStop);
  exMotor.attach (motorPin);
  Serial.begin(9600);
  Serial.println("Arduino Duemilanove ATmega328 Starting");
}

/////////////////////////////
// Setup the light
///////////////////////////
void setupLight()
{
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
  Serial.println("Arduino Duemilanove ATmega328 Starting");
}


void loop()
{ 
  samples=0;
  // read nsamp number of samples from the sensor and average them when done
  // this will avoid being set off by a couple bad values
  int totalSamplesTaken = 0;
  for (i=0; i<nsamp; i++)
  {
    if (lightPiece)
    {
      sdist = analogRead(PWPinLED);
    }
    else
    {
      sdist = analogRead(PWPinMotor);
    }
    samples+=sdist; 
  } 
  sdist = samples/nsamp;


  Serial.println();
  Serial.print("Sensor Distance sampled (sdist) ");
  Serial.println(sdist, DEC);


  // if the room is empty, and there is someone close enough to trigger
  //  but only trigger five times in a row
  if (empty)
  {
    if(sdist < trigger)
    {
      Serial.print("Triggered: ");
      Serial.println(sdist);
      if (nRepeat < maxRepeat)   // run it five times  
      {
        // Run the motor or turn on the light
        if (lightPiece)
        {
          changeLight(true);
        }
        else
        {
          changeMotor(true);
        }
        nRepeat++;
        Serial.print("Repeat: ");
        Serial.println(nRepeat);
      } 
      else // we have reached our maximum repeat, turn it all off 
      //  until someone moves farther away
      {
        if (lightPiece)
        {
          changeLight(false);
        }
        else
        {
          changeMotor(false);
        }
        Serial.println("Max repeats reached");
        Serial.println("OFF");  
        empty = false;
      }
    }
    else // there was no trigger, so reset our maximum count
    {
      nRepeat = 0;
    }
  }

  // if the room is full, test to see if the person has moved away
  if (!empty) 
  {
    Serial.println("Full");
    if (sdist > resetTrigger)
    {
      empty = true;
      nRepeat = 0;
    }
  }
  delay(1000);  //checks sensor every 1 second for person/object  
}

/////////////////////////////
// This will turn the light
// on and off depending on 
// true false passed in
////////////////////////////
void changeLight(bool change)
{
  if (change)
    {
    Serial.println("ON");
    digitalWrite(LED, HIGH); 
    delay(2000);
    digitalWrite (LED, LOW);
    }
  else
  {
    Serial.println("OFF");        
    digitalWrite(LED, LOW);
  }
}


/////////////////////////////
// This will turn the motor
// on and off dependon on 
// true false passed in
////////////////////////////
void changeMotor(bool change)
{
  if (change)
  {
    Serial.println("ON");
    exMotor.attach (motorPin); 
    exMotor.write (motorFwd);   
    delay(2000);
    exMotor.write (motorStop);
  }
  else
  {
    exMotor.detach();
  }
}

const int motorStop = 90; // may need some adjusting to find stop-point.

Did you try to adjust the stop pulse width with this line?

Basic continuous rotation servo calibration.

When making a continuous rotation servo, one should "calibrate" the servo for best positioning. One way is to send the servo a 90 1500us position command and then carefully take the servo apart for the mod and secure the pot shaft with out moving the shaft. Another way is when the servo is apart, power it up and send it a 1500us command, then carefully adjust the pot until the motor stops, then secure the pot shaft in place.

Thanks everyone, I appreciate the feedback! With the help of a programming buddy I changed my Stop and Fwd variables to 1520 and 1300 respectively and it now functions. I am hopeful that eventually I will stop stumping myself completely but we'll see.