Hi guys i need an aplication where i can set the speed for the steeper motor and a button to start movement
im using a
DM860H driver
Motor Nema 34 1696oz/in 122kg/cm
4.0Amps
1.4Ohms
Bipolar 8 cables
limit swith and a potenciometer
im current able to start the stepper when i press the button but im unable to change speed with the potenciometer , i been trying for hours and diferent code but i cant make it work together any help will be very appreciated
my code
#include <AccelStepper.h>
AccelStepper stepper(AccelStepper::DRIVER,7,6);
int buttonPin = 2;
int buttonState;
int ledpin = 13;
int motorSpeed = 9600;
int motorAccel = 100000;
int speedVal = 0;
int speedControl = A0;
void setup()
{
Serial.begin(9600);
stepper.setMaxSpeed(motorSpeed);
//stepper.setSpeed(motorSpeed);
stepper.setAcceleration(motorAccel);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledpin, OUTPUT);
}
void loop()
{
buttonState = digitalRead(buttonPin);
//int sensorValue = analogRead(speedAnalog);
// speedStepper = map((analogRead(speedAnalog)),0,1023,2000,50);
if (buttonState == LOW){
digitalWrite(ledpin,HIGH);
stepper.moveTo(60000);
stepper. run();
}
if (buttonState == HIGH){
digitalWrite(ledpin,LOW);
stepper.stop();
stepper.runToPosition();
stepper.setCurrentPosition(0);
}
}
after adding the analogodRead my stepper get really slow and also dont change speed , please help
Do not perform the analogRead() every pass through the loop. Place it on a "blink without delay" millis() timer and execute the analogRead() read every 250 or 500 ms.
cattledog:
Do not perform the analogRead() every pass through the loop. Place it on a “blink without delay” millis() timer and execute the analogRead() read every 250 or 500 ms.
that it thanks @cattledog with your help i was manage to do all that i need
for reference this is my final code
#include <AccelStepper.h>
AccelStepper stepper(AccelStepper::DRIVER,7,6);
int buttonPin = 2;
int buttonState;
int ledpin = 13;
int motorSpeed = 7000;
int motorAccel = 100000;
int speedVal = 0;
int speedControl = A0;
int speedStepper = 0;
unsigned long previousMillis = 0; // will store last time LED was updated
long OffTime = 750; // milliseconds of off-time
void setup()
{
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledpin, OUTPUT);
}
void loop()
{
unsigned long currentMillis = millis();
buttonState = digitalRead(buttonPin);
if ((buttonState == LOW) && (currentMillis - previousMillis >= OffTime)){
speedStepper = map(analogRead(A0), 0, 1023, 100, 7000);
Serial.println(speedStepper);
stepper.setMaxSpeed(speedStepper);
stepper.setAcceleration(motorAccel);
}
if (buttonState == HIGH){
digitalWrite(ledpin,HIGH);
stepper.moveTo(60000);
stepper. run();
}
if (buttonState == LOW){
digitalWrite(ledpin,LOW);
stepper.stop();
stepper.runToPosition();
stepper.setCurrentPosition(0);
}
}
must be a better way as always but im too tired to keep testing , thanks agaian