Hi,
Currently I have a working steppermotor with a disc on top. underneath the disc are legs in eight directions and the ) position has an extra leg which is added for the homing function via light barrier.
At the momentent I have added the commands the motor needs to be able to do like home (h) return to the 0 (0) and move to position XX (pXX).
during the movement to postion XX i would like to be able to send another command to see what the status is (the red encased part in the flow chart).
My idea was to use the the leg of the of each of the positions of the disc and another light barrier.
But I dont know where to start to tackle this problem. Any help or insight into finding a solution to this challenge would be realy appreciated.
my current code is posted down below.
String sdata = ""; // Initialised to nothing.
byte test, start;
#include "FastAccelStepper.h"
#include "AVRStepperPins.h" // Only required for AVR controllers
#include "StepperISR.h"
#define dirPinStepper 2 //stepper motor connection cable 1
#define enablePinStepper 4 //stepper motor connection cable 2
#define stepPinStepper 9 //stepper motor connection cable 3
#define home_switch 12 //light gate
const int BUTTON_PIN = 11;
int counter = 0;
char rx_byte = 0;
// If using an AVR device use the definitons provided in AVRStepperPins
// stepPinStepper1A
//
// or even shorter (for 2560 the correct pin on the chosen timer is selected):
// stepPinStepperA
FastAccelStepperEngine engine = FastAccelStepperEngine();
FastAccelStepper *stepper = NULL;
long TravelX; // Used to store the X value entered in the Serial Monitor
int move_finished = 1; // Used to check if move is completed
long initial_homing = -1; // Used to Home Stepper at startup
void setup(void) {
Serial.begin(9600);
Serial.println("Command Interpreter");
engine.init();
stepper = engine.stepperConnectToPin(stepPinStepper);
if (stepper) {
stepper->setDirectionPin(dirPinStepper);
stepper->setEnablePin(enablePinStepper, true);
stepper->setAutoEnable(true);
stepper->setSpeedInHz(500); // 500 steps/s
stepper->setAcceleration(100); // 100 steps/s²
}
pinMode(BUTTON_PIN, INPUT_PULLUP);
Serial.begin(9600);
Serial.print("boot up...");
Serial.println("");
pinMode(home_switch, INPUT_PULLUP);
}
void loop(void) {
byte ch;
String valStr;
int val;
if (Serial.available()) {
ch = Serial.read();
sdata += (char)ch;
if (ch == '\r') { // Command received and ready.
sdata.trim();
// Process command in sdata.
switch (sdata.charAt(0)) {
// case '+': obsolete
// {
// //Serial.print("add step. carousel in position: ");
// Serial.println(counter);
// //Serial.println(rx_byte);
// counter++;
// delay(500);
// }
// break;
case 'h':
{
Serial.print("Stepper is Homing . . . . . . . . . . . ");
while (!digitalRead(home_switch)) { // Make the Stepper move CCW until the switch is activated
stepper->moveTo(initial_homing); // Set the position to move to
initial_homing--; // Decrease by 1 for next move if needed
stepper->runForward(); // Start moving the stepper
delay(1);
}
stepper->setCurrentPosition(0); // Set the current position as zero for now
stepper->setSpeedInHz(100); // 100 steps/s
stepper->setAcceleration(100); // 100 steps/s²
initial_homing = 1;
while (digitalRead(home_switch))
{ // Make the Stepper move CW until the switch is deactivated
stepper->moveTo(initial_homing);
initial_homing++;
stepper->runForward();
delay(1);
}
stepper->setCurrentPosition(0);
Serial.println("Homing Completed");
Serial.println("");
stepper->setSpeedInHz(500); // 2000 steps/s
stepper->setAcceleration(100); // 100 steps/s²
}
break;
case 'p':
if (sdata.length() > 1) {
valStr = sdata.substring(1);
val = valStr.toInt();
}
Serial.print("move to ");
Serial.println(val);
stepper->moveTo(val);
break;
default: Serial.println(sdata);
} // switch
sdata = ""; // Clear the string ready for the next command.
} // if \r
} // available
}