Reputation: 11
Hi I'm having trouble adding two buttons to my code, I'm not sure how to add it for a project I'm working on.
I'm using Pin 8 (Button 1)and Pin 9 (Button 2) on my Arduino uno,the stepper motor is connected to Pin 3 (DIR +) , Pin 4 (EN +) and Pin 5 (PUL +)
The Code is Perfect and works 100% it makes the motor turn 360 but i also like to use the buttons to turn the motor manually with the buttons if possible.
#include <Stepper.h>
const int stepsPerRevolution = 25600; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 3 through 4:
Stepper myStepper(stepsPerRevolution, 3,4);
int timeCounter=0;
void setup() {
Serial.begin(100); // initialize the serial port:
myStepper.setSpeed(100); // set the speed at 60 rpm:
}
void loop() {
Serial.println("clockwise"); // step one revolution in one direction:
myStepper.step(stepsPerRevolution);
delay(100);
Serial.println("\ncounterclockwise"); // step one revolution in the other direction:
myStepper.step(-stepsPerRevolution);
delay(100);
timeCounter++;
if(timeCounter == 2 )
myStepper.setSpeed(0);
}
I have tried adding this code with Push buttons but cant get it to work.I need to combine these two codes in to one.
// defines pins numbers
const int dirPin = 3;
const int stepPin = 4;
const int enPin = 5;
const int switchOne = 8;
const int switchTwo = 9;
int p1buttonState = 0; // current state of the button
int lastp1buttonState = 0; // previous state of the button
int p2buttonState = 0; // current state of the button
int lastp2buttonState = 0; // previous state of the button
bool bPress = false;
bool isForward = false;
bool isBackward = false;
void setup() {
Serial.begin(9600);
pinMode( switchOne, INPUT_PULLUP);
pinMode( switchTwo, INPUT_PULLUP);
// Sets the two pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enPin, OUTPUT);
digitalWrite(enPin, LOW);
}
void loop() {
isForward = false;
isBackward = false;
p1buttonState = digitalRead(switchOne);
p2buttonState = digitalRead(switchTwo);
if (p1ButtonPress()) {
digitalWrite(dirPin, HIGH);
delay(5);
}
if (p2ButtonPress()) {
digitalWrite(dirPin, LOW);
delay(5);
}
if ( isForward || isBackward ) {
for (int x = 0; x < 3200; x++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
}
}
}
bool p1ButtonPress()
{
bool isPress = false;
// compare the p1buttonState to its previous state
if (p1buttonState != lastp1buttonState) {
// if the state has changed, increment the counter
if (p1buttonState == LOW) {
// if the current state is HIGH then the button went from off to on:
bPress = true;
isPress = true;
Serial.println("Plaer One score");
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
isForward = true;
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastp1buttonState = p1buttonState;
return isPress;
}
bool p2ButtonPress()
{
bool isPress = false;
// compare the p1buttonState to its previous state
if (p2buttonState != lastp2buttonState) {
// if the state has changed, increment the counter
if (p2buttonState == LOW) {
// if the current state is HIGH then the button went from off to on:
bPress = true;
isPress = true;
Serial.println("Plaer Two score");
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
isBackward = true;
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastp2buttonState = p2buttonState;
return isPress;
}
Upvotes: 1
Views: 348
Reputation: 61
Here is an example using the Bounce2 library (per Juraj's suggestion). If I knew more about your application I could create a more tailored example. I suspect that your issue is timing related. See this forum post for more info on why not to use Delay. Remember to install the Bounce2 library before compiling.
#include <Stepper.h>
#include <Bounce2.h>
/* Timing Defines */
const int debounceInterval = 5;
/* End Timing Defines */
/* Stepper Defines */
const int stepsPerRevolution = 25600;
// initialize the stepper library on pins 3 through 4:
Stepper myStepper(stepsPerRevolution, 3, 4);
/* End Stepper Defines */
/* Button Defines */
const int buttonOnePin = 8;
const int buttonTwoPin = 9;
const int stepsPerButtonPress = 25600 / 32; // make the denominator larger for smaller increments
Bounce2::Button buttonOne = Bounce2::Button();
Bounce2::Button buttonTwo = Bounce2::Button();
/* End Button Defines */
void setup() {
Serial.begin(9600); // initialize the serial port
while (!Serial)
; // This will cause your program to wait for the serial port to open
myStepper.setSpeed(100); // set the speed at 60 rpm:
myStepper.step(stepsPerRevolution);
buttonOne.attach(buttonOnePin, INPUT_PULLUP);
buttonTwo.attach(buttonTwoPin, INPUT_PULLUP);
buttonOne.interval(debounceInterval);
buttonTwo.interval(debounceInterval);
Serial.println("Lets spin a stepper!");
}
void loop() {
// check for updates
buttonOne.update();
buttonTwo.update();
// check buttonOne
if (buttonOne.pressed()) {
Serial.println("Step up");
myStepper.step(stepsPerButtonPress);
}
// check buttonTwo
if (buttonTwo.pressed()) {
Serial.println("Step down");
myStepper.step(-stepsPerButtonPress);
}
}
Upvotes: 1