Hi Everyone!
I'm making my first stepper motor project (let's say, I'm a beginner), and I think I've run into some problems...
I want to make a one-axis camera slider, with standard NEMA 17 form factor motor, A4988 driver and an Arduino UNO Rev3. The idea is that I use 3 potentiometers to set the Acceleration, Maximum Speed and Final Position, after the initial homing is complete (in void setup, taken fron Dejan Nedelkovski). The setup part seems to be working fine.
In looop, the motor should start moving after the Go Button is pressed, though it seems that the motor stops after the button is released, even if it hasn't reached its final position yet (which isn't desired). I have a reset button, but I think this will be excluded, as homing is done at startup (switching the arduino off and then on is thus the same thing as a reset button). Also, I'm not sure if I'll need a stop button on the other side (endbtn), as I presume the max mapped value will limit the step count to 80000.
The stepper runs, but does so strangely. When uploading a simple AccelStepper example code with MaxSpeed of 300 it rotates much faster than when 300 is displayed as the MaxSpeed on my LCD, while running the code below.
Am I missing something (some things) here? I have been troubleshooting this for 3 days now, looking into how other people have done it, etc., so any type of help is appreciated!
#include <LiquidCrystal.h>
LiquidCrystal lcd(10, 2, 4, 5, 6, 7);
#include <AccelStepper.h>
AccelStepper myStepper(1, 8, 9);
int gobtn = A3;
int rstbtn = A4;
int endbtn = A5; // Analog pins used as digital (due to pin shortage on UNO)
#define hombtn 3
int accpot = A0;
int spdpot = A1;
int pospot = A2; // the potentiometers for acceleration, max speed and final position
void setup() {
// put your setup code here, to run once:
myStepper.setMaxSpeed(3000);
myStepper.setSpeed(200);
pinMode(gobtn, INPUT);
pinMode(rstbtn, INPUT);
pinMode(endbtn, INPUT);
pinMode(hombtn, INPUT_PULLUP);
pinMode(accpot, INPUT);
pinMode(spdpot, INPUT);
pinMode(pospot, INPUT);
lcd.begin(16, 2);
lcd.print("Homing..."); // Prints to LCD
while (digitalRead(hombtn) != 0) {
myStepper.setSpeed(300);
myStepper.runSpeed();
myStepper.setCurrentPosition(0); // When limit switch pressed set position to 0 steps
}
delay(20);
// Move 200 steps back from the limit switch
while (myStepper.currentPosition() != -200) {
myStepper.setSpeed(-300);
myStepper.run();
}
delay(2000);
lcd.clear(); //Homed!
}
void loop() {
// put your main code here, to run repeatedly:
long acc = map(analogRead(accpot), 0, 1023, 0, 1000);
long spd = map(analogRead(spdpot), 0, 1023, 0, 3000);
long pos = map(analogRead(pospot), 0, 1023, 0, 80000);
lcd.setCursor(0, 0); // Sets the cursor to col 0 and row 0
lcd.print("A: "); // Prints to LCD
lcd.print(acc); // Prints value on Potpin1 to LCD
lcd.setCursor(8, 0); // Sets the cursor to col 8 and row 0
lcd.print("S: "); // Prints Sensor Val: to LCD
lcd.print(spd); // Prints value on Potpin1 to LCD
lcd.setCursor(0, 1); // Sets the cursor to col 0 and row 1
lcd.print("P: "); // Prints Sensor Val: to LCD
lcd.print(pos); // Prints value on Potpin1 to LCD
lcd.setCursor(9, 1); // Sets the cursor to col 9 and row 1
lcd.print("GO?"); // Prints to LCD
if (digitalRead(gobtn) == HIGH) // if go button pressed {
lcd.setCursor(9, 1); // Sets the cursor to col 1 and row 0
lcd.print("GO!"); // Prints to LCD
myStepper.setAcceleration(acc);
myStepper.setMaxSpeed(spd);
myStepper.moveTo(pos);
myStepper.run();
if (endbtn == HIGH) {
myStepper.setSpeed(-100);
myStepper.runSpeed();
delay(3000);
myStepper.setSpeed(0);
myStepper.runSpeed();
delay(1000);
}
}
if (rstbtn == HIGH) {
while (hombtn == LOW) {
myStepper.setSpeed(-2000);
myStepper.runSpeed();
}
if (hombtn == HIGH) {
myStepper.setSpeed(0);
myStepper.runSpeed();
myStepper.setCurrentPosition(0);
}
}
}