Issues using AccelStepper, general programming

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);
    }

  }

}

For a start on line 69 your opening { has wandered out into the comment

...R

oops, you are right, thogh this is not part of the issue, as I added the comment only for clarification before posting (uploaded code doesn't have the comment).

The run() function should be in loop() but outside of any other function so that it is executed every time through loop() and loop() is executed more often than a step is required.

You may want to look at the state change detection example to see how to detect when a button becomes pressed versus is pressed. Then you will not have to hold the button down.

groundFungus:
The run() function should be in loop() but outside of any other function so that it is executed every time through loop() and loop() is executed more often than a step is required.

Yeah, this seems to work! Thank you so much for the help!!!

OliverMJ:
oops, you are right, thogh this is not part of the issue, as I added the comment only for clarification before posting (uploaded code doesn't have the comment).

Please post the real code. It is a waste of time looking at the wrong code.

...R

Aside from a few comments, it IS the uploaded code.

OliverMJ:
Aside from a few comments, it IS the uploaded code.

I'm not going to edit your code to make it what you would like it to be - that's your job.

In any case I might introduce some other error that would confuse both of us.

...R