Hi I am working on a project and I'm not familiar with arduino at all. I have two push buttons and was looking to control the direction of the motor with each push button. In my code i want to control the speed of the direct. I would like p2buttonState to go faster then p1buttonState. I had it working before but now for some reason it wont work. Heres the code im using.
// 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() {
p1buttonState = digitalRead(switchOne);
p2buttonState = digitalRead(switchTwo);
//if (p1buttonState != lastp1buttonState) {
// forward
if (p1buttonState == LOW) {
digitalWrite(dirPin,HIGH);
// printSerialMessage("Setting Forward Command");
digitalWrite(stepPin, HIGH);
// printSerialMessage("RUN");
}
if (p1buttonState == HIGH) {
digitalWrite(stepPin, LOW);
// printSerialMessage("STOP");
}
delayMicroseconds(5000);
// lastp1buttonState = p1buttonState;
//}
//if (p2buttonState != lastp2buttonState) {
// reverse
if (p2buttonState == LOW) {
digitalWrite(dirPin,LOW);
// printSerialMessage("Setting Reverse Command");
digitalWrite(stepPin, HIGH);
// printSerialMessage("RUN");
}
if (p2buttonState == HIGH){
digitalWrite(stepPin, LOW);
// printSerialMessage("STOP");
}
delayMicroseconds(500);
//lastp2buttonState = p2buttonState;
//}
}