hello can someone take a quick look at this code trying to select speed via switch that latches i know i have something wrong but i cant see??it
/*
28-byj-48 stepper motor control uln2003 driver board
arduino uno ky040 encoder
this will use a switch on pin7 - gnd to select run fast for course selection and slow for fine control
*/
#include "Stepper.h"
#define STEPS 32 // Number of steps for one revolution of Internal shaft
// 2048 steps for one revolution of External shaft
volatile boolean TurnDetected; // need volatile for Interrupts
volatile boolean rotationdirection; // CW or CCW rotation
const int PinCLK=2; // Generating interrupts using CLK signal
const int PinDT=3; // Reading DT signal
const int PinSW=4; // Reading Push Button switch
float buttonPin = 7; // the pin that the pushbutton is attached to select speed
int RotaryPosition=0; // To store Stepper Motor Position
int PrevPosition; // Previous Rotary position Value to check accuracy
int StepsToTake;
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
// Setup of proper sequencing for Motor Driver Pins
// In1, In2, In3, In4 in the sequence 1-3-2-4
Stepper small_stepper(STEPS, 8, 10, 9, 11);
// Interrupt routine runs if CLK goes from HIGH to LOW
void isr () {
delay(4); // delay for Debouncing
if (digitalRead(PinCLK))
rotationdirection= digitalRead(PinDT);
else
rotationdirection= !digitalRead(PinDT);
TurnDetected = true;
}
void setup () {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);//to select slow step
digitalWrite(buttonPin, HIGH); // Pull-Up resistor for switch
pinMode(PinCLK,INPUT);
pinMode(PinDT,INPUT);
pinMode(PinSW,INPUT);
digitalWrite(PinSW, HIGH); // Pull-Up resistor for switch
attachInterrupt (0,isr,FALLING); // interrupt 0 always connected to pin 2 on Arduino UNO
}
void loop () {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
if(buttonPin); (HIGH); small_stepper.setSpeed(70);
(buttonPin); (LOW); small_stepper.setSpeed(200);
//small_stepper.setSpeed(200);
//small_stepper.setSpeed(70); //Max seems to be 700
if (!(digitalRead(PinSW))) { // check if button is pressed
if (RotaryPosition ==0) { // check if button was already pressed
} else {
small_stepper.step(-(RotaryPosition*20));
RotaryPosition=0; // Reset position to ZERO
}
}
// Runs if rotation was detected
if (TurnDetected) {
PrevPosition = RotaryPosition; // Save previous position in variable
if (rotationdirection) {
RotaryPosition=RotaryPosition-1;} // decrease Position by 1
else {
RotaryPosition=RotaryPosition+1;} // increase Position by 1
TurnDetected = false; // do NOT repeat IF loop until new rotation detected
}
// Which direction to move Stepper motor
if ((PrevPosition + 1) == RotaryPosition) { // Move motor CW
StepsToTake=1; // fine tune steps and same below
small_stepper.step(StepsToTake);
}
if ((RotaryPosition + 1) == PrevPosition) { // Move motor CCW
StepsToTake=-1;
small_stepper.step(StepsToTake);
}
}