I looking for help trying to get a 23CS23C stepper motor with DM542S driver using uno R3 to rotate 2 1/2 turns every time I press the buttonpin. This is my 1st project with Arduino. It works properly on the 1st press but will not do anything after. The serial monitor shows that it is working properly but the stepper isn't moving. any help would be greatly appreciated.
#include "AccelStepper.h"
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
const int dirPin = 8;
const int stepPin = 9;
int buttonpin = 3;
bool bucket = 0;
#define motorInterfaceType 1
// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType,stepPin, dirPin,bucket);
void setup() {
pinMode(dirPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(buttonpin, INPUT);
Serial.begin(9600);
// Set the maximum speed and acceleration:
stepper.setMaxSpeed(500);
stepper.setAcceleration(80);
}
void loop() {
int bucket = 0;
int I = 1;
if(digitalRead(buttonpin)==HIGH){
bucket = I+I;
if (bucket >= 2){
Serial.println(bucket);
digitalWrite(dirPin, HIGH);// Set the target position:
Serial.println("high.");
stepper.moveTo(800);
// Run to target position with set speed and acceleration/deceleration:
stepper.runToPosition();
delay(1000);
Serial.println("low");
int bucket = 0;
Serial.println(bucket);
}
}else((bucket)==0);{
Serial.println("I am elsing");
delay(5000);
} }
Because in this snippet, you define an entirely different bucket variable. Get rid of the int before the word bucket. Each and EVERY time you put a type in front of a variable name you create a new variable.
#include "AccelStepper.h"
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
const int dirPin = 8;
const int stepPin = 9;
const int buttonpin = 3;
#define motorInterfaceType 1
// Create a new instance of the AccelStepper class:
AccelStepper stepper(motorInterfaceType, stepPin, dirPin);
void setup() {
pinMode(dirPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(buttonpin, INPUT); // pull down resistor assumed
Serial.begin(9600);
// Set the maximum speed and acceleration:
stepper.setMaxSpeed(500);
stepper.setAcceleration(80);
digitalWrite(dirPin, HIGH);
}
void loop() {
static int Count = 0;
if (digitalRead(buttonpin) ) { // pull down resistor assumed
Count ++;
delay(20);
Serial.println(Count);
}
if (Count) {
stepper.move(800);
Count--;
Serial.println(Count);
}
stepper.run();
}
Thanks Paul, I did try that and it worked for 1 press then it goes into a weird loop (I am elsing, I am elsing, 2, high., low, 0, I am elsing, I am elsing) but the stepper doesn't move. I tried uploading a schematic but said I couldn't because I was new.
I would like it to go (800) every time I press the button. I did make some changes and it looked good on the serial monitor, it did everything the way I wanted it to, until I turned on the power to the driver and I'm right back to where I was.
#include "AccelStepper.h"
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
const int dirPin = 8;
const int stepPin = 9;
const int buttonpin = 3;
bool bucket = 0;
#define motorInterfaceType 1
// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin, bucket);
void setup() {
pinMode(dirPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(buttonpin, INPUT);
Serial.begin(9600);
// Set the maximum speed and acceleration:
stepper.setMaxSpeed(500);
stepper.setAcceleration(80);
}
void loop() {
int bucket = 0;
int I = 1;
if (digitalRead(buttonpin) == HIGH) {
bucket = I+I;
if (bucket == 2) {
Serial.println(bucket);
digitalWrite(dirPin, HIGH);// Set the target position:
Serial.println("high.");
stepper.moveTo(800);
// Run to target position with set speed and acceleration/deceleration:
stepper.runToPosition();
delay(1000);
Serial.println("low");
bucket = 0;
Serial.println(bucket);
}
}else((bucket) == 0); {
stepper.moveTo(0);
stepper.runToPosition();
Serial.println("I am elsing");
delay(5000);
}
}
Than you should not use moveTo(800) but move(800) moveTo(800) moves the stepper to an absolute position, and if it is already there nothing happens. move(800) always moves it 800 steps away from its current position ( relative move ).
But there are more problems in your sketch ( as @DaveX already pointed out ).
Thank you everyone for all your help. I found several things that I had done wrong and used some of your suggestions. Here is my sketch that is running awesome. Thanks again.
#include "AccelStepper.h"
// Define stepper motor connections and motor interface type. Motor interface type must be set to 1 when using a driver:
const int dirPin = 8;
const int stepPin = 9;
const int buttonpin = 3;
int buttonValue;
bool bucket = 0;
int dt=250;
#define motorInterfaceType 1
// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin, bucket);
void setup() {
pinMode(dirPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(buttonpin, INPUT);
digitalWrite(buttonpin,HIGH);
Serial.begin(9600);
// Set the maximum speed and acceleration:
stepper.setMaxSpeed(1500);
stepper.setAcceleration(1500);
}
void loop() {
buttonValue=digitalRead(buttonpin);
Serial.print("your button i:");
Serial.println(buttonValue);
int bucket = 0;
int I = 1;
if (digitalRead(buttonpin) == LOW) {
bucket = I+I;
}
if (bucket == 2){
Serial.println(bucket);
digitalWrite(dirPin, HIGH);// Set the target position:
delay(dt);
Serial.println("high.");
stepper.move(10000);
// Run to target position with set speed and acceleration/deceleration:
stepper.runToPosition();
delay(dt);
Serial.println("low");
bucket = 0;
Serial.println(bucket);
}
else
bucket == 0; {
Serial.println("I am elsing");
delay(dt);
}
}