Or will this need to be approached on the coding side?
It needs to be handled in code, but it is really simple. Keep track of the previous state of the switch. Compare that to the current state. If they are different, the switch was either just pressed or just released.
The current state of the switch will tell you which transition occurred. You could make the action occur when the switch is released, so it doesn't matter how long the switch is held.
You can make it happen when the switch is just pressed, to, since the transition from released to pressed can't happen until the switch goes back to the released state.
int currState;
int prevState = HIGH;
void loop()
{
currState = digitalRead(switchPin);
if(currState != prevState)
{
// A transition occurred
if(currState == LOW)
{
// From released to pressed
}
else
{
// From pressed to released
}
}
prevState = currState;
}
This assumes the use of pullup resistors. If using pulldown resistors, change LOW to HIGH and HIGH to LOW. If not using any resistor, never mind, since you'll never get a valid reading from the switch.