Im totally new to arduino and steppers. Im working on a project where I have to rotate the motor specific number of rotations(only 5 rotations in my case) when a condition is met. The condition is 3 digital reads from buttons( or tactile sensors)! I have been able to rotate the motor but didn't manage so far to make it rotate only a specific number of rotations. Moreover it seems that also the condition isn't working at all. I mean the motor rotates when there is no condition but with one it doesn't at all. There must be something I miss so please help me!!!
how can i make it rotate only 5 times? and what is wrong with the condition in the if statement?
this is what I have so far:
#include <Stepper.h>
int switchPin1 = 2;
int switchPin2 = 4;
int switchPin3 = 7;
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9,10,11);
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(1500);
// initialize the serial port:
Serial.begin(9600);
pinMode(switchPin1, INPUT);
pinMode(switchPin2, INPUT);
pinMode(switchPin3, INPUT);
}
void loop() {
if(digitalRead(switchPin1)==0 && digitalRead(switchPin2)==0 && digitalRead(switchPin3)==0 )
{
//do nothing
myStepper.step(0);
delay(100);
}
if(digitalRead(switchPin1)==1 && digitalRead(switchPin2)==0 && digitalRead(switchPin3)==0 ){
//rotate 5 times in clockwise direction
Serial.println("Clockwise");
myStepper.step(20);
delay(100);
}
if(digitalRead(switchPin1)==1 && digitalRead(switchPin2)==0 && digitalRead(switchPin3)==1 ){
//rotate 5 times counterclockwise direction
Serial.println("Counterclockwise");
myStepper.step(-20);
delay(100);
}
}
How are the SwitchPins wired? Do you have pullup or pulldown resistors so the inputs are not floating? Are they momentary push buttons, toggle switches, what?
attached is the wiring of the pushbuttons! forget about the potentiometer!
Also I have tried the method that MarkT has suggested, unfortunately its not working! plz could your read my code again Im not sure if its correct maybe Im missing something!
Also does anybody know how to set a number of rotations for the motor? like if I want it to rotate only 5 times it rotates only 5 times and then stops.
I've tried doing the following but its not working!
#include <Stepper.h>
const int stepsPerRevolution = 20; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9,10,11);
void setup() {
// set rpm:
myStepper.setSpeed(1500);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
for(int i = 0; i < 6 ; i++)
{
//rotate 5 times in clockwise direction
Serial.println("Clockwise");
myStepper.step(20);
delay(5000);
}
for(int i = 0; i < 6 ; i++)
{
//rotate 5 times counterclockwise direction
Serial.println("Counterclockwise");
myStepper.step(-20);
delay(5000);
}
}
The motor rotates only in clockwise direction and doesn't stop! its like the for loop is being ignored! Any suggestions!
Actually I've tried doing it with different delay intervals, but the outcome was the same, I even tried it without delay at all but no difference. Moreover the motor only rotates in clockwise direction! it doesn't rotate counterclockwise!
It seems to me that the motor is stuck in the for loop for the clockwise rotation and cant get out of it for some reason, because if i don't write a for loop for both rotations, the motor does a clockwise then counterclockwise rotation.
if I comment it out it rotates counterclockwise, if i have both it rotates only clockwise!!! this is so weird!
could anyone who have a stepper motor try my last code( with the for loops) on his motor? please!!! I want to find out if its a programming problem or a hardware thing!