Hi there, im having a bit of trouble with a project, using a Arduino Uno and an adafruit motor shield and a stepper motor.
here is my sketch so far
#include <AFMotor.h>
// Connect a stepper motor with 72 steps per revolution (5 degree)
// to motor port #1 (M1 and M2)
AF_Stepper motor(72, 1);
const int PotPin = A0; // Change this if pot is connected to another pin
const int goPin = 2; //the pin that the go switch is connected to
// this is switching digital pin 2 from High to Low
const int redPin = A2; // the pin for the RED LED
const int bluePin = A4; // the pin for the BLUE LED
int direction = FORWARD; // Where we are going
int speed = 0; // How fast
int potVal = 0; // Current potvalue
int goState = 0; //current state of the goPin
void setup() {
pinMode (goPin, INPUT);
pinMode (redPin, OUTPUT);
pinMode (bluePin, OUTPUT);
Serial.begin(9600); // initialize serial communication at 9600 bits per second:
while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only
}
}
void loop() {
potVal = analogRead(PotPin); // read the input on the pot:
goState = digitalRead (goPin);
if (potVal < 510)
{
direction = FORWARD; // Smaller than 512 so we're going forward
speed = map(potVal, 0, 511, 20, 1); // Map value from pot-range to motorspeed-range
}
else if (potVal > 513)
{ // Otherwise, we're going backward
direction = BACKWARD;
// Map value from pot-range to motorspeed-range
speed = map(potVal, 512, 1024, 1, 20);
}
// print out the variables:
Serial.print("GoPin: ");
Serial.print(goState );
Serial.print(" POT Value: ");
Serial.print(potVal);
Serial.print(" direction: ");
if (direction==FORWARD)
Serial.print("Forward");
else
Serial.print("Backward");
Serial.print(" Speed: ");
Serial.println(speed); // End of line, so also make it go a line
// make motor go a step
motor.setSpeed(speed); // Set speed of motor
motor.step(1,direction,INTERLEAVE);
// You can change SINGLE to SINGLE, DOUBLE, INTERLEAVE or MICROSTEP
delay(1); // delay in between reads for stability
}
the stepper runs perfectly
I want to add a "go Switch" which is a single throw switch - the center pin is connected to pin 2 and the outer pins are +5v and Ground respectively,
So I don't want my stepper to run untill pin 2 see's high voltage and then I want it to stop when pin 2 see's low voltage.
If someone could show me how to add this to my current sketch that would be super.