hello, i am trying to make a automated cocktail bar with an arduino and 2 nema 17 steppers, i tryed different sketches but nothing was like it needed to be with my components so i make a mix from alot of different sketches, after a long time trying to fix all the errors i could compile the code without errors.
every axis (X and Z) can be homed now, and i can let the axis move with an serial input, but i always get an error when i try the command " F2 H2500 W3000" also command "F" gives me the same result even with a number after it
i get the answer "Hold and wait duration parameters cannot be lower than or equal to 0"
so i think my problem is in the "void pour" section.
is there somebody who can help me on the way? and can explain why it went wrong?
here is the sketch;
#include "AccelStepper.h"
#include "Configuration.h"
String serialBuffer = "";
String actions[TOTAL_ACTIONS];
int counter = 0;
int lastIndex = 0;
AccelStepper stepperX(X_INTERFACE_TYPE, X_STEP_PIN, X_DIR_PIN); // Define a stepper and the pins it will use
AccelStepper stepperZ(Z_INTERFACE_TYPE, Z_STEP_PIN, Z_DIR_PIN); // Define a stepper and the pins it will use
void setup() {
Serial.begin(9600); // Serial port for debugging
//maestroSerial.begin(9600); // Servo controller
//Serial2.begin(9600); // Bluetooth module
stepperX.setMaxSpeed(X_MAX_SPEED); // Sets the maximum speed the X axis accelerate up to
stepperZ.setMaxSpeed(Z_MAX_SPEED); // Sets the maximum speed the X axis accelerate up to
pinMode(X_ENDSTOP_PIN, INPUT_PULLUP); // Initialize endstop pin with the internal pull-up resistor enabled
pinMode(Z_ENDSTOP_PIN, INPUT_PULLUP); // Initialize endstop pin with the internal pull-up resistor enabled
homeXAxis(); // Return the X axis to it's home position at the startup
delay(5000); // waits for a second
homeZAxis(); // Return the X axis to it's home position at the startup
}
void homeXAxis() {
int endStopState = digitalRead(X_ENDSTOP_PIN);
while (endStopState == HIGH) {
stepperX.moveTo(100);
stepperX.setSpeed(X_HOME_SPEED);
stepperX.runSpeed();
endStopState = digitalRead(X_ENDSTOP_PIN);
}
stepperX.moveTo(stepperX.currentPosition() - 50);
while (stepperX.distanceToGo() != 0) {
stepperX.setSpeed(X_PARK_SPEED * -1);
stepperX.runSpeed();
}
endStopState = digitalRead(X_ENDSTOP_PIN);
while (endStopState == HIGH) {
stepperX.moveTo(100);
stepperX.setSpeed(X_PARK_SPEED);
stepperX.runSpeed();
endStopState = digitalRead(X_ENDSTOP_PIN);
}
stepperX.setCurrentPosition(0);
}
void homeZAxis() {
int endStopState = digitalRead(Z_ENDSTOP_PIN);
while (endStopState == HIGH) {
stepperZ.moveTo(50);
stepperZ.setSpeed(Z_HOME_SPEED);
stepperZ.runSpeed();
endStopState = digitalRead(Z_ENDSTOP_PIN);
}
stepperZ.moveTo(stepperZ.currentPosition() - 25);
while (stepperZ.distanceToGo() != 0) {
stepperZ.setSpeed(Z_PARK_SPEED * -1);
stepperZ.runSpeed();
}
endStopState = digitalRead(Z_ENDSTOP_PIN);
while (endStopState == HIGH) {
stepperZ.moveTo(50);
stepperZ.setSpeed(Z_PARK_SPEED);
stepperZ.runSpeed();
endStopState = digitalRead(Z_ENDSTOP_PIN);
}
stepperZ.setCurrentPosition(0);
}
void loop() {
while (Serial.available() > 0) {
char ch = Serial.read();
serialBuffer += ch;
if (ch == '\n') {
for (int i = 0; i < serialBuffer.length(); i++) {
if (serialBuffer.substring(i, i + 1) == ",") {
actions[counter] = serialBuffer.substring(lastIndex, i);
lastIndex = i + 1;
counter++;
}
if (i == serialBuffer.length() - 1) {
actions[counter] = serialBuffer.substring(lastIndex, i);
}
}
for (int z = 0; z < TOTAL_ACTIONS; z++) {
if (actions[z] != "0") {
parseInput(actions[z]);
}
}
Serial.println("voidloop");
for (int y = 0; y < TOTAL_ACTIONS; y++) {
actions[y] = "0";
}
serialBuffer = "";
counter = 0;
lastIndex = 0;
serialFlush();
}
}
}
void parseInput(String input) {
input.trim();
byte command = input.charAt(0);
switch (command) {
case 'H':
homeXAxis();
homeZAxis();
break;
case 'X':
moveXTo(input);
break;
case 'Z':
moveZTo(input);
break;
case 'F':
pour(input);
break;
}
}
void moveXTo(String input) {
int pos = input.substring(1).toInt();
Serial.print("X goes to: ");
Serial.println(pos);
Serial.println(input);
if (pos < 0 && pos >= X_MAX_POS) {
stepperX.setAcceleration(X_ACCELERATION);
stepperX.moveTo(pos);
if (pos < stepperX.currentPosition()) {
stepperX.setSpeed(-100);
} else {
stepperX.setSpeed(100);
}
while (stepperX.distanceToGo() != 0) {
stepperX.run();
}
} else {
Serial.println("Position should be between -9000 and 0");
}
}
void moveZTo(String input) {
int pos = input.substring(1).toInt();
Serial.print("Z goes to: ");
Serial.println(pos);
Serial.println(input);
if (pos < 0 && pos >= Z_MAX_POS) {
stepperZ.setAcceleration(Z_ACCELERATION);
stepperZ.moveTo(pos);
if (pos < stepperZ.currentPosition()) {
stepperZ.setSpeed(-100);
} else {
stepperZ.setSpeed(100);
}
while (stepperZ.distanceToGo() != 0) {
stepperZ.run();
}
} else {
Serial.println("Position should be between -4995 and 0");
}
}
void pour(String input) {
int holdDuration = getParameterValue(input, input.indexOf("h"));
int waitDuration = getParameterValue(input, input.indexOf("w"));
int times = getParameterValue(input, input.indexOf("t"));
int count = 0;
if (holdDuration > 0 && waitDuration > 0) {
for (int i = 0; i < times; i++) {
moveZTo(String(Z_MAX_POS));
delay(holdDuration - HOLD_DELAY_IN_Z_MAX);
moveZTo(String(0));
//moveZTo(0); origineel
if (times - 1 > count) {
delay(waitDuration);
} else {
delay(DELAY_BETWEEN_INGREDIENTS);
}
count++;
}
} else {
Serial.println("Hold and wait duration parameters cannot be lower than or equal to 0");
}
}
int getParameterValue(String input, int z) {
for (int y = z + 1; y < input.length(); y++) {
if (input.substring(y, y + 1) == " ") {
return input.substring(z + 1, y).toInt();
}
if (y == input.length() - 1) {
return input.substring(z + 1, y + 1).toInt();
}
}
}
void serialFlush() {
while (Serial.available() > 0) Serial.read();
}