Hello, I need with one command lets say character "V" to make two movements, moveTo(2000) and then moveTo(0) again (I have performed homing before sending the command). However as it stands, it only takes the second command and doesn't move at all. I am thinking it has something to do with not being in the void loop. Any help ? The first movement comes from Grab() and the second from Z_GO_BACK(). If I use the serial again with "B" it works as it should however I need it to be doing the 2 movements automatically without me intervening. The code is this
#include <AccelStepper.h>
char receivedCommand;
int receivedCell;
int travel_z = 2000;
bool newData, runallowedz = false; // booleans for new data from serial, and runallowed flag
AccelStepper stepper_z(1, 4, 5);// direction Digital 9 (CCW), pulses Digital 8 (CLK)
#define home_switchz 10 //Limit switch z
void setup() {
Serial.begin(9600); //define baud rate
//setting up some default values for maximum speed and maximum acceleration
Serial.println("Default speed: 400 steps/s, default acceleration: 800 steps/s^2.");
//Stepper z
stepper_z.setMaxSpeed(400); //SPEED = Steps / second
stepper_z.setAcceleration(800); //ACCELERATION = Steps /(second)^2
stepper_z.disableOutputs(); //disable outputs
pinMode(home_switchz, INPUT_PULLUP);
}
void loop() {
//Constantly looping through these 2 functions.
//We only use non-blocking commands, so something else (should also be non-blocking) can be done during the movement of the motor
checkSerial(); //check serial port for new commands
RunTheMotorz(); //function to handle the motor z
}
void checkSerial() //function for receiving the commands
{
if (Serial.available() > 0) //if something comes from the computer
{
receivedCommand = Serial.read(); // pass the value to the receivedCommad variable
newData = true; //indicate that there is a new data by setting this bool to true
if (newData == true) //we only enter this long switch-case statement if there is a new command from the computer
{
switch (receivedCommand) //we check what is the command
{
case 'V': //R uses the moveTo() function of the AccelStepper library, which means that it moves absolutely to the current position.
Grab();
Z_GO_BACK();
break;
case 'B':
Z_GO_BACK();
break;
case 'L': //L: Location
runallowedz = false; //we still keep running disabled
stepper_z.disableOutputs(); //disable power
Serial.print("Current location of the motor Z: ");//Print the message
Serial.println(stepper_z.currentPosition()); //Printing the current position in steps.
break;
case 'H': //H: Homing
runallowedz = true;
Serial.println("Homing"); //Print the message
GoHomez();// Run the function
break;
default:
break;
}
}
//after we went through the above tasks, newData is set to false again, so we are ready to receive new commands again.
newData = false;
}
}
void Grab()
{
runallowedz = true;
stepper_z.setMaxSpeed(400);
stepper_z.moveTo(2000);
}
void Z_GO_BACK()
{
runallowedz = true;
stepper_z.setMaxSpeed(400);
stepper_z.moveTo(0);
}
void RunTheMotorz() //function for the motor
{
if (runallowedz == true)
{
stepper_z.enableOutputs(); //enable pins
stepper_z.run(); //step the motor (this will step the motor by 1 step at each loop)
}
else //program enters this part if the runallowed is FALSE, we do not do anything
{
stepper_z.disableOutputs(); //disable outputs
return;
}
}
void GoHomez(){
long initial_homingz=1;
stepper_z.setMaxSpeed(100.0);
stepper_z.setAcceleration(100.0);
while (digitalRead(home_switchz)) { // Make the Stepper move CCW until the switch is activated
stepper_z.moveTo(initial_homingz); // Set the position to move to
initial_homingz++; // Decrease by 1 for next move if needed
stepper_z.run(); // Start moving the stepper
delay(5);
}
stepper_z.setCurrentPosition(0); // Set the current position as zero for now
stepper_z.setMaxSpeed(100.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper_z.setAcceleration(100.0); // Set Acceleration of Stepper
initial_homingz=-1;
while (!digitalRead(home_switchz)) { // Make the Stepper move CW until the switch is deactivated
stepper_z.moveTo(initial_homingz);
stepper_z.run();
initial_homingz--;
delay(5);
}
stepper_z.setCurrentPosition(0);
}
Thank you in advance