Hi there,
First time poster, new Arduino learner.
I know there's a few posts about this project already and I've had a read and tried a few of the code solutions, but no banana. So hoping someone might be able to give my code/set up a quick troubleshoot to see if I'm just being a bit dense!
My circuit is attached and here's my code is below. I'm getting no movement at all when I push the buttons/twist the potentiometer. There's three different set ups in the photo as I wondered if it was the placement of my potentiometer that might be causing the problem.
Any thoughts would be massively appreciated. Thank you!
const int controlPin1 = 2;
const int controlPin2 = 3;
const int enablePin = 9;
const int directionSwitchPin = 4;
const int onOffSwitchStateSwitchPin = 5;
const int potPin = A0;
int onOffSwitchState = 0;
int previousOnOffSwitchState = 0;
int directionSwitchState = 0;
int previousDirectionSwitchState = 0;
int motorEnabled = 0;
int motorSpeed = 0;
int motorDirection = 1;
void setup() {
// put your setup code here, to run once:
pinMode(directionSwitchPin, INPUT);
pinMode(onOffSwitchStateSwitchPin, INPUT);
pinMode(controlPin1, OUTPUT);
pinMode(controlPin2, OUTPUT);
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
onOffSwitchState =
digitalRead(onOffSwitchStateSwitchPin);
delay(1);
directionSwitchState =
digitalRead(directionSwitchPin);
motorSpeed = analogRead(potPin)/4;
if(onOffSwitchState != previousOnOffSwitchState){
if(onOffSwitchState == HIGH){
motorEnabled = !motorEnabled;
}
if (directionSwitchState !=
previousDirectionSwitchState) {
if (directionSwitchState == HIGH) {
motorDirection = !motorDirection;
}
if (motorDirection == 1) {
digitalWrite(controlPin1, HIGH);
digitalWrite(controlPin2, LOW);
}
else {
digitalWrite(controlPin1, LOW);
digitalWrite(controlPin2, HIGH);
}
if (motorEnabled == 1) {
analogWrite(enablePin, motorSpeed);
}
else {
analogWrite(enablePin, 0);
}
previousDirectionSwitchState =
directionSwitchState;
previousOnOffSwitchState = onOffSwitchState;
}
}
}