I apologize ahead of time for how basic this question is, but why won't my variable "outputmode" increment each time the "button" is pushed in this code? The buttonflag seems to work, just can' increment the outputmode?
int outputmode = 0;
int Relay1 = 13;
int Relay2 = 12;
int Relay3 = 11;
int LEDMode1 = 10; // The LED Light for Mode 1 active
int LEDMode2 = 9; // The LED Light for Mode 2 active
int LEDMode3 = 8; // The LED Light for Mode 3 active
int Button = 2; // The switch to change output modes
int Knee = 3; // The Switch Input
int Buttonstate = 0;
int buttonflag = 0; // Indicates if Button is pushed
void setup() {
Serial.begin(9600);
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(Relay3, OUTPUT);
pinMode(LEDMode1,OUTPUT);
pinMode(LEDMode2,OUTPUT);
pinMode(LEDMode3,OUTPUT);
pinMode(Button,INPUT_PULLUP);
pinMode(Knee,INPUT_PULLUP);
}
void loop() {
// Change outputmode by pressing Mode button
Buttonstate = digitalRead(Button);
if(Buttonstate==LOW){
Serial.println("Button pressed");
buttonflag = 1; // Set flag to TRUE if button is pushed.
}
if (buttonflag==1 & Buttonstate==HIGH){ // Button has been pressed then released
Serial.println("Button released");
outputmode++;
buttonflag = 0;
} // Button is pushed and released, increment Mode by 1
if(outputmode=0)
{
digitalWrite(LEDMode1,HIGH);
digitalWrite(LEDMode2,LOW);
digitalWrite(LEDMode3,LOW);
}
else if(outputmode=1)
{
digitalWrite(LEDMode1,LOW);
digitalWrite(LEDMode2,HIGH);
digitalWrite(LEDMode3,LOW);
}
else if(outputmode=2)
{
digitalWrite(LEDMode1,LOW);
digitalWrite(LEDMode2,LOW);
digitalWrite(LEDMode3,HIGH);
}
else if (outputmode > 2)
{
outputmode = 0;
}
Serial.print("Button State: ");
Serial.print(Buttonstate);
Serial.print(" Flag = ");
Serial.print(buttonflag);
Serial.print(" & Mode = ");
Serial.println(outputmode);
delay (1000);
}