This is exactly what I want to do except only on the released-to-pressed state. I need help on how to do it.
int currState = HIGH;
int prevState = HIGH;
void loop()
{
currState = digitalRead(somePin);
if(currState != prevState)
{
// A transition occurred
if(currState == LOW)
{
// Switch is now pressed
}
else
{
// Switch is now released
}
}
prevState = currState;
}
If you are using pullup resistors, this code should give you an idea. If you are using pulldown resistors, change all LOWs to HIGHs, and all HIGHs to LOWs, and ask yourself why you are using external hardware, when the Arduino has built in (pullup) resistors.