I want to be able to Home the stepper to a sensor switch which I call Home in my sketch and then step to different locations from the home position.
Attached is my current program that I want to modify to the above.
Can you tell me the best way to home the stepper in my program. I would like to use the Park button to rotate the stepper until it hits the Home switch and then make this the home position and step to the other positions from there.
Thanks for your help. Rmac
- Code: Select all
#include <buttons.h>
#include <Bounce.h>
#include <AccelStepper.h>
#include <AFMotor.h>
/*
Four switch stepper motor Program
Turns on and off digital pins connected to pin 9,10,11,13,2 as output when pressing pushbuttons
(Park,Rev,Ntrl, Dr and Home ) attached to digital pins 14,15,16,17and 18
The circuit:
* Using AfaFruit shield with motor output on M3 and M4
* pushbuttons are attached to digital pins 14,15,16,17,qnd switch 18 +5V supply for switches. // analog pins A0,A1,A2,A3, and A4 are used as digital pins 14 through 18:
* 10K resistors attached to digital pins 2 from ground
*/
// constants won't change. They're used here to
// set pin numbers:
const int buttonPinPark = 14; //Setting Park button to Pin 14
const int buttonPinRev = 15; //Setting Reverse button to Pin 15
const int buttonPinNtrl = 16; //Setting Neutral button to Pin 16
const int buttonPinDr = 17; //Setting Drive button to Pin 17
const int buttonPinHome = 18; //Used as input for the home position on pin 18
int buttonStatePark = 0; //Setting Park button state to off
int buttonStateRev = 0; //Setting Reverse button state to off
int buttonStateNtrl = 0; //Setting Neutral button state to off
int buttonStateDr = 0; //Setting Drive button state to off
int buttonStateHome = 0; //Setting Drive button state to off
const int Pin9 = 9; // PIN OUTPUT NUMBER
const int Pin10 = 10; // PIN OUTPUT NUMBER
const int Pin11 = 11; // PIN OUTPUT NUMBER
const int Pin13 = 13; // PIN OUTPUT NUMBER
const int Pin2 = 2; // PIN OUTPUT NUMBER
AF_Stepper motor(200, 2);
void setup() {
// open the serial port at 9600 bps:
Serial.begin(9600);
// initialize the pin as an output:
pinMode (Pin9,OUTPUT);
pinMode (Pin10,OUTPUT);
pinMode (Pin11,OUTPUT);
pinMode (Pin13,OUTPUT);
pinMode (Pin2,OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPinPark, INPUT);
pinMode(buttonPinRev, INPUT);
pinMode(buttonPinNtrl, INPUT);
pinMode(buttonPinDr, INPUT);
pinMode(buttonPinHome, INPUT);
motor.setSpeed(10); // 10 rpm
}
void loop()
{
// delay 10 milliseconds before the next reading:
delay(10);
// read the state of the Park pushbutton value:
buttonStatePark = digitalRead(buttonPinPark); //code for Park button
Serial.print("Park = ");
Serial.println(buttonStatePark, DEC); // print as an ASCII-encoded decimal:
// check if the Park pushbutton is pressed.
// if it is, the buttonStatePark is HIGH:
if (buttonStatePark == HIGH)
{
// Have output on pin 9 (5 volts:
digitalWrite (Pin9, HIGH);
motor.step(20, FORWARD, SINGLE);
}
else
{
// Set Pin1 Off:
digitalWrite(Pin9, LOW);
}
{
// delay 10 milliseconds before the next reading:
delay(10);
// read the state of the Rev pushbutton value:
buttonStateRev = digitalRead(buttonPinRev); //code for Rev button
Serial.print("Rev = ");
Serial.println(buttonStateRev, DEC); // print as an ASCII-encoded decimal:
// check if the Rev pushbutton is pressed.
// if it is, the buttonStateRev is HIGH:
if (buttonStateRev == HIGH)
{
// Have output on pin 10 (5 volts:
digitalWrite (Pin10, HIGH);
motor.step(40, BACKWARD, SINGLE);
}
else
{
// Set Pin2 Off:
digitalWrite(Pin10, LOW);
{
// delay 10 milliseconds before the next reading:
delay(10);
// read the state of the Ntrl pushbutton value:
buttonStateNtrl = digitalRead(buttonPinNtrl); //code for Ntrl button
Serial.print("Ntrl = ");
Serial.println(buttonStateNtrl, DEC); // print as an ASCII-encoded decimal:
// check if the Ntrl pushbutton is pressed.
// if it is, the buttonStateNtrl is HIGH:
if (buttonStateNtrl == HIGH)
{
// Have output on pin 11 (5 volts:
digitalWrite (Pin11, HIGH);
motor.step(60, FORWARD, SINGLE);
}
else
{
// Set Pin1 Off:
digitalWrite(Pin11, LOW);
}
{
// delay 10 milliseconds before the next reading:
delay(10);
// read the state of the Rev pushbutton value:
buttonStateDr = digitalRead(buttonPinDr); //code for Dr button
Serial.print("Dr = ");
Serial.println(buttonStateDr, DEC); // print as an ASCII-encoded decimal:
// check if the Dr pushbutton is pressed.
// if it is, the buttonStateDr is HIGH:
if (buttonStateDr == HIGH)
{
// Have output on pin 13 (5 volts:
digitalWrite (Pin13, HIGH);
motor.step(80, FORWARD, SINGLE);
}
else
{
// Set Pin2 Off:
digitalWrite(Pin13, LOW);
{
}
}
}
}
}
}
}

