I'm trying to set up a bipolar stepper motor with feedback. I want the motor to rotate down until a button is pressed and then return to the original position, while assigning the number of steps traveled to a variable.
I've set up my circuit according to this (with four pins) and this (bipolar stepper circuit).
arduino.cc/en/Reference/StepperBipolarCircuit
arduino.cc/en/Tutorial/MotorKnob
I plan on using the built-in stepper function, and this is what I have written so far:
#include <Stepper.h>
#include <LiquidCrystal.h>
#define STEPS 48 // change this to the number of steps on
//your motor
Stepper stepper(STEPS, 9, 10, 11, 12); // create an instance of
//the stepper class, specifying the number of steps of the motor
//and the pins it's attached to
LiquidCrystal lcd(1, 2, 3, 4, 5, 6); // initialize library with
// the numbers of interface pins
int previous = 0; // the previous reading from the analog input
#define BUTTONA 8 // the input pin where the
// pushbutton is connected
int val = 0; // val will be used to store the state
// of the input pin
int old_val = 0; // this variable stores the previous
// value of "val"
int val2 = 0; // val2 will be used to store the state
// of the input pin
int old_val2 = 0; // this variable stores the previous
// value of "val2"
int state = 0; // 0 = STEPPER off and 1 = STEPPER on
#define BUTTONB 7 // Voltage readout from probes
int n = 1; // number of steps
int z = 0; //variable
void setup() {
pinMode(BUTTONA, INPUT); // and BUTTON is an input
pinMode(BUTTONB, INPUT);
stepper.setSpeed(30); // set the speed of the motor to 30 RPMs
lcd.begin(16,2); //set up the LCD's number of columns and rows
}
void loop(){
val = digitalRead(BUTTONA); // read input value and store it
if ((val == HIGH) && (old_val == LOW)){ // check if there
//was a transition
state = 1 - state;
delay(10);
}
old_val = val; // val is now old, let's store it
if (state == 1) {
z = 1;
n = 0;
loop(); {
val2 = digitalRead(BUTTONB);
if ((val2 == LOW) && (z = 1)) { //If the sensor is than 5 volts
n=n+1; // number of steps plus one
stepper.step(1); // move 1 step
}
else if (val2 == HIGH) {
stepper.step(-n); // move back up corresponding number
//of steps
z = 2;
lcd.display();
lcd.clear();
lcd.print("Water Depth"); //Display The water depth is
lcd.setCursor(0,1);
lcd.print(15 - (n * 0.0127)); // Calculate water depth and display it
delay(15000);
lcd.noDisplay();
}
// Store to SD Card
// Display to LCD
} // run STEPPER Loop
}
}
Any thoughts?