Controlling one output from two different variables

Hello, I am new to arduino and programming. I am trying to control a pc fan with the option of manual setting which is fan on with speed control or an auto setting which will turn fan on high when the reedswitch is open. My code seems to work by itself, but when i place it all together, no matter what I do the fan is only controlled by the potentiometer and the code seems to ignore the reedswitch input. Here is a snip of my code.

// this constant won't change: const int reedswitch = 4;//door switch const int buttonPin1 = 2;//push button for light control const int buttonPin2 = 3;//push button for fan control const int ledPinonL = 12;//led showing state of light control const int ledPinautoL = 11;//led showing state of light control const int ledPinonF = 10;//led showing state of fan control const int ledPinautoF = 9;//led showing state of fan control const int analogOutPin = 6;//to fan const int analogInPin = A0;//from potentiometer int sensorValue = 0; int outputValue = 0;

// Variables will change: int buttonPushCounter1 = 0; int buttonState1 = 0; int lastButtonState1 = 0; int buttonPushCounter2 = 0; int buttonState2 = 0; int lastButtonState2 = 0; void setup() { pinMode(reedswitch, INPUT); pinMode(buttonPin1, INPUT); pinMode(ledPinonL, OUTPUT); pinMode(ledPinautoL, OUTPUT); pinMode(buttonPin2, INPUT); pinMode(ledPinonF, OUTPUT); pinMode(ledPinautoF, OUTPUT); pinMode(analogOutPin, OUTPUT); pinMode(analogInPin, INPUT); Serial.begin(9600); }

void loop() { buttonState1 = digitalRead(buttonPin1); if (buttonState1 != lastButtonState1) if (buttonState1 == HIGH) { buttonPushCounter1++; Serial.println("on"); Serial.print("number of button pushes: "); Serial.println(buttonPushCounter1); } else { Serial.println("off"); } { lastButtonState1 = buttonState1; } if (buttonPushCounter1 % 3 == 1) { digitalWrite(ledPinonL, HIGH); } else { digitalWrite(ledPinonL, LOW); } if (buttonPushCounter1 % 3 == 2) { digitalWrite(ledPinautoL, HIGH); } else { digitalWrite(ledPinautoL, LOW) ; } buttonState2 = digitalRead(buttonPin2); if (buttonState2 != lastButtonState2) if (buttonState2 == HIGH) { buttonPushCounter2++; Serial.println("on"); Serial.print("number of button pushes: "); Serial.println(buttonPushCounter2); } else { Serial.println("off"); } { lastButtonState2 = buttonState2; } if (buttonPushCounter2 % 3 == 1) { digitalWrite(ledPinonF, HIGH);//Turn on led for fan } else { digitalWrite(ledPinonF, LOW); } switch (buttonPushCounter2) case 1:if (buttonPushCounter2 %3 == 1) {analogWrite(analogOutPin, outputValue);

break; case 2:if (buttonPushCounter2 %3 == 2 && reedswitch == LOW); {analogWrite(analogOutPin, 255);}

break; default: {analogWrite(analogOutPin, 0); }

if (buttonPushCounter2 % 3 == 2) { digitalWrite(ledPinautoF, HIGH);//Turn on led for standby operation } else { digitalWrite(ledPinautoF, LOW) ; } { // read the analog in value: sensorValue = analogRead(analogInPin); // map it to the range of the analog out: outputValue = map(sensorValue,0, 1023, 0, 255); // change the analog out value: analogWrite(analogOutPin, outputValue);

// print the results to the serial monitor: Serial.print("sensor = " ); Serial.print(sensorValue); Serial.print("\t output = "); Serial.println(outputValue);

// wait 2 milliseconds before the next loop // for the analog-to-digital converter to settle // after the last reading: delay(2); }}}

If anyone can help with code or suggest with example another way of performing the desired outcome i would much appreciate it.

I'm confused about the swith - case part. You're switching on buttonPushCounter2 but you only have cases for 1 and 2. So after the button has been pushed twice, that part is going to fall through to the default. You also don't have a case for 0, so until you push the button at least once it falls through to the default.

In each of the cases, you are checking against buttonPushCounter %3. I think what you wanted to do there was have a line that sets buttonPushCounter = buttonPushCounter %3 before you start your switch case. That way you'll always either have 0, 1, or 2 in that variable and your switch - case will work like I think you want it to.

It looks there is something wrong with your '{' and '}'. Please check it again, and use spaces for indents. If you show code in your post, use the code tags (use the '#' button above the text field and place your code between those tags).

This is one of the many examples that is just not possible (there is a '{' without '}'):

case 1:if (buttonPushCounter2 %3 == 1)
    {analogWrite(analogOutPin, outputValue);

Also the '}' '{' combination here is not good programming:

    lastButtonState1 = buttonState1;

And please use also a "break" with the "default" in a "case" statement. Also add comment to your code. You have some kind of state machine, and those are hard to understand without proper comments.

 const int reedswitch = 4;//door switch
    case 2:if (buttonPushCounter2 %3 == 2 && reedswitch == LOW);

Why are you comparing the pin number to LOW? Wouldn't you want to compare the state of the pin?

Why are you comparing the pin number to LOW? Wouldn't you want to compare the state of the pin?

Why is the body of the if statement a single semicolon? Might as well delete the whole if statement.