Stepper motor mapping to sensor

Hi guys, i am struggling to understand why my stepper motor isn't moving,
below is the code, it should take the mapped value and then use it to set the position and speed of the motor and then run the motor

many thanks


#include <AccelStepper.h>

AccelStepper stepper2(AccelStepper::DRIVER, 2, 7);

int sensorPin = A0;
int sensorValue = 0;
 int motorValue;

void setup() {
  Serial.begin(9600);
 stepper2.setMaxSpeed(10000);
}

void loop() {
sensorValue = analogRead(A0);
//Serial.println(sensorValue);

motorValue = map(sensorValue, 10,510,6000,60000);
delay(100);
Serial.println(",");
Serial.println(motorValue);

if (stepper2.distanceToGo() == 0)
  {
   stepper2.move(motorValue);
   stepper2.run();
   delay(100);
    
 }
}

AccelStepper and delay() don't really fit together well.
You must put the stepper2.run() statement out of the if (stepper2.distanceToGo() == 0) block.
stepper2.run() must be called as often as possible, because this is the call that creates the step pulses.

What Arduino are you using?

A 'standard' Arduino with ATmega processor cannot run the stepper that fast (with Accelstepper).

How will you define speed and position with one value? And how should the value define the position? Absolutely? relative?

hi thankyou for replying! im using a Leonardo, and will attempt to add more run() calls now, will get back to you!

Im not sure the best solution for this, i think it will need to be an absolute, and well i can define the speed numerically and just use the map return for the position?

By what means? Maybe a good idea if you tell a bit more about your project.

the project is an xy plotter but where the stepper motors are progammed to respond to levels of sound from the analogue sensor, i just need some sort of movement for the uni hand in haha

mmhmm .. I can't really imagine that... :thinking:

No acceleration is set. Is default low enough to be safe?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.