Hi,
I have my stepper motor working via serial commands, but, not with pushbuttons as well. Any ideas where I am wrong? Thanks!
#include <Stepper.h>
#define motorSteps 200 // change this depending on the number of
// steps per revolution of your motor
#define motorPin1 9
#define motorPin2 10
#define motorPin3 11
#define motorPin4 12
#define ledPin 13
const int buttonPin1 = 3; // Switch connected to digital pin 3
const int buttonPin2 = 4; // Switch connected to digital pin 4
char up = 'u'; // ascii motor up
char down = 'd'; // ascii motor down
int myStepperState;
// variables will change:
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
char nextChar;
// initialize of the Stepper library:
Stepper myStepper(motorSteps, motorPin1,motorPin2,motorPin3,motorPin4);
void setup()
{
myStepper.setSpeed(80); // set the motor speed at 60 RPMS:
Serial.begin(9600); // Initialize the Serial port:
pinMode(buttonPin1, INPUT); // sets the digital pin as input to read switch
pinMode(buttonPin2, INPUT); // sets the digital pin as input to read switch
}
void loop()
{
if (Serial.available())
{
nextChar = Serial.read(); // read just one character
buttonState1 = digitalRead(buttonPin1); // read the state of the pushbutton value:
buttonState2 = digitalRead(buttonPin2); // read the state of the pushbutton value:
if (nextChar == up) // check for 'u'
{
myStepper.step(300); // Step forward 300 steps:
Serial.println("Serial Up");
}
else if (nextChar == down) // check for 'd'
{
myStepper.step(-300); // Step backward 100 steps:
Serial.println("Serial Down");
}
else if (buttonState1 == HIGH) // check if the button is pressed
{
myStepper.step(300); // Step forward 300 steps:
Serial.println("Switch Up");
}
else if (buttonState2 == HIGH) // check if the button is pressed
{
myStepper.step(-300); // Step backward 100 steps:
Serial.println("Switch Down");
}
}
}