Hello, i have this code and what i'm trying to do is when i click at the button A the serial monitor will ask how many times i want the LED to blink and with that information the led will blink x times we choose, its now working well, when i click at button the led keeps blinking without stop, where it could be the problem?
int ledPin = 6;
int buttonApin = 11;
int buttonBpin = 10;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonApin, INPUT_PULLUP); //means that the pin is to be used as an input, but that if nothing else is connected to the input it should be 'pulled up' to HIGH
pinMode(buttonBpin, INPUT_PULLUP); //In other words, the default value for the input is HIGH, unless it is pulled LOW by the action of pressing the button
Serial.begin(1200);
while (! Serial);
}
void loop()
{
if (digitalRead(buttonApin) == LOW)
{
Serial.available();
Serial.println("How many times do you want the LED to blink?");
char ch = Serial.read();
int led = ch;
for (int i=0; i=led; i++)
{
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
}
}
Please use code tags when posting code... these things in the editor "</>".
This is the source of your problem. i=led should be i <= led
You should also be checking to see if there is anything available to read from the monitor. Serial.read() will return -1 if there is nothing available.
Often if (Serial.available() > 0) is used to check if there is something available before reading.
Your topic has been moved to a more suitable location on the forum. Introductory Tutorials is for tutorials that e.g. you write, not for questions. Feel free to write a tutorial once you have solved your problem
int ledPin = 6;
int buttonApin = 11;
int buttonBpin = 10;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonApin, INPUT_PULLUP); //means that the pin is to be used as an input, but that if nothing else is connected to the input it should be 'pulled up' to HIGH
pinMode(buttonBpin, INPUT_PULLUP); //In other words, the default value for the input is HIGH, unless it is pulled LOW by the action of pressing the button
Serial.begin(1200);
while (! Serial);
}
void loop()
{
if (digitalRead(buttonApin) == LOW)
{
Serial.available();
Serial.println("How many times do you want the LED to blink?");
char ch = Serial.read();
int led = ch;
for (int i = 0; i = led; i++)
{
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
}
}
It seems to be a school assigment, isn´t it?
What type of Arduino are you using?
Have a nice day and enjoy programming in C++ and learning.
Apart from what @red_car said about the = vs <=, it's not going to work with reading the character in like that anyway. Serial.read() reads the character in as a character in the Ascii table and that's the value you'll get when you do the blinking.
So if you type in the value "5" from the keyboard and print the value of "i" each time through the for(), you will get this:
... because the Ascii value of "5" in the Ascii table is 53.
That's how many blinks you'll get.
You need to get your hands on the input as a number, by using for example parseInt.
It has the added benefit of allowing you to read multi-digit numbers like 42- your current way would read that in as two characters, a 4 and a 2 not the number that we know as forty-two.
edit: Or a quick-n-dirty fix is to subtract 48 from the character code, eg 53-48=5, for single digit input.
Alternatively, example 4 here shows how to gather many keypresses into one string and then convert that to a number using atoi.