if else question

I’m building a small pump system, when the microswitch is actuated the pump should run clockwise when the microswitch is released the pump should run counterclockwise for two seconds.

This is the code I am using but it has a bug. When I switched the main power on, the pump start spinning forever, when I actuate the microswitch it reverses and also spins forever.

Suggestions would be very helpful!

Regards Peter

Here the code I’m using and a link to the schematic:

int pomp1   = 7;
int pomp2   = 8;
int PWM     = 9;
int microswitch   = 11;



void setup()
{
pinMode(microswitch, INPUT_PULLUP); // Enable internal pull-up resistor on pin 11
pinMode(pomp1, OUTPUT);
pinMode(pomp2, OUTPUT);
pinMode(PWM, OUTPUT);
}

void loop(){
 
if(digitalRead(microswitch) == LOW){ //microswitch on
 digitalWrite(pomp1, HIGH);
 digitalWrite(pomp2, LOW);
 analogWrite(PWM, 255);
}
else{
 digitalWrite(pomp1, LOW);
 digitalWrite(pomp2, HIGH);
 analogWrite(PWM, 255);

 delay(2000);

 digitalWrite(pomp1, LOW);
 digitalWrite(pomp2, LOW);
 analogWrite(PWM, 255);
}
}

schematic:

https://drive.google.com/file/d/0B5U4ayHnW98IbWFSamVaTEd4UVE/view?usp=sharing

Please edit your post to have code tags around the code. See How to use the forum. Makes it a lot easier for us to spot the error.

If you're looking for the duplicate of this thread, I'll save you the hassle and tell you that I've deleted it.

So let's think about what your loop function does:

First you read the pin, and if it is low then you turn the pump on one way. But if it is not LOW (the else part) then you turn the pump on the other way, delay two seconds, and turn it off. Then the loop function repeats. It checks the pin again. The pin is still not LOW so it does it again, turn the pump on delay two seconds and turn it off. This all happens in a matter of microseconds so you never see the pump turn off, it turns back on too quickly.

I think you should take a look at the "State Change Example" that comes with the IDE. You don't want to run the pump if the button IS high you want to run the pump if it has just BECOME high but it was low the last time you ran through loop. This requires that you remember the last time you read the button. The State Change Example illustrates how to do this.

Any sketch that reads a switch needs to be non-blocking. As mentioned the state change example will help.

You also need to think about the different states your machine can be in. It is always going to have to monitor the switch as that effects the states. One state is that it will be doing nothing except waiting for the switch to be closed. While the switch is closed it will be running the pump and waiting for the switch to open. Then the state will be running the other pump (or backwards) and timing that for two seconds waiting to go back to the state of doing nothing but waiting for the switch to close again. This can all be done by timing using millis. You need to think of things happening in a loop rather than a linear script.

Any sketch that reads a switch needs to be non-blocking.

I know what you mean, but that statement is not entirely accurate. If the one and only thing that the program is doing is waiting for the switch to change state it can block to it's heart's content.

The danger is doing that when it doesn't matter in one program then using the same technique when it does matter in another one. Horses for courses.

The same goes for the infamous delay() command. If you really don't care that the program can do nothing else during the delay() period then it is OK to use it. There are more accurate ways to do nothing for up to 49 and a bit days but if delay() will do what you want then why not use it ?

Delta_G Thank you very much you set me on the right track. The code I was using was indeed not very intelligent as you pointed out, it was a forever ongoing loop.

I studied the code you mentioned "State Change Example" and changed it to my situation and everything works flawlessly now.

Below the code I use now, again thank you very much:

int pomp1   = 7;
int pomp2   = 8;
int PWM     = 9;
//int micro   = 11;
int  buttonPin = 11;

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup()
{
pinMode(buttonPin, INPUT_PULLUP); // Enable internal pull-up resistor on pin 11
pinMode(pomp1, OUTPUT);
pinMode(pomp2, OUTPUT);
pinMode(PWM, OUTPUT);

}

void loop(){
  
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == LOW) {
      // if the current state is LOW then the button
      // wend from off to on:
      buttonPushCounter++;
 digitalWrite(pomp1, HIGH);
 digitalWrite(pomp2, LOW);
 analogWrite(PWM, 255);


    } else {
      // if the current state is HIGH then the button
      // wend from on to off:
 digitalWrite(pomp1, LOW);
 digitalWrite(pomp2, HIGH);
 analogWrite(PWM, 255);

 delay(2000);

 digitalWrite(pomp1, LOW);
 digitalWrite(pomp2, LOW);
 analogWrite(PWM, 255);
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;
}

OK, it works, and that is good, but why use int variables that take 2 bytes for values that will never be greater than 255 (1 byte) ? Also, it would make sense for variables whose value will never change in the program, such as pin numbers, to be declared as const.

Another observation, As the value written to the PWM pin is never anything but 255, why do it over and over again in the loop() function as opposed to once in setup() ?

UKHeliBob: Another observation, As the value written to the PWM pin is never anything but 255, why do it over and over again in the loop() function as opposed to once in setup() ?

For that matter why use analogWrite at all if the value will be 255 every time. Just digitalWrite it high and leave it.

For that matter, why use digitalWrite() at all, just connect it to 5V :smiley:

The answer that I was expecting (hoping for) was that some time in the future the PWM pin would be used to control the speed of the motor, but we shall see when/if the OP replies.