Hello I am trying to control the direction and speed of a stepper motor using the following program. The speed counter seems to work, unfortunately i am not able to control the direction. Is my approach wrong?
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ezButton.h>
#include <AccelStepper.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
ezButton inc (9);
ezButton dec (10);
ezButton cw (6);
ezButton ccw(7);
int cnt = 0;
int incPrev, decPrev, speedPrev, val1, val2;
// Define stepper motor connections and steps per revolution:
const int dirPin = 2;
const int stepPin = 3;
AccelStepper myStepper(1, stepPin, dirPin);
//#define stepsPerRevolution 10
long speed;
void setup()
{
//lcd
lcd.begin(16, 2);
lcd.backlight();
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
myStepper.setMaxSpeed(10000);
Serial.begin(115200);
delay(2000);
digitalWrite(dirPin, HIGH);
}
void loop()
{
inc.loop();
dec.loop();
cw.loop();
ccw.loop();
updatecnt();
myStepper.runSpeed();
}
void updatecnt ()
{
bool update = false; // no need to update speed
if (inc.isPressed() && cnt < 10) {
cnt++;
update = true;
}
else if (dec.isPressed() && cnt > 0) {
cnt--;
update = true;
}
if (( update == true ) && (cw.isPressed() )){
long speed = 200 * cnt;
Serial.print("New speed = ");
Serial.println(speed);
myStepper.setSpeed(speed);
lcd.clear();
lcd.print("RPM = ");
lcd.print(cnt);
}
else if (( update == true ) && (ccw.isPressed() ))
{
long speed = 200 * cnt;
Serial.print("New speed = ");
Serial.println(speed);
myStepper.setSpeed(-speed);
lcd.clear();
lcd.print("RPM = ");
lcd.print(cnt);
}
}
@alto777 Thank you so much. You are really helpful.
I have modified the program and added a couple more switches to enable more control. Here is the link to it.
But Still, this is not working the way i want it and i couldn't resolve the issues.
There are few issues here:
The toggle switch doesn't change direction instantaneously when i switch it. I need to press the inc/dec button to make it work.
-I added another toggle switch to control the operation mode; continuous rotation vs motor rotate when switch is pressed. But this doesn't seem to work.
I am beginner when it comes to arduino, so any help would be appreciated.
The switch is a two position switch, no center off position.
Yes, the speed must change with the press of the inc?dec button right away.
It does not do all and what you want. It is just a beginning of the restructuring. If you read through the code, you can see what I am talking about. I tried to put //... where I made changes.
Note: ezButton needs
manual.setDebounceTime(25);
the denounce time set for every button that is EZ. ALso note, several of your switches and buttons do not need to be EZ. Just read them and use the logic value as I show.
Going under the umbrella just now. See if that framework makes it easier to implement your advnaced control features.
Do you have an understanding of how ezButtons function?
I'm not a fan of library code, especially button code. I have a minimal grip on what ezButton can and cannot do for you. I may have come to some inaccuracies, I suggest that you familiar ize yourself with the examples using ezButton that come with the library. I hope there are examples.
But I the below is close enough:
If all you want to check is the level of a digital input, just use digitalRead() on the pin. I used digitalRead() on one pin to dictate the motor direction.
If you want to take action when a button gets pressed, test buttonWhatever.isPressed().
isPressed returns true once each time you press the button.
I used isPressed() to turn on the motor.
If you want to take action when a button gets released, test buttonWhatever.isReleased().
isReleased returns true once each time you let off the button.
I used isReleaswd() to turn off the morot.
You might see I could have just used the level… many choices.
I think if you keep all this in mind and carefully read the code, you will be able to get the other switches and buttons working.