Project 10 problems with program, help needed

Hey,

i followed every step described in the projects book (at least i think i did), but my power button does not seem to work at all. The problem is in the code i think, because as you can see, i was monitoring it through serial communication, and every time i press the button, the "motorPower" variable stays the same. It does write out "INDICATOR" tho. Thanks for the help in advance :slight_smile: Here is my code:

const int controlPin1 = 2;
const int controlPin2 = 3;
const int enablePin = 9;
const int directionSwitchPin = 4;
const int onOffPin = 5;
const int potPin = A0;

int onOffSwitchState = 0;
int prevOnOffSwitchState = 0;
int directionSwitchState = 0;
int prevDirectionSwitchState = 0;

int motorPower = 0;
int motorSpeed = 0;
int motorDirection = 1;
void setup() {
Serial.begin(9600);
pinMode(controlPin1,OUTPUT);
pinMode(controlPin2,OUTPUT);
pinMode(enablePin,OUTPUT);
pinMode(directionSwitchPin,INPUT);
pinMode(onOffPin,INPUT);

analogWrite(enablePin, 0);
}

void loop() {
onOffSwitchState = digitalRead(onOffPin);
delay(1);

directionSwitchState = digitalRead(directionSwitchPin);

motorSpeed = analogRead(potPin)/4;

if (onOffSwitchState != prevOnOffSwitchState) {
if (onOffSwitchState == 1) {
Serial.print("INDICATOR");
motorPower = 0;
}
prevOnOffSwitchState = onOffSwitchState;
}

if (directionSwitchState != prevDirectionSwitchState) {
if (directionSwitchState == 1) {
motorDirection = !motorDirection;
}
}

if (motorDirection = 1) {
digitalWrite(controlPin1,LOW);
digitalWrite(controlPin2,HIGH);
} else {
digitalWrite(controlPin1,HIGH);
digitalWrite(controlPin2,LOW);
}

if (motorPower = 1){
analogWrite(enablePin,motorSpeed);
} else {
analogWrite(enablePin,0);
}
prevDirectionSwitchState = directionSwitchState;
Serial.print("onoffPin = ");Serial.print(onOffSwitchState);Serial.print(" prev: ");Serial.print(prevOnOffSwitchState);Serial.print(" motorpower: ");Serial.println(motorPower);
}

Try adding more print statements to show when button states change and speed up the serial baud rate. 9600 is slow even compared to that sketch, go for the high end to serial monitor.

(deleted)

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. Using code tags and other important information is explained in the How to use this forum post. Please read it.