I am working on a Project (a simple Gondel / Cablecar) with to switches and von Stepper Motor...
It shoule go along the rope until one switch is pressed,.. then reverse the motor the other way until it ends again and that in a loop.. its for my 3 year old cousin.
I have never worked with arduino before so the last days i tryed a lot with codes and it seams to work but not like it should
When i am decomment the last to rows with the stepper motor
if (a ==1) stepperName.step(stepsPerRevolution);
if (a ==0) stepperName.step(stepsPerRevolution*-1);
In the serial plotter everything works like a charm.. pressing one button i get a A == 1 in the plotter.. pressing the other one i get A == 0,.. that was hard to get.. but now.. i just want to say if
A == 1 .. motor turns left, if A == 0 motor turns right.. but thats not working... also the serial plotter is like freezed and only giving values every 15sek,.. instead of every ms.
The Stepper part is working because if i only but that on the board with a delay.. motor turns left / right one round left 1sek delay, one round right.
Here is my code, i hope you can help me with that =)
#include <ezButton.h>
#include <Stepper.h>
const int stepsPerRevolution = 2048;
Stepper stepperName = Stepper(stepsPerRevolution, 8, 9, 10, 11);
ezButton toggleSwitch(3); ;
ezButton toggleSwitch2(5);
int a = 0;
int b = 0;
void setup() {
stepperName.setSpeed(5);
Serial.begin(9600);
toggleSwitch.setDebounceTime(50); // set debounce time to 50 milliseconds
toggleSwitch2.setDebounceTime(50); // set debounce time to 50 milliseconds
}
void loop() {
toggleSwitch.loop(); // MUST call the loop() function first
toggleSwitch2.loop(); // MUST call the loop() function first
int state = toggleSwitch.getState();
int state2 = toggleSwitch2.getState();
if (state == LOW) Serial.println("The switch: ON");
if (state == LOW) a = 1;
Serial.println("A");
Serial.println(a);
if (state2 == LOW) Serial.println("The switch2: ON");
if (state2 == LOW) a = 0;
Serial.println("A");
Serial.println(a);
if (a ==1) stepperName.step(stepsPerRevolution);
if (a ==0) stepperName.step(stepsPerRevolution*-1);
}
You have steps per revolution as 2048. Most stepper motors are 200 steps per revolution unless they are microstepped. What stepper motor and driver are you using?
Also, you have the motor speed set for 5 RPM. This means a full revolution takes 12 seconds. Since the step() function in the Stepper library is blocking, it will take 12 seconds to return from that function if you ask for one revolution. This may be why your serial output gets delayed.
I am not sure if these switches are limit switches or user buttons your cousin will press to make it go in either direction....
Your code should take only a single step each time through loop, rather than trying to make one large move...
This code will only move if a button is pressed and held down. It will stop upon release. Adjust as needed.
#include <ezButton.h>
#include <Stepper.h>
const int stepsPerRevolution = 2048;
Stepper stepperName = Stepper(stepsPerRevolution, 8, 9, 10, 11);
ezButton toggleSwitch(3);
ezButton toggleSwitch2(5);
int direction = 0; // -1 == reverse, 0 == stop, 1 == forward
void setup() {
stepperName.setSpeed(5);
Serial.begin(9600);
toggleSwitch.setDebounceTime(50); // set debounce time to 50 milliseconds
toggleSwitch2.setDebounceTime(50); // set debounce time to 50 milliseconds
}
void loop() {
toggleSwitch.loop(); // MUST call the loop() function first
toggleSwitch2.loop(); // MUST call the loop() function first
if (toggleSwitch.isPressed() ) {
Serial.println("switch PRESSED");
direction = 1;
}
if (toggleSwitch.isReleased() ) {
Serial.println("switch RELEASED");
direction = 0;
}
if (toggleSwitch2.isPressed() ) {
Serial.println("switch2 PRESSED");
direction = -1;
}
if (toggleSwitch2.isReleased() ) {
Serial.println("switch2 RELEASED");
direction = 0;
}
stepperName.step(direction);
delay(25); // adjust for speed of steps
}