An active high digital input reads as true/1/HIGH when the
attached digital sensor is active (e.g. button pressed, door
open, engine overheated, or motion detected). Read the state of an active high
digital input with digitalRead(pin).
#define SENSOR1 2 // Some kind of digital sensor connected to D2
void setup()
{
pinMode(SENSOR1, INPUT);
}
void loop()
{
// Persistent logic state variable, value preserved between calls
// to loop()
static bool oldstate;
// Nonpersistent logic state variable, value NOT preserved between
// calls to loop()
bool newstate;
// Read digital input state into a logic state variable
newstate = digitalRead(SENSOR1);
// Do something if the sensor input is active
if (newstate)
{
DoSomething();
}
// Do something if the sensor input is active (do not save state)
if (digitalRead(SENSOR1))
{
DoSomething();
}
// Do something if the sensor input is active (save state)
if (newstate = digitalRead(Sensor1))
{
DoSomething();
}
// Do something if the sensor input CHANGES
if (newstate != oldstate)
{
DoSomething();
}
// Save logic state for next call to loop()
oldstate = newstate;
}