I've been wrestling with my stepper motor sketch and trying to add a on/off switch with a debounce on the button.
The sketch compiles fine, and as far as I can see my wiring is fine, but its not working.
I wondered if anyone can spot any obvious problems with the sketch?
The Sketch should move the motor, pause, blink an LED (pin 13) and repeat. The on/off button (pin 2) should turn off the motor and LED.
Many thanks in advance,
Gus
Code: Select all
/*
Controls Stepper Motor with an on/off switch with debounce. The Stepper should move, pause and blink the LED before moving again.
*/
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
int buttonPin1 = 2 ;
int led = 13 ;
bool runState = false;
boolean lastButton = LOW;
boolean currentButton = LOW;
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Stepper test!");
pinMode (buttonPin1, INPUT_PULLUP);
pinMode(led, OUTPUT);
AFMS.begin(); // create with the default frequency 1.6KHz
//AFMS.begin(1000); // OR with a different frequency, say 1KHz
myMotor->setSpeed(30); // Speed motor turns
}
boolean debounce(boolean last)
{
boolean current = digitalRead(buttonPin1);
if (last != current)
{
delay(5);
current = digitalRead(buttonPin1);
}
return current;
}
void loop() {
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH)
{
runState = !runState;
}
lastButton = currentButton;
digitalWrite(buttonPin1, runState);
{
Serial.println("Double coil steps");
myMotor->step(50, FORWARD, DOUBLE); // Amount motor turns
digitalWrite(13, HIGH); // turn LED on
delay(250); // waits for quarter of a second
digitalWrite(13, LOW); // turn LED off
delay(500); // waits for half a second
}
}