Go Down

Topic: [Help] Set and modify "steps per lap" Accelstepper (Read 71 times) previous topic - next topic


Feb 26, 2020, 02:56 am Last Edit: Feb 26, 2020, 03:18 am by Bruno1visconti
The idea is to set the "step-by-turn" VMAX to calibrate the range in which I can move with the potentiometer.

If A0 (the potentiometer) is at 0
and keep the SET button pressed for 3s then I enter the calibration program.

I can move the motor step by step with the "FF" and "REV" buttons, and for every pulse the Pololu receives, +1 is added to the "CON" variable
Once the range of motion is calibrated, the SET button is pressed again for three seconds and the VMAX value (200 by default) is replaced by the "CON" value.

Can I explain?
My dude is:

I am following the logic in the programming well?

I leave the code.

PD: Something that I am missing in the code is that when it finishes setting the engine it returns to the starting point.

Code: [Select]
#include "AccelStepper.h"
// Library created by Mike McCauley at http://www.airspayce.com/mikem/arduino/AccelStepper/

// AccelStepper Setup
AccelStepper stepper(1, 8, 5);  // 1 = Easy Driver interface
                                  // Nano Pin 8 connected to STEP pin of Easy Driver
                                  // Nano Pin 5 connected to DIR pin of Easy Driver

// Variables to store current, previous and move position
int val = 0;
int previous = 0;
int long newval = 0;    
int VMIN = 0;
int VMAX = 200;
int CONT = 0;
int SETPIN = 1;
int FFPIN = 2;
int REVPIN = 6;
unsigned long tiempo; //Variable para el control
unsigned long tiempo_max= 3000; //Variable para el control
boolean estado= false;
void setup() {
  digitalWrite (2, HIGH);
  stepper.setMaxSpeed(4800);  // Set speed fast enough to follow pot rotation
  stepper.setAcceleration(4800);  //  High Acceleration to follow pot rotation
   digitalWrite(SETPIN,HIGH); //Activa PullUp resistor

void loop() {

  if(millis()-tiempo >= tiempo_max){
      estado = true;
  if (estado == true){analog();}
  digitalWrite(SETPIN,HIGH); //Activa PullUp resistor
  tiempo= millis();

void calibracion(){
digitalWrite(8, HIGH);
digitalWrite(8, LOW);
int cont = (cont+1);

  if(millis()-tiempo >= tiempo_max){
      estado = true;
  if (estado == true)
  { int cont = VMAX;  
  digitalWrite(SETPIN,HIGH); //Activa PullUp resistor
  tiempo= millis();

void analog(){

    val = analogRead(A0);  //  Read Potentiometer current value

 if (val = 0) {calibracion();}
void giro () {
  val = analogRead(A0);  //  Read Potentiometer current value
  if ((val > previous+6) || (val < previous-6)) {  // Check that a move of the pot is at least > or < than 6
    newval = map(val, 0, 1023, VMIN, VMAX);  // Map value (1600 = 1 stepper shaft rotation)
    stepper.runToNewPosition(newval);  // Move stepper to new position
    previous = val;  // save current value into variable previous


Go Up