This is a bit vague. As sayzer asks what kind of switch? A standard pushbutton doesn’t latch, so it only indicates a “1” while pressed, and “0” when released.3) After click pressed button the green LED will be flashed,
4) After that we pressed again the red LED will be flashed again.
A pushbutton that latches will cost more then a button that doesn’t remember it’s state.
So.. to implement the cheaper switch you’ll need to A) debounce it and 2) remember the previous state.
Debounce simply means read it several times to insure you have a stable state. Typically a switch will only bounce in one direction, meaning it bounces ( rapidly closes and opens several times) when pressed but breaks cleanly; however, this may change over a lot or over the lifetime of the device, so best to debounce both ways.
I usually debounce a switch by reading it every 5 ms for 10 times. If I get the same reading every time, I accept the reading. If I get a different reading in any sample, I throw them all away and start over. This has worked well for me for the most part, the switch still responds quickly (typ 50 ms or so) so the person pressing it gets good feedback, and slow enough to get around any bounces.
Remembering the previous state just means saving it in a variable.
Bookmarks