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


user60469: 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

user60431: 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(...).

61682: <p>I have an Arduino and a <a href="https://uk.rs-online.com/web/p/microswitches/1594590/" rel="nofollow noreferrer">microswitch</a> 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). </p>

<p>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.</p>

<p>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.  </p>

<pre><code>void setup() {
  pinMode(7, INPUT);
  Serial.begin(9600);
}

void loop() {  
  if (digitalRead(7) == HIGH) {
    Serial.println("OnsetTime");
    }
}
    //Serial.println("GetOffsetTime");
</code></pre>
 60469: <p>If you want to be simple, you can use <a href="https://www.arduinolibraries.info/libraries/ez-button" rel="nofollow noreferrer">this library</a>. The code will be</p>

<pre><code>#include &lt;ezButton.h&gt;

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");
}
</code></pre>

<p>If you want to understand in depth, You can see the code and instruction in <a href="https://arduinogetstarted.com/tutorials/arduino-button#content_modifying_arduino_code" rel="nofollow noreferrer">this tutorial</a></p>
 60431: <p>You can save the value of the switch read to compare it on the next loop. This way you're able to detect changes.</p>

<pre><code>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");
    }
  }
}
</code></pre>

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