1

I have an Arduino and a microswitch that I want to use to know when the switch is initially being pressed (get "OnsetTime" on Serial) and when the switch is not being pressed anymore (get "OffsetTime" on Serial).

Imagine a bird sitting on the switch. I want to get an output "OnsetTime" when the bird first sits on the switch and an output "OffsetTime" when the bird flies away from the switch.

Right now, I have an output that constantly prints out "OnsetTime" when the switch is pressed. I would appreciate it if anybody can help me figure out how can I implement the below code to get the outputs only when the switch is initially pressed and not being pressed anymore.

void setup() {
  pinMode(7, INPUT);
  Serial.begin(9600);
}

void loop() {  
  if (digitalRead(7) == HIGH) {
    Serial.println("OnsetTime");
    }
}
    //Serial.println("GetOffsetTime");

2 Answers 2

1

You can save the value of the switch read to compare it on the next loop. This way you're able to detect changes.

int lastValue;

void setup() {
  pinMode(7, INPUT);
  lastValue= digitalRead(7);

  Serial.begin(9600);
}

void loop() {
  int currentValue = digitalRead(7);
  if (lastValue != currentValue) {
    lastValue = currentValue;

    if (currentValue == HIGH) {
      Serial.println("OnsetTime");
    } else {
      Serial.println("OffsetTime");
    }
  }
}

Please be aware that switches bounce. If you see multiple messages at each mechanical change, insert a delay(...).

1

If you want to be simple, you can use this library. The code will be

#include <ezButton.h>

ezButton button(7);  // create Button object that attach to pin 7;

void setup() {
  Serial.begin(9600);
  button.setDebounceTime(100); // set debounce time to 100 milliseconds
}

void loop() {
  button.loop(); // MUST call the loop() function first

  if(button.isPressed())
    Serial.println("OnsetTime");

  if(button.isReleased())
    Serial.println("GetOffsetTime");
}

If you want to understand in depth, You can see the code and instruction in this tutorial

6
  • Hi. This gives me the error: 'class Button' has no member named 'setDebounceTime'
    – John
    Commented Dec 18, 2019 at 7:09
  • did you install ezButton library ? if not, follow this instruction to install libraray Commented Dec 18, 2019 at 7:21
  • Yes, I did. This is an unusual case so I'm a bit confused. Did you try it and does it work?
    – John
    Commented Dec 18, 2019 at 7:30
  • Yes, I tried, it works. I think, you may install multiple Button libraries. Please check it. Or please run example code from library. If still happened, which Arduino are you using? Commented Dec 18, 2019 at 7:43
  • Do you mean I'd want to delete other Button libraries if I have any? I am using Arduino Uno.
    – John
    Commented Dec 18, 2019 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.