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 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);
}