I'm trying to write a programm to let a stepper motor drive to two destinations if my rotary encoder reached a certain value and if a button is pressed. So far everything works but when i try to tell my stepper to do a movement once and repeat this again it wont. It will only do the process once.
#include <Button.h>
#include <AccelStepper.h>
#include <Encoder.h>
AccelStepper stepper;
Button button1(A0); // Connect your button between pin 2 and GND
int State = 0;
Encoder myEnc(A4, A5);
void setup() {
button1.begin();
stepper.setMaxSpeed(200);
stepper.setAcceleration(200);
while (!Serial) {}; // for Leos
Serial.begin(9600);
}
long oldPosition = -999;
void loop() {
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
Serial.println(newPosition);
}
if(newPosition == 0) {
digitalWrite(6, LOW);
digitalWrite(7,LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
// Spielerzahl 3 Motor dreht 120 Grad pro Spieler
if(newPosition == 8) {
digitalWrite(6, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
if (button1.pressed()) {
State = 1;
Serial.print(State);
}
while (State == 1) {
stepper.moveTo(802);
stepper.run();
if (stepper.distanceToGo() == 0) {
stepper.setCurrentPosition(0);
stepper.moveTo(802);
stepper.run();
State = 0;
Serial.print(State);
}
}
}
}
When i read my values in the Serial monitor it shows me state 0 after the first stepper movement but it just ignores the second move command.
It means that the Arduino's MCU chip does not need an extra USB-Serial chip, it can natively connect to USB without one. Leonardo and Pro Micro are examples, plus many of the more modern models.
You don't have the infinite loop problem @anon92864395 was hinting at, your description of the problem seems to prove that.
"Elegoo" is a make (a clone manufacturer), not a model, so that doesn't tell us much unfortunately.
No, it just means that @anon92864395 was not correct about the problem you are facing. It's something else.
(If you had been using a Leonardo, Pro-micro or a clone of those models, or any other model with native USB, then @anon92864395 may well have been correct about the infinite loop. But from your description of the problem, we know that wasn't it).
It would help if you could Auto-Format your code and re-post it. Correct indentation, you may have heard, does not affect how your sketch runs, it won't fix anything. But it sure makes it a hell of a lot easier for humans to spot an error in the code!
Sorry that was my error after the first if statement it does get closed i forgot to add that because ive shorten the code for you guys to make it read easier.
I've added something to the code which is working fine, kind of tried to find a way around my problem but the actuall probelm still stays. Here is my full code now.
#include <Button.h>
#include <AccelStepper.h>
#include <Encoder.h>
#include <Servo.h>
Servo myservo;
int pos = 0;
AccelStepper stepper;
Button button1(A0);
int State = 0;
Encoder myEnc(A4, A5);
void setup() {
myservo.attach(A1);
button1.begin();
stepper.setMaxSpeed(200);
stepper.setAcceleration(200);
while (!Serial) {}; // for Leos
Serial.begin(9600);
}
long oldPosition = -999;
void loop() {
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
Serial.println(newPosition);
}
if(newPosition == 0) {
digitalWrite(6, LOW);
digitalWrite(7,LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
// Spielerzahl 2, Motor dreht 360 Grad
if(newPosition == 4) {
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
digitalWrite(6, HIGH);
digitalWrite(9, HIGH);
digitalWrite(8, HIGH);
if (button1.pressed()) {
State = 1;
Serial.print(State);
}
while (State == 1) {
stepper.moveTo(2048);
stepper.run();
if (stepper.distanceToGo() == 0) {
stepper.setCurrentPosition(0);
State = 0;
Serial.print(State);
}
}
}
// Spielerzahl 3 Motor dreht 120 Grad pro Spieler
if(newPosition == 8) {
digitalWrite(6, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
if (button1.pressed()) {
State = 1;
Serial.print(State);
}
while (State == 1) {
stepper.moveTo(802);
stepper.run();
if(stepper.distanceToGo() == 0) {
myservo.write(180);
delay(15);
myservo.write(0);
delay(1000);
if(stepper.distanceToGo() == 0) {
stepper.setCurrentPosition(0);
stepper.moveTo(802);
stepper.run();
State = 0;
Serial.print(State);
}
}
}
}
}
Because the while loop ends with setting state=0. I think you missunderstand how the .run() command works. It creates at max 1 step per call. Therfore you must call .run() as often as possible. It doesn't matter if you call it too often.
Also the combination of these two statements:
doesn't make sense. The .moveTo command tells Accelstepper where to go, and after that you must only call .run() very often to create the necessary steps.
P.S. if you want your sketch to block while the stepper is moving, use .runToNewPosition(targetposition) and omit the .run().