So now i've attempted to play with button state to have the program remember what button was pushed when it hits the limiter switch. unfortunately it's not working though.
if i press button1 and then the limiter switch, it works as expected.
if i press button2 and then the limiter switch, it runs the buttonstate1 && limitsw instead of the buttonstate2 && limitsw if statement.
also, if i press the limiter switch just by itself, it goes through limiter switch && button 1 if statement and moves 600 steps regardless if I pressed button1 or not.
#include <Button.h>
#include <Stepper.h>
#define STEPS_PER_REVOLUTION 200
// Pick correct Arduino development board for Rugged Motor Driver
#define BOARD 0 /* Arduino Duemilanove/Uno (ATmega328P) */
//#define BOARD 1 /* Arduino Mega (ATmega1280) */
//#define BOARD 2 /* Rugged Circuits Gator (ATmega324P) */
#ifndef BOARD
# if defined(__AVR_ATmega328P__)
# define BOARD 0
# elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
# define BOARD 1
# elif defined(__AVR_ATmega324P__)
# define BOARD 2
# else
# error You must define BOARD near the top of this file
# endif
#endif
#if (BOARD!=0) && (BOARD!=1) && (BOARD!=2)
#error Unknown board
#endif
// Enable (PWM) outputs
#define EN1_PIN 3
#define EN2_PIN 11
// Direction outputs
#define DIR1_PIN 12
#define DIR2_PIN 13
// Fault inputs, active low
#define FAULT1_PIN 5
#define FAULT2_PIN 8
Stepper stepper(STEPS_PER_REVOLUTION, DIR1_PIN, DIR2_PIN);
// Set initial default values
unsigned RPM = 100;
unsigned PWM = 15;
unsigned DIR = -1;
//create a Button object at pin X
/*
|| Wiring:
|| GND -----/ ------ pin X
*/
Button button = Button(6,PULLUP);
Button button2 = Button(7,PULLUP);
Button limitsw = Button(9,PULLUP);
// try to use button state to remember which buttons was pressed //
int buttonState1 = 0;
int buttonState2 = 0;
void setup(){
// Configure all outputs off for now
pinMode(EN1_PIN, OUTPUT); digitalWrite(EN1_PIN, LOW);
pinMode(EN2_PIN, OUTPUT); digitalWrite(EN2_PIN, LOW);
pinMode(DIR1_PIN, OUTPUT); digitalWrite(DIR1_PIN, LOW);
pinMode(DIR2_PIN, OUTPUT); digitalWrite(DIR2_PIN, LOW);
// Configure fault inputs with pullups
pinMode(FAULT1_PIN, INPUT); digitalWrite(FAULT1_PIN, HIGH);
pinMode(FAULT2_PIN, INPUT); digitalWrite(FAULT2_PIN, HIGH);
Serial.begin(9600); // only necessary for debugging
// Change from divide-by-64 prescale on Timer 2 to divide by 8 to get
// 8-times faster PWM frequency (976 Hz --> 7.8 kHz). This should prevent
// overcurrent conditions for steppers with high voltages and low inductance.
TCCR2B = _BV(CS21);
}
void loop(){
//////////// configure button 1 /////////////
if(button.uniquePress()){
// Now enable PWM and start motion
buttonState1 = 1;
analogWrite(EN1_PIN, PWM);
analogWrite(EN2_PIN, PWM);
stepper.setSpeed(RPM);
Serial.println("reverse");
}
///////// configure button 2//////////////
if(button2.uniquePress()){
// Now enable PWM and start motion
buttonState2 = 1;
analogWrite(EN1_PIN, PWM);
analogWrite(EN2_PIN, PWM);
stepper.setSpeed(RPM);
Serial.println("reverse");
}
//// limiter switch with button1 //
if(buttonState1 = 1 && limitsw.uniquePress()){
analogWrite(EN1_PIN, PWM);
analogWrite(EN2_PIN, PWM);
stepper.setSpeed(RPM);
Serial.println("stop button1 reverse");
delay(100);
Serial.println("forward 3 revolutions");
// # of steps forward//
stepper.step(600);
// turn off power to the motor then be on "ready" state //
digitalWrite(EN1_PIN, LOW);
digitalWrite(EN2_PIN, LOW);
digitalWrite(DIR1_PIN, LOW);
digitalWrite(DIR2_PIN, LOW);
}
//// limiter switch with button2 //
if(buttonState2 = 1 && limitsw.uniquePress()){
analogWrite(EN1_PIN, PWM);
analogWrite(EN2_PIN, PWM);
stepper.setSpeed(RPM);
Serial.println("stop button1 reverse");
delay(100);
Serial.println("forward 1 revolution");
// # of steps forward//
stepper.step(200);
// turn off power to the motor then be on "ready" state //
digitalWrite(EN1_PIN, LOW);
digitalWrite(EN2_PIN, LOW);
digitalWrite(DIR1_PIN, LOW);
digitalWrite(DIR2_PIN, LOW);
}
// This is a busy-wait loop until the inter-step time passes
stepper.step(DIR);
}
Any ideas guys?