EDITED
The Arduino loop() function makes the use of a while loop redundant in this case. Even worse: the way you've implemented that while loop prevents buttonState from being updated, thus getting stuck in the loop.
What you actually need is to use timers to blink the LED, and a 2-state machine driven by HIGH to LOW button transitions.
Try this instead:
#include "Timer.h"
// Pin 13 has a LED connected on most Arduino boards.
// give it a name:
const int led = 13;
const int buttonPin = 2;
// declare state variablevariables as global
bool buttonState = LOW;
bool buttonState_prev = LOW;
bool toggleBlinking;
bool blinkState;
// declare a Timer object so blinking can be done without loosing button presses
Timer timer_b;
int blink_id;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
// initial state: LOWtoggle led each 1000 ms
digitalWriteblink_id = timer_b.oscillate(led, LOW1000, HIGH);
blinkState = True;
toggleBlinking = False;
}
// the loop routine runs over and over again forever:
void loop()
{
// Update the timer (required)
timer_b.update();
// check if button has been pressed at(HIGH theto startLOW),
of each// loop()then iterationdebounce it and raise toggle flag
buttonState = digitalRead(buttonPin);
// do either one thing orif another,((buttonState according!= tobuttonState_prev) buttonState
&& (buttonState_prev switch== (buttonStateHIGH)) {
case// LOW:
simple button debounce (confirm press after digitalWrite(led,some HIGHms);
// turndelay the(50); LED// onmay (HIGHinterfere iswith the voltage level)timer?
buttonState = delaydigitalRead(1000buttonPin);
if (buttonState != buttonState_prev) toggleBlinking = True;
}
// waitdo foreither aone second
thing or another, according to buttonState
digitalWrite(led, LOWswitch (blinkState); {
// turncase theTrue:
LED off by making the voltageif LOW(toggleBlinking == True) {
delay timer_b.stop (1000blink_id);
blinkState = False;
// waitbreak;
for a second
case False:
break;
if (toggleBlinking case== HIGH:True) {
digitalWrite blink_id = timer_b.oscillate(led, LOW1000, HIGH);
blinkState = True;
break;
}
buttonState_prev = buttonState;
toggleBlinking = False;
}
As you can see, it first checks the value of the button, then executes code according to that.
NOTE: There are better waysYou'll need to implementinstall the functionality you're looking forTimer.h library. Also, but I've just tried to do as few changes as possible to your code so you can grasp the specific problem that has been bugging youI HAVE NOT TESTED THE CODE, TAKE IT AS A FIRST STEP AND FURTHER DEVELOP THE IDEA YOURSELF.