problema con accelstepper y potenciometro

hola a todos he querido usar la libreria de accel stepper para poder controlar un motor y variar la velocidad con un potenciometro, pero no he podido lograrlo de ninguna forma, alguna idea??

#include <AccelStepper.h>

// Define the stepper and the pins it will use
AccelStepper stepper1(1, 9, 8);

// Define our three input button pins
#define  LEFT_PIN  4
#define  STOP_PIN  3
#define  RIGHT_PIN 2

// Define our analog pot input pin
#define  SPEED_PIN 0

// Define our maximum and minimum speed in steps per second (scale pot to these)
#define  MAX_SPEED 500
#define  MIN_SPEED 0.1

void setup() {
  // The only AccelStepper value we have to set here is the max speeed, which is higher than we'll ever go 
  stepper1.setMaxSpeed(10000.0);
  
  // Set up the three button inputs, with pullups
  pinMode(LEFT_PIN, INPUT_PULLUP);
  pinMode(STOP_PIN, INPUT_PULLUP);
  pinMode(RIGHT_PIN, INPUT_PULLUP);
}

void loop() {
  static float current_speed = 0.0;         // Holds current motor speed in steps/second
  static int analog_read_counter = 1000;    // Counts down to 0 to fire analog read
  static char sign = 0;                     // Holds -1, 1 or 0 to turn the motor on/off and control direction
  static int analog_value = 0;              // Holds raw analog value.
  
  // If a switch is pushed down (low), set the sign value appropriately
  if (digitalRead(LEFT_PIN) == 0) {
    sign = 1;
  }
  else if (digitalRead(RIGHT_PIN) == 0) {    
    sign = -1;
  }
  else if (digitalRead(STOP_PIN) == 0) {
    sign = 0;
  }

  // We only want to read the pot every so often (because it takes a long time we don't
  // want to do it every time through the main loop).  
  if (analog_read_counter > 0) {
    analog_read_counter--;
  }
  else {
    analog_read_counter = 3000;
    // Now read the pot (from 0 to 1023)
    analog_value = analogRead(SPEED_PIN);
    // Give the stepper a chance to step if it needs to
    stepper1.runSpeed();
    //  And scale the pot's value from min to max speeds
    current_speed = sign * ((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED;
    // Update the stepper to run at this new speed
    stepper1.setSpeed(current_speed);
  }

  // This will run the stepper at a constant speed
  stepper1.runSpeed();
}

tambien he probado con este y sin ningun resultado

#include <AccelStepper.h>
// Define a stepper and the pins it will use
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
// This defines the analog input pin for reading the control voltage
// Tested with a 10k linear pot between 5v and GND
#define ANALOG_IN A0
void setup()
{  
  stepper.setMaxSpeed(1000);
}
void loop()
{
  // Read new position
  int analog_in = analogRead(ANALOG_IN);
  stepper.moveTo(analog_in);
  stepper.setSpeed(100);
  stepper.runSpeedToPosition();
}

Tu hardware.. algun diagrama de como cableaste, tensiones involucradas.
Que arduino?
El motor gira, no hace nada, vibra. Que mas puedes informar.

Estas corriendo un demo x lo que se ve. Debería funcionar asi que suena a error de cableado.
Agrego:
Busqué tu librería y veo que los ejemplos tienen extensión .pde o sea son viejos.
aca veo librería
pero refiere a la 1.3 y hay una 1.43 disponible

surbyte:
Tu hardware.. algun diagrama de como cableaste, tensiones involucradas.
Que arduino?
El motor gira, no hace nada, vibra. Que mas puedes informar.

Estas corriendo un demo x lo que se ve. Debería funcionar asi que suena a error de cableado.
Agrego:
Busqué tu librería y veo que los ejemplos tienen extensión .pde o sea son viejos.
aca veo librería
pero refiere a la 1.3 y hay una 1.43 disponible

hola disculpa que no habia contestado, ok, con ambos codigos el motor corre sin variar nada solo se mueve a velocidad cosntante, pero al poner el potenciometro en lugares medios empieza a moverse loco acelera des acelera o se queda trabajo por isntantes,

el esquema del potenciometro, pues solo es como una resistencia variable, el pin 1 a GND el pin 2 a A0 y el pin 3 a 5V

Cuando haces eso ves el valor que tomas en A0, tenes algo que lo presenta en el Serial Monitor? Te pregunto porque algunas veces el cursor del potenciómetro hace mal contacto y es como que entrega 5V, o la tensión a la que esta tocando... y como resultado tu motor aceleraría y volvería a velocidad normal.
Suena como que el pote esta mal si esto coincide con lo leido por el AD en A0
Si los valores bailan desde X1 valor a maximo o cero entoces tengo razón.

surbyte:
Cuando haces eso ves el valor que tomas en A0, tenes algo que lo presenta en el Serial Monitor? Te pregunto porque algunas veces el cursor del potenciómetro hace mal contacto y es como que entrega 5V, o la tensión a la que esta tocando... y como resultado tu motor aceleraría y volvería a velocidad normal.
Suena como que el pote esta mal si esto coincide con lo leido por el AD en A0
Si los valores bailan desde X1 valor a maximo o cero entoces tengo razón.

acabo de probar serial y no obtengo nada concreto solo valores al azar, probare un nuevo pote para ver si es el problema

Me parece que por ahi esta el problema.
Revisa las conexiones y mide con un multímetro la salida del potenciómetro.

A mi este codigo me funciono perfectamente! :slight_smile:

copachino:
hola a todos he querido usar la libreria de accel stepper para poder controlar un motor y variar la velocidad con un potenciometro, pero no he podido lograrlo de ninguna forma, alguna idea??

#include <AccelStepper.h>

// Define the stepper and the pins it will use
AccelStepper stepper1(1, 9, 8);

// Define our three input button pins
#define  LEFT_PIN  4
#define  STOP_PIN  3
#define  RIGHT_PIN 2

// Define our analog pot input pin
#define  SPEED_PIN 0

// Define our maximum and minimum speed in steps per second (scale pot to these)
#define  MAX_SPEED 500
#define  MIN_SPEED 0.1

void setup() {
  // The only AccelStepper value we have to set here is the max speeed, which is higher than we'll ever go
  stepper1.setMaxSpeed(10000.0);
 
  // Set up the three button inputs, with pullups
  pinMode(LEFT_PIN, INPUT_PULLUP);
  pinMode(STOP_PIN, INPUT_PULLUP);
  pinMode(RIGHT_PIN, INPUT_PULLUP);
}

void loop() {
  static float current_speed = 0.0;        // Holds current motor speed in steps/second
  static int analog_read_counter = 1000;    // Counts down to 0 to fire analog read
  static char sign = 0;                    // Holds -1, 1 or 0 to turn the motor on/off and control direction
  static int analog_value = 0;              // Holds raw analog value.
 
  // If a switch is pushed down (low), set the sign value appropriately
  if (digitalRead(LEFT_PIN) == 0) {
    sign = 1;
  }
  else if (digitalRead(RIGHT_PIN) == 0) {   
    sign = -1;
  }
  else if (digitalRead(STOP_PIN) == 0) {
    sign = 0;
  }

// We only want to read the pot every so often (because it takes a long time we don't
  // want to do it every time through the main loop). 
  if (analog_read_counter > 0) {
    analog_read_counter--;
  }
  else {
    analog_read_counter = 3000;
    // Now read the pot (from 0 to 1023)
    analog_value = analogRead(SPEED_PIN);
    // Give the stepper a chance to step if it needs to
    stepper1.runSpeed();
    //  And scale the pot's value from min to max speeds
    current_speed = sign * ((analog_value/1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED;
    // Update the stepper to run at this new speed
    stepper1.setSpeed(current_speed);
  }

// This will run the stepper at a constant speed
  stepper1.runSpeed();
}