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


hello! using gws s35 std continuous rotation motor wore out load had. swapped parallax motor , runs great - problem new motor doesn't respond code - runs continuously instead of start/stop/pause have in code . i'm @ loss pay attention code. i'm assuming need change in language (i.e. parallax requires different language s35)? serial monitor shows code doing supposed (i.e. no weird values) motor doesn't respond of commands. new game , troubleshooting skills limited. welcome! thank-you! :)

i've included code works great old motor (no problems). note: code light gets written out bool declaration @ start. 
code: [select]
#include "servo.h"

// if putting code in light piece, set true
// if putting code in motor piece, set false
bool lightpiece = false;


// 20 , 80 motor
// 10 , 25 light
int trigger = 35; // how close needs to set off
int resettrigger = 80; // arbitrary , matters resetting after has
// triggered maxrepeat times
//  above trigger

// variables declared here global , accesible loops(scopes) here down
// set loop or sensor variables 0 unless antother default needed, why?
// because ensures set , not 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 adjusting find stop-point.
const int motorfwd    = 45; // or 135 opposite direction

// led code
const int led = 9; //led on pin 9
const int pwpinled = a0; // sensor on analog 0

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

// constants
int maxrepeat = 5; // maximum number of times want set off before shutting down
int nrepeat = 0; // counter how many times has been set off

void setup()
{
  if (lightpiece)
  {
    setuplight();
  }
  else
  {
    setupmotor();
  }
  delay(3000);
  // gives 3 seconds posterior out of way of sensor before calibrate
  // default readings hard coded

}

/////////////////////////////
// setup motor
////////////////////////////
void setupmotor()
{
  exmotor.write (motorstop);
  exmotor.attach (motorpin);
  serial.begin(9600);
  serial.println("arduino duemilanove atmega328 starting");
}

/////////////////////////////
// setup light
///////////////////////////
void setuplight()
{
  pinmode(led, output);
  serial.begin(9600);
  serial.println("arduino duemilanove atmega328 starting");
}


void loop()
{
  samples=0;
  // read nsamp number of samples sensor , average them when done
  // avoid being set off couple bad values
  int totalsamplestaken = 0;
  (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 room empty, , there close enough trigger
  //  trigger 5 times in row
  if (empty)
  {
    if(sdist < trigger)
    {
      serial.print("triggered: ");
      serial.println(sdist);
      if (nrepeat < maxrepeat)   // run 5 times 
      {
        // run motor or turn on light
        if (lightpiece)
        {
          changelight(true);
        }
        else
        {
          changemotor(true);
        }
        nrepeat++;
        serial.print("repeat: ");
        serial.println(nrepeat);
      }
      else // have reached our maximum repeat, turn off
      //  until moves farther away
      {
        if (lightpiece)
        {
          changelight(false);
        }
        else
        {
          changemotor(false);
        }
        serial.println("max repeats reached");
        serial.println("off"); 
        empty = false;
      }
    }
    else // there no trigger, reset our maximum count
    {
      nrepeat = 0;
    }
  }

  // if room full, test see if person has moved away
  if (!empty)
  {
    serial.println("full");
    if (sdist > resettrigger)
    {
      empty = true;
      nrepeat = 0;
    }
  }
  delay(1000);  //checks sensor every 1 second person/object 
}

/////////////////////////////
// turn light
// on , 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);
  }
}


/////////////////////////////
// turn motor
// on , 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();
  }
}


quote
const int motorstop   = 90; // may need adjusting find stop-point.


did try adjust stop pulse width line?


Arduino Forum > Using Arduino > Motors, Mechanics, and Power (Moderator: fabioc84) > Different Setup/code for a Parallax Servo vs a GWS s35 STD?


arduino

Comments

Popular posts from this blog

opencv3, tbb and rasp pi 2 - Raspberry Pi Forums

small ethernet problem - Raspberry Pi Forums

Multithumb configuration params not working? - Joomla! Forum - community, help and support