I need help with my code.
Problem Statement:
Design circuitry hardware and code that will allow the change of 3 different speeds using one pushbutton and allow to change direction using another push button.
I have created the following circuit and the following code.
Can anyone please guide me as I am having trouble getting my code and project to work correctly.
#define spButton 5
#define dirButton 6
#define pwm1 3
#define pwm2 2
#define EN1 4
#define ledPin 13
boolean motorDir = 0;
int motorSp = 0;
int spVal = 0;
int speed1 = 120;
int speed2 = 188;
int speed3 = 255;
// Variables for reading and storing button state:
int speedButtonState; // the current reading from the input speed pin
int lastSpeedButtonState = LOW; // the previous reading from the input pin
int directionButtonState; // the current reading from the input direction pin
int lastDirectionButtonState = LOW; // the previous reading from the direction pin
// the following variables are long's because the time, measured in miliseconds, will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(spButton, INPUT);
pinMode(dirButton, INPUT);
pinMode(pwm1, OUTPUT);
pinMode(pwm2, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// read the state of the switch into a local variable:
int speedReading = digitalRead(spButton);
if (speedReading != lastSpeedButtonState) // If the button state is different from last time
{
lastDebounceTime = millis(); // reset the debouncing timer
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (speedReading != speedButtonState) {
speedButtonState = speedReading; // Store the state of button in speedButtonState
// only toggle the LED if the new button state is HIGH
if (speedButtonState == HIGH) {
digitalWrite(ledPin, HIGH); //turn on the LED
spVal = spVal + 1;
if (spVal >= 4) // When stat >= 4, set it as 0.
{
spVal = 0;
}
} else
digitalWrite(ledPin, LOW);
}
}
switch (spVal) {
case 1:
motorSp = analogRead(speed1);
if (motorDir)
analogWrite(pwm1, motorSp);
else
analogWrite(pwm2, motorSp);
if (!digitalRead(dirButton)) { // If direction button is pressed
while (!digitalRead(dirButton))
; // Wait until direction button released
motorDir = !motorDir; // Toggle direction variable
if (motorDir)
digitalWrite(pwm2, 0);
else
digitalWrite(pwm1, 0);
break;
case 2:
motorSp = analogRead(speed2);
if (motorDir)
analogWrite(pwm1, motorSp);
else
analogWrite(pwm2, motorSp);
if (!digitalRead(dirButton)) { // If direction button is pressed
while (!digitalRead(dirButton))
; // Wait until direction button released
motorDir = !motorDir; // Toggle direction variable
if (motorDir)
digitalWrite(pwm2, 0);
else
digitalWrite(pwm1, 0);
break;
case 3:
motorSp = analogRead(speed1);
if (motorDir)
analogWrite(pwm1, motorSp);
else
analogWrite(pwm2, motorSp);
if (!digitalRead(dirButton)) { // If direction button is pressed
while (!digitalRead(dirButton))
; // Wait until direction button released
motorDir = !motorDir; // Toggle direction variable
if (motorDir)
digitalWrite(pwm2, 0);
else
digitalWrite(pwm1, 0);
break;
}
}
}
}
}
