Control the blinking of a LED using serial monitor

Since I am newbie in Arduino and C/C++ programming language (I studied mostly Pascal and HTML/JavaScript in high school and only a couple of classes of C/C++ :expressionless: ), I humbly ask for your advice.
So, I'll leave here the code I wrote for this application that requires an input number "x" in Serial Monitor, by which the LED (the yellow Led in my case) will blink "x" times when button A is pressed. For example, if I type "4" in the Serial Monitor, the led will blink 4 times when I press the button.

Here's the code:

#define buttonA 14
#define LedYellow 13
int blinkingtimes = 0;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(LedYellow, OUTPUT);
pinMode(buttonA, INPUT);
}

void loop() {
// put your main code here, to run repeatedly:
int statusButtonA = 0;

statusButtonA = digitalRead(buttonA);

if (statusButtonA = HIGH){
Serial.print("Introduce the on-off commutation number of the yellow LED ");
if(Serial.available() > 0){
blinkingtimes = Serial.read();
}
while (blinkingtimes > 0){
digitalWrite(LedYellow, HIGH);
delay(500);
blinkingtimes = blinkingtimes - 1;
}
}
}

Thank you in advance!
Have a great week ahead!

1 Like
 if(Serial.available() > 0){
      blinkingtimes = Serial.read();
    }

A single ASCII character?

Please remember to use code tags when posting code

if (statusButtonA = HIGH){

That code assigns the value HIGH to statusButtonA. You want to compare '=='

Also note that when you read in one character into blinkingTimes, you get the ASCII value of the character. If you enter in "1", the ascii value is 49 so that is how many times it will blink. The next character after the one you enter will most likely be a carriage return or a linefeed, both of which you probably don't want. It would be best to read one character, make sure it is within the range you want (1-9) and then convert it to a number.