trying to set a home position. on a contentious run servo

i am working on a color filter wheel for astronomy. have built a few other projects that work fine but this one is driving me nuts.
i am trying to get set it(goto) to home position when i first turn on the unit and set current filter to 1. i am using a contentious run servo for motor. and a small switch that when it goes low it sets home position. so i only want it to run 1 time at start up. been at it a few days no with no luck. here is the code i have now. might take me a day or two to replay because of work thanks for looking.

robert

// ducks filter wheel 

#include <Servo.h> 


Servo myservo;
int CurrentFilter = 0; //current filter pisition
int  HomePin = 2;  //home position switch 
int PositionPin = 3; //position switch for changing filters
int homePos;

void setup()
{

  Serial.begin(19200);
  Serial.flush();
  pinMode(HomePin, INPUT);
  pinMode(PositionPin, INPUT);
  myservo.attach(9);
  myservo.writeMicroseconds(1498);
  boolean homePos = true;
  
  if (digitalRead(HomePin) == HIGH){
    boolean homePos = false;
  }
  else{
    boolean homePos = true;
  }
  
}
void loop()
{
  String cmd;

  if (Serial.available() >0) {
    cmd = Serial.readStringUntil('#');
    if (cmd=="GETFILTER") {
      Serial.print(CurrentFilter);
      Serial.println("#");
    }
    else if (cmd=="FILTER1") MoveFilter(1);
    else if (cmd=="FILTER2") MoveFilter(2);
    else if (cmd=="FILTER3") MoveFilter(3);
    else if (cmd=="FILTER4") MoveFilter(4);
    else if (cmd=="FILTER5") MoveFilter(5);
  }
  
  
   while (homePos != true && digitalRead(HomePin) != HIGH){
    homeset();
   
  }
  
}
void MoveFilter(int pos)
{
  delay(2000);
  CurrentFilter = pos;
  Serial.println("0#");

}


void homeset()
{
myservo.write(95);
delay (300);

//if (digitalRead(HomePin) != LOW)
  myservo.writeMicroseconds(1498);
//  boolean homePos = true;
  return;

}

If you want help you need to tell us what your code is currently doing.
Do you know how to draw and algorithm diagram ?
If not, try listing the intended behaviour of the program with a combination of queries and statements.
ie:
Move servo to "home" position
Check homebutton=>High?==>do this
LOW==> do this
etc.

maybe writing it out with words will help troubleshoot your code.

I suspect you mean CONTINUOUS and not CONTENTIOUS.

Continuous rotation servos don't have a home position.

I can think of two options.

(A) install a limit switch that can be used to detect whe the servo has rotated to a particular position. I have done this using a second servo which can lift the "switch" out of the way. My "switch" is just a piece of metal that is contacted by another piece of metal on the continuous rotation platform to make an electrical contact.

(B) if your project will work with a max of about 3 revolutions of the servo you could use a sail winch servo which does retain its positioning ability.

...R

ok ill try this again. the set up is a 5 position position filter wheel driven by a continuous run servo(pin9) with a switch that goes LOW (pin2)when it is at filter one. what i am trying to get is

at power up (only!)
run servo motor till HomePin (pin2) goes low
then set CurrentFilter to 1

i have removed all my code that tried to do this so you can see the bassic code. and maybe come up with something that will work. thanks.

// ducks filter wheel 

#include <Servo.h> 


Servo myservo;
int CurrentFilter = 0; //current filter pisition
int  HomePin = 2;  //home position switch 
int PositionPin = 3; //position switch for changing filters

void setup()
{

  Serial.begin(19200);
  Serial.flush();
  pinMode(HomePin, INPUT);
  pinMode(PositionPin, INPUT);
  myservo.attach(9);
  myservo.writeMicroseconds(1498);
  
}
void loop()
{
  String cmd;

  if (Serial.available() >0) {
    cmd = Serial.readStringUntil('#');
    if (cmd=="GETFILTER") {
      Serial.print(CurrentFilter);
      Serial.println("#");
    }
    else if (cmd=="FILTER1") MoveFilter(1);
    else if (cmd=="FILTER2") MoveFilter(2);
    else if (cmd=="FILTER3") MoveFilter(3);
    else if (cmd=="FILTER4") MoveFilter(4);
    else if (cmd=="FILTER5") MoveFilter(5);
  }
  
     
    
}
void MoveFilter(int pos)
{
  delay(2000);
  CurrentFilter = pos;
  Serial.println("0#");

}


void homeset()
{


}

Sorry, I didn't see you already have a switch.

I think you need something like this pseudo code

switchVal = digitalRead(homePin);

while (switchVal == HIGH) { // assumes LOW when contacts close
    myservo.write(slow servo speed value);
    switchVal = digitalRead(homePin);
}
myservo.write(stop value);

This code assumes you don't care which side of the homePosition the servo is already at - it might go 355 degrees when 5 deg backwards would have been enough.

If very accurate location is important I think I would use a "switch" that was strong enough to halt the motion until the servo is stopped. That's the way mine works.

...R

ky1duck:
i am working on a color filter wheel for astronomy. have built a few other projects that work fine but this one is driving me nuts.
i am trying to get set it(goto) to home position when i first turn on the unit and set current filter to 1. i am using a contentious run servo for motor. and a small switch that when it goes low it sets home position. so i only want it to run 1 time at start up. been at it a few days no with no luck. here is the code i have now. might take me a day or two to replay because of work thanks for looking.

robert

Continuous rotation servos are not servos, they are small DC motors with a
speed control built-in.

You need a proper servo that will go to a set position on command, not a
continuous rotation one.

Robin2

Thanks I will try that when I get back home thur.

Mark t

The reason I an using the servo is light weight and has gear box. Weight is a big problem with astromony equipment also the motor has to make too many revolutions to use a regular servo. That is also the same reason I didn't use a stepper motor.

Thanks
Robert

Sail winch servos can usually make 3-6 turns, but most are large for operating RC sail boat sails.

A stepper would be about perfect for this.

polyglot:
A stepper would be about perfect for this.

I thought about a stepper but then again there is the weight issue and would still need to use home and position switches because of slippage and missed steps and even more code. Trying to keep this as simple as possible as a diy project for others.

Robert

I thought about a stepper but then again there is the weight issue and would still need to use home and position switches because of slippage and missed steps and even more code.

I've never tinkered with stepper motors, but I the ones below don't look too large. A home limit switch should be easy to set up. You might even just make a hard stop to stop the motor movement and use that as a start calibration point.

http://www.ebay.com/itm/5V-Stepper-Step-Motor-Driver-Board-ULN2003-28BYJ-48-For-Arduino-High-Quality-/271473069262?pt=LH_DefaultDomain_2&hash=item3f350e4cce

http://www.ebay.com/itm/arduino-L298N-stepper-motor-drive-28BYJ-48-stepper-motor-drive-circuit-C-/370880546505?pt=LH_DefaultDomain_2&hash=item565a3406c9

Robin2

robin2...... thanks the code you posted works great thanks again.

anyone? using the code i have here set up as void homeset() will only run at startup Right?

/ ducks color filter wheel Ver.1 

#include <Servo.h> 


Servo myservo;
int CurrentFilter = 0; //current filter pisition
int  HomePin = 2;  //home position switch 
int PositionPin = 3; //position switch for changing filters
int homePos;
int switchVal;


void setup()
{

  Serial.begin(19200);
  Serial.flush();
  pinMode(HomePin, INPUT);
  pinMode(PositionPin, INPUT);
  myservo.attach(9);
  myservo.writeMicroseconds(1501);
  boolean homePos = true;
  homeset(); // runs once till home position is set. 


}
void loop()
{
  String cmd;

  if (Serial.available() >0) {
    cmd = Serial.readStringUntil('#');
    if (cmd=="GETFILTER") {
      Serial.print(CurrentFilter);
      Serial.println("#");
    }
    else if (cmd=="FILTER1") MoveFilter(1);
    else if (cmd=="FILTER2") MoveFilter(2);
    else if (cmd=="FILTER3") MoveFilter(3);
    else if (cmd=="FILTER4") MoveFilter(4);
    else if (cmd=="FILTER5") MoveFilter(5);
  }




}
void MoveFilter(int pos)
{
  delay(2000);
  CurrentFilter = pos;
  Serial.println("0#");

}


void homeset()   // used to set the home position when first powered up.
// thanks to Robin2 for this section

{
  int switchVal;

  switchVal = digitalRead(HomePin);

  while (switchVal == LOW) { // assumes LOW when contacts close
    myservo.write(1400);
    switchVal = digitalRead(HomePin);
  }
  myservo.write(1501);
  CurrentFilter = (1);


}

ky1duck:
anyone? using the code i have here set up as void homeset() will only run at startup Right?

Yes.

...R

after looking around i decided to try a stepper motor and got it to work using the following code. just would like to know if there is a better way (simpler)to write it. for moving the stepper from one place to another. thanks for the help.

robert

also is there a way to get it to just montor the serial port till it gets a command the go move move the stepper

// ducks color filter wheel Ver.1 

 
#include <AccelStepper.h>


int  HomePin = 8;          //home position switch 
int homePos;
int switchVal;
int CurrentFilter = 0; //current filter pisition
int buttonState = 0;       // current state of the button
int lastButtonState = 0;   // previous state of the button
AccelStepper stepper;
 
String cmdString;


void setup()
{

  Serial.begin(19200);
  Serial.flush();
  pinMode(HomePin, INPUT);
 // pinMode(PositionPin, INPUT);
  stepper.setMaxSpeed(400);
  stepper.setAcceleration(100);
  homeset();               // runs once till home position is set. 

}

void loop()
{
 String cmd;

  if (Serial.available() >0) {
    cmd = Serial.readStringUntil('#');
    if (cmd=="GETFILTER") {
      Serial.print(CurrentFilter);
      Serial.println("#");
    }
    else if (cmd=="FILTER0")
      { 
        stepper.moveTo(1);
        stepper.run();
        stepper.runToPosition(); 
        stepper.disableOutputs ();
        CurrentFilter = (0);
      //  Serial.print(CurrentFilter);
        Serial.println("#");
       // delay(4000);
      }
       
    else if (cmd=="FILTER1") 
    { 
        stepper.moveTo(409);
        stepper.run();
        stepper.runToPosition(); 
        stepper.disableOutputs ();
        CurrentFilter = (1);
       // Serial.print(CurrentFilter);
        Serial.println("#");
      //  delay(4000);
      }
    
    else if (cmd=="FILTER2")
    { 
        stepper.moveTo(819);
        stepper.run();
        stepper.runToPosition(); 
        stepper.disableOutputs ();
        CurrentFilter = (2);
       // Serial.print(CurrentFilter);
        Serial.println("#");
       // delay(4000);
      }
      
    else if (cmd=="FILTER3") 
    { 
        stepper.moveTo(1229);
        stepper.run();
        stepper.runToPosition(); 
        stepper.disableOutputs ();
        CurrentFilter = (3);
       // Serial.print(CurrentFilter);
        Serial.println("#");
      //  delay(4000);
      }
      
    else if (cmd=="FILTER4")
    { 
        stepper.moveTo(1638);
        stepper.run();
        stepper.runToPosition(); 
        stepper.disableOutputs ();
        CurrentFilter = (4);
       // Serial.print(CurrentFilter);
        Serial.println("#");
     //   delay(4000);
      }
  }
  
}

void homeset()   // used to set the home position when first powered up.
                 // thanks to Robin2 for this section
{
  int switchVal;
  switchVal = digitalRead(HomePin);
  while (switchVal == LOW) { // assumes LOW when contacts close
    stepper.run();
    stepper.setSpeed(300);
    switchVal = digitalRead(HomePin);
  }
  stepper.stop();
  stepper.setCurrentPosition(0);
  stepper.disableOutputs ();
  CurrentFilter = (0);
    Serial.print(CurrentFilter);
    Serial.println("#");

}

Your homeset() function will only run once, in setup().

But I fail to see why you are using a continuous servo at all. Setting the home position isn't really going to help you much, why don't you manipulate your color wheel with a regular position-controlled servo ?

ky1duck:
also is there a way to get it to just montor the serial port till it gets a command the go move move the stepper

I haven't studied your code very closely but it seems to me that is what it is already doing?

Maybe I don't see what you have in mind.

...R

michinyon:
Your homeset() function will only run once, in setup().

But I fail to see why you are using a continuous servo at all. Setting the home position isn't really going to help you much, why don't you manipulate your color wheel with a regular position-controlled servo ?

i am not using a servo any more i took the advice from here and found a small stepper to use. the homeset function is onl suppose to rune once and it is just to make sure that the filter wheel is at position 0 and the step count is at 0 also.

the project is a 5 position color filter wheel. that when it gets a command from the computer it will move the wheel to that position. the problem i seem to be having now is that it seem to miss some of the commands that are sent from the computer. so that some times it moves and others it dosent.

Your system uses the commands "FILTER0", "FILTER1" etc to control your motor.

But the only useful information in those commands is the 0 and the 1. The letters "FILTER" are just a waste of time.

I would modify the code so it just needs to send (and receive) a single number or character to determine what action to take.

Then it will probably be easier to figure out why some commands are not being interpreted properly.

...R