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");