1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
#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 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:
// delay 10 milliseconds before the next reading:
delay(10);
// read the state of the Home pushbutton value:
buttonStateHome = digitalRead(buttonPinHome); //code for Park button
Serial.print("Home = ");
Serial.println(buttonStateHome, DEC); // print as an ASCII-encoded decimal:
// check if the Park pushbutton is pressed.
// if it is, the buttonStatePark is HIGH:
int val = 0; // variable to store the read value
if (buttonStatePark == HIGH)
{
// Have output on pin 9 (5 volts:
digitalWrite (Pin9, HIGH);
{
while (buttonStateHome != HIGH)
// step until the home limit switch is detected
motor.step(1, FORWARD, SINGLE);
val = digitalRead(buttonStateHome); // read the input pin
digitalWrite(buttonStateHome, val); // sets the Home Pin to the button's value
}
{
}
}
else
{
// Set Pin9 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 Pin10 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 Pin11 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 Pin13 Off:
digitalWrite(Pin13, LOW);
{
}
}
}
}
}
}
}
|