Pushbuttons and motor control with Adafruit motor shield? SIMPLE Problem!


hello, hope challenge in design come nothing more game of tic-tac-toe willing help.

basically, have arduino uno board, adafruit motorshield v2.3, , unipolar/bipolar stepper motor. along these items handful of resistors, leds, , couple of four-pronged pushbuttons, , 9v ac dc wall plug power arduino independently of computer.

my goal simple; first, want able have simple "on" switch extended processor can accessible outside of prototype. in nature, switch there activate , deactivate motor, spins , forth programmed me.

currently, have gotten far programming motor start , stop spinning based on digital input "state change detection" when pressing pushbutton. issue first of all, breadboard , pushbutton inaccessible hand when prototype assembled.

the second issue pushbutton activates motor on first attempt, should, -getting stop instantaneously upon second button press not happen. motor stop when pushbutton pressed @ right time, held down , released @ right time, or annoying combination of that.

i figured simplest way around splicing switch 9v wall adapter , cleanly running guts exterior of prototype, programming arduino such motor spins continuously long power available. good, since stepper motors draw power when aren't spinning. however, bad way lead later development stages, different button inputs set different motor cycles (for example.)

so summarize, here have accomplished:
motor activation upon pushbutton press (see attached code)
motor deactivation mysteriously correct timing of button press

here need accomplishing:
getting single pushbutton press instantaneously interrupt , cancel motor cycle wherever in cycle. if pressed again, motor cycle start on again. have attached code using try accomplish this. have been pointed in direction of "interrupts" before, little more telling me go research interrupt commands.

lastly, appreciate guidance finding user-friendly, external button panel works arduino , adafruit motorshield. 3d print sort of fitment it, @ least, need know sort of compatible tactile "button" there can extended breadboard or motor shield itself. @ costs, i'm trying avoid sort of brittle mechanical linkage external button pushbutton on breadboard within.


code: [select]
/*
  state change detection (edge detection)

 often, don't need know state of digital input time,
 but need know when input changes 1 state another.
 for example, want know when button goes off on.  called
 state change detection, or edge detection.

 this example shows how detect when button or button changes off on
 and on off.

 the circuit:
 * pushbutton attached pin 2 +5v
 * 10k resistor attached pin 2 ground
 * led attached pin 13 ground (or use built-in led on
   most arduino boards)

 created  27 sep 2005
 modified 30 aug 2011
 by tom igoe < naw niggas lame

this example code in public domain.

 http://arduino.cc/en/tutorial/buttonstatechange

 */

// adding shit shit because we're richhhh
#include <wire.h>
#include <adafruit_motorshield.h>
#include "utility/adafruit_pwmservodriver.h"
adafruit_motorshield afms = adafruit_motorshield();
adafruit_steppermotor *mymotor = afms.getstepper(200, 2);

// constant won't change:
const int  buttonpin = 2;    // pin pushbutton attached to
const int ledpin = 13;       // pin led attached to
const int led2pin = a0;

// variables change:
int buttonpushcounter = 0;   // counter number of button presses
int buttonstate = 0;         // current state of button
int lastbuttonstate = 0;     // previous state of button

void setup() {
  // initialize button pin input:
  pinmode(buttonpin, input);
  // initialize led output:
  pinmode(ledpin, output);
  pinmode(led2pin, output);

  // initialize serial communication:
  serial.begin(9600);
  // set serial library @ 9600 bps
  serial.println("giving ");

  afms.begin();  // create default frequency 1.6khz
  //afms.begin(1000);  // or different frequency, 1khz
}

void loop() {{
  // read pushbutton input pin:
  buttonstate = digitalread(buttonpin);

  // compare buttonstate previous state
  if (buttonstate != lastbuttonstate) {
    // if state has changed, increment counter
    if (buttonstate == high) {
      // if current state high button
      // wend off on:
      buttonpushcounter++;
      serial.println("click!");
      //serial.print("number of button pushes:  ");
      serial.println(buttonpushcounter);
      //serial.println(digitalread(2));
      //serial.println(buttonstate);     
    }
//    else {
//      // if current state low button
//      // wend on off:
//      serial.println("off");
//    }
    // delay little bit avoid bouncing
    delay(50);
  }
  // save current state last state,
  //for next time through loop
  lastbuttonstate = buttonstate;}


  // turns on led every ___ button pushes by
  // checking modulo of button push counter.
  // modulo function gives remainder of
  // division of 2 numbers:
  if (buttonpushcounter % 2 == 0) {
    digitalwrite(ledpin, high);
    digitalwrite(led2pin, low);
    mymotor->setspeed(0);
} else {
      digitalwrite(ledpin, low);
      digitalwrite(led2pin, high);
      digitalwrite
     
      //motor block
      mymotor->setspeed(75);  // 50 rpm   
      mymotor->step(200, forward, double); //"single" implies single motor steps
      mymotor->step(150, backward, double);
      mymotor->step(50, forward, double); //"single" implies single motor steps
      mymotor->step(50, backward, double);
      mymotor->step(80, forward, double); //"single" implies single motor steps
      mymotor->step(100, backward, double);
  //     {if (buttonstate == high)
  //     serial.println("high");
  //     if (buttonstate == low)
  //     serial.println("low");
  //     }
    }
}
 

my goal simple; first, want able have simple "on" switch extended processor can accessible outside of prototype. in nature, switch there activate , deactivate motor, spins , forth programmed me.

my coding issue pushbutton activates motor on first attempt, should, -getting stop instantaneously upon second button press not happen. motor stop when pushbutton pressed @ right time, held down , released @ right time, or annoying combination of that.

i figured simplest way around splicing switch 9v wall adapter , cleanly running guts exterior of prototype, programming arduino such motor spins continuously long power available. good, since stepper motors draw power when aren't spinning. however, bad way lead later development stages, different button inputs set different motor cycles (for example.)

in summary, need getting single pushbutton press instantaneously interrupt , cancel motor cycle wherever in cycle. if pressed again, motor cycle start on again. have attached code using try accomplish this. have been pointed in direction of "interrupts" before, little more telling me go research interrupt commands.

code: [select]
/*
  state change detection (edge detection)

 often, don't need know state of digital input time,
 but need know when input changes 1 state another.
 for example, want know when button goes off on.  called
 state change detection, or edge detection.

 this example shows how detect when button or button changes off on
 and on off.

 the circuit:
 * pushbutton attached pin 2 +5v
 * 10k resistor attached pin 2 ground
 * led attached pin 13 ground (or use built-in led on
   most arduino boards)

 created  27 sep 2005
 modified 30 aug 2011
 by tom igoe < naw niggas lame

this example code in public domain.

 http://arduino.cc/en/tutorial/buttonstatechange

 */

// adding shit shit because we're richhhh
#include <wire.h>
#include <adafruit_motorshield.h>
#include "utility/adafruit_pwmservodriver.h"
adafruit_motorshield afms = adafruit_motorshield();
adafruit_steppermotor *mymotor = afms.getstepper(200, 2);

// constant won't change:
const int  buttonpin = 2;    // pin pushbutton attached to
const int ledpin = 13;       // pin led attached to
const int led2pin = a0;

// variables change:
int buttonpushcounter = 0;   // counter number of button presses
int buttonstate = 0;         // current state of button
int lastbuttonstate = 0;     // previous state of button

void setup() {
  // initialize button pin input:
  pinmode(buttonpin, input);
  // initialize led output:
  pinmode(ledpin, output);
  pinmode(led2pin, output);

  // initialize serial communication:
  serial.begin(9600);
  // set serial library @ 9600 bps
  serial.println("giving ");

  afms.begin();  // create default frequency 1.6khz
  //afms.begin(1000);  // or different frequency, 1khz
}

void loop() {{
  // read pushbutton input pin:
  buttonstate = digitalread(buttonpin);

  // compare buttonstate previous state
  if (buttonstate != lastbuttonstate) {
    // if state has changed, increment counter
    if (buttonstate == high) {
      // if current state high button
      // wend off on:
      buttonpushcounter++;
      serial.println("click!");
      //serial.print("number of button pushes:  ");
      serial.println(buttonpushcounter);
      //serial.println(digitalread(2));
      //serial.println(buttonstate);     
    }
//    else {
//      // if current state low button
//      // wend on off:
//      serial.println("off");
//    }
    // delay little bit avoid bouncing
    delay(50);
  }
  // save current state last state,
  //for next time through loop
  lastbuttonstate = buttonstate;}


  // turns on led every ___ button pushes by
  // checking modulo of button push counter.
  // modulo function gives remainder of
  // division of 2 numbers:
  if (buttonpushcounter % 2 == 0) {
    digitalwrite(ledpin, high);
    digitalwrite(led2pin, low);
    mymotor->setspeed(0);
} else {
      digitalwrite(ledpin, low);
      digitalwrite(led2pin, high);
      digitalwrite
     
      //motor block
      mymotor->setspeed(75);  // 50 rpm   
      mymotor->step(200, forward, double); //"single" implies single motor steps
      mymotor->step(150, backward, double);
      mymotor->step(50, forward, double); //"single" implies single motor steps
      mymotor->step(50, backward, double);
      mymotor->step(80, forward, double); //"single" implies single motor steps
      mymotor->step(100, backward, double);
  //     {if (buttonstate == high)
  //     serial.println("high");
  //     if (buttonstate == low)
  //     serial.println("low");
  //     }
    }
}
 


Arduino Forum > Using Arduino > Programming Questions > Pushbuttons and motor control with Adafruit motor shield? SIMPLE Problem!


arduino

Comments