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("#");
}