One button, two states

Hi!

I want to have one button do two things, or maybe three First, when the button is not pressed, nothing happen, but when I press the button, and hold it in, action A starts, and when I release the button, action B starts, and after action B ends, I want the Arduino to go to "sleep", but "wake up" when I press the button again, and the whole loop starts again.

Like this:

Nothing is pressed, nothing happen

Button is pressed, Action A goes as long the button is pressed Button is released, Action B goes for 5000ms Nothing happens again


How can I do this?

I want to have one button do two things, or maybe three

Permission granted. Write some code. If you run into difficulties, post your code here, and explain what the problems are.

int button = 2;
int led = 5;
int buttonState = 0;
int pump = 3;

void setup() {
 // put your setup code here, to run once:
 pinMode(led, OUTPUT);
 pinMode(button, INPUT);
 pinMode(pump, OUTPUT);

}

void loop() {
 // put your main code here, to run repeatedly:
buttonState = digitalRead(button);

if (buttonState == HIGH) {
 digitalWrite(led, HIGH);
 delay(50);
 digitalWrite(led, LOW);
 delay(50);
} else {
 digitalWrite(led, LOW);
 digitalWrite(pump, HIGH);
delay(5000);
 digitalWrite(pump, LOW);
}
}

-- The pump only stopes when I push in the button, and it's a delay from I push the button to the "blink" is happening

There's a delay between pushing the button and the response? Gee I wonder what caused the delay? Maybe this line?

delay(5000);

Study the blink without delay sketch in the examples or any of the hundreds of tutorials on it. You can't use delay if you want things to be responsive.

Please edit the post to use code-tags, see How to use the forum.

Next, have a look at the state change detection sketch in the examples :) A button becoming released isn't the same as a button that IS released ;)

Thanks for help!

I managed to make this code, and it works

const int button = 2;
const int led = 5;
const int pump = 3;

int buttonPush = 0;
int buttonState = 0;
int lastButtonState = 0;
void setup() {
  // put your setup code here, to run once:
pinMode(led, OUTPUT);
pinMode(button, INPUT);
pinMode(pump, OUTPUT);
Serial.begin(9600);
}

void loop() {
  buttonState = digitalRead(button);

  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {

      buttonPush++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPush);

    } else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
      buttonPush++;
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }

  lastButtonState = buttonState;
  
if (buttonPush == 1) {
  digitalWrite(led, HIGH);
  delay(50);
  digitalWrite(led, LOW);
  delay(50);
} else {
  digitalWrite(led, LOW);
  digitalWrite(pump, LOW);
  
}
if (buttonPush == 2) {
  delay(500);
  digitalWrite(pump, HIGH);
  delay(5000);
  digitalWrite(pump, LOW);
  buttonPush--;
  buttonPush--;
  }
}

Not sure if this is the right way to do it, so if you have a better way to make this code, please tell me :slight_smile:

Those delays will still cause things to be non-responsive should you press the button while the processor is waiting through the delay.