Stepper Motor On-Off with Debounce

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
awallace42
 
Posts: 5
Joined: Wed Sep 17, 2014 7:23 am

Stepper Motor On-Off with Debounce

Post by awallace42 »

Hello,

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
}
}

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Stepper Motor On-Off with Debounce

Post by adafruit_support_bill »

Your motor movement & led blinking code is not in the scope of any conditional (if) statement, so it will run every time through the loop.

The call to step() and the two calls to delay() mean that your motor movement/led blinking will take at least 3/4 of a second. During that time you will not be checking for button presses, so the chances of actually seeing a button press are very small.

User avatar
awallace42
 
Posts: 5
Joined: Wed Sep 17, 2014 7:23 am

Re: Stepper Motor On-Off with Debounce

Post by awallace42 »

Thanks for the response.

So I understand what you’re saying. But how would I put the motor step, pause and blink within the if statement? What would the “Loop" look like?

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Stepper Motor On-Off with Debounce

Post by adafruit_support_bill »

This should get you a bit closer to what you want. The motor and led flashing code is inside an 'if' statement that tests "runState". And the button state is checked on each step of the motor, so it can stop the motor in the middle of the cycle. You can use a similar approach to monitor the buttons while timing the led blink.

Code: Select all

void loop() 
{
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)
  {
    runState = !runState;
  }
  lastButton = currentButton;
  digitalWrite(buttonPin1, runState);
  
  if (runState)
  {
    Serial.println("Double coil steps");
    for (int i = 0; i < 50; i++)
    {
        myMotor->step(1, FORWARD, DOUBLE); // Amount motor turns
        if (digitalRead(buttonPin1) == HIGH)
        {
            break; // bail out on a button press
        }
    }
    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
  }
}

User avatar
awallace42
 
Posts: 5
Joined: Wed Sep 17, 2014 7:23 am

Re: Stepper Motor On-Off with Debounce

Post by awallace42 »

Thanks!

As ever more solutions just opens up for more questions!

Very much appreciated.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”