So I'm learning Arduino via Youtube and was trying to modify this code a little and experiment.
The program worked fine up until I added the variable user inputs. It will sit there displaying the first two lines of text as expected and I enter an number, then somehow a 0 get put in for the input for the second variable, and then it sits and waits for the third input, skips the fourth just like the second and waits for the fifth input.
Anyone know how to correct this so it doesn't put a 0 in for input 2 and 4?
Oh btw the program is to ask a user how many times a set of 5 LEDs should blink
Thanks in advance for anyone who responds
This is what is displayed
"Welcome to my Program
How many times do you want the Red LED to blink?
2
How many times do you want the Green LED to blink?
0
How many times do you want the Blue LED to blink?
3
How many times do you want the Yellow LED to blink?
0
How many times do you want the White LED to blink?"
/*This program is designed to ask a user for input on how many times each of 5 LED's should blink, start program and continue in that pattern*/
int redLED=9; // declare pin 9 as the red LED
int greenLED=10; // declare pin 10 as yellow LED
int blueLED=11; //Blue LED to pin 11
int yellowLED=2; //yellow LED to pin 2
int whiteLED=3; //white LED to pin 3
int redOnTime=170; // Red LED on time
int redOffTime=50; //Red LED off time
int greenOnTime=170; // Green LED on time
int greenOffTime=50; // Green LED off time
int numRedBlink; // Number of times to blink LED
int numGreenBlink; // Number of times to blink LED
int numBlueBlink; // Number of times to blink LED
int numYellowBlink; // Number of times to blink LED
int numWhiteBlink; // Number of times to blink LED
int blueOnTime=170; // blue LED on time
int blueOffTime=50; //blue LED off time
int yellowOnTime=170; // yellow LED on time
int yellowOffTime=50; //yellow LED off time
int whiteOnTime=170; // white LED on time
int whiteOffTime=50; //white LED off time
String redMessage="The Red LED is blinking";
String greenMessage="The Green LED is blinking";
String blueMessage="The Blue LED is blinking";
String yellowMessage="The Yellow LED is blinking";
String whiteMessage="The White LED is blinking";
void setup() {
Serial.begin(9600);
String wm1= "Welcome to "; //Declare a string variable wml and assign a value
String wm2= "my Program"; //Declare string variable wm2 and assign a value
String wm3; // Declare string variable wm3
wm3= wm1+wm2; // concatanating wm1 and wm2 into wm3
Serial.println(wm3);
pinMode(redLED,OUTPUT);
pinMode(greenLED,OUTPUT);
pinMode(blueLED,OUTPUT);
pinMode(yellowLED,OUTPUT);
pinMode(whiteLED,OUTPUT);
Serial.println("How many times do you want the Red LED to blink?"); //Ask for input
while(Serial.available()==0){ } //Twiddle thumbs until you receive input
numRedBlink = Serial.parseInt(); //Read user input
Serial.println(numRedBlink); //Displays number user inputed
Serial.println("How many times do you want the Green LED to blink?"); //Ask for input
while(Serial.available()==0){ } //Twiddle thumbs until you receive input
numGreenBlink = Serial.parseInt(); //Read user input
Serial.println(numGreenBlink); //Displays number user inputed
Serial.println("How many times do you want the Blue LED to blink?"); //Ask for input
while(Serial.available()==0){ } //Twiddle thumbs until you receive input
numBlueBlink = Serial.parseInt(); //Read user input
Serial.println(numBlueBlink); //Displays number user inputed
Serial.println("How many times do you want the Yellow LED to blink?"); //Ask for input
while(Serial.available()==0){ } //Twiddle thumbs until you receive input
numYellowBlink = Serial.parseInt(); //Read user input
Serial.println(numYellowBlink); //Displays number user inputed
Serial.println("How many times do you want the White LED to blink?"); //Ask for input
while(Serial.available()==0){ } //Twiddle thumbs until you receive input
numWhiteBlink = Serial.parseInt(); //Read user input
Serial.println(numBlueBlink); //Displays number user inputed
}
void loop() {
Serial.println(redMessage);
for (int j=1; j<=numRedBlink; j=j+1) {
Serial.print("You are on blink #:");
Serial.println(j);
digitalWrite(redLED,HIGH); // Turn the red LED on
delay(redOnTime);
digitalWrite(redLED,LOW); // Turn off the red LED
delay(redOffTime);
}
Serial.println(" ");
Serial.println(greenMessage);
for (int k=1; k<=numGreenBlink; k=k+1) {
Serial.print("You are on blink #:");
Serial.println(k);
digitalWrite(greenLED,HIGH); // Turn the green LED on
delay(greenOnTime);
digitalWrite(greenLED,LOW); // Turn off the green LED
delay(greenOffTime);
}
Serial.println(" ");
Serial.println(blueMessage);
for (int m=1; m<=numBlueBlink; m=m+1) {
Serial.print("You are on blink #:");
Serial.println(m);
digitalWrite(blueLED,HIGH); // Turn the blue LED on
delay(blueOnTime);
digitalWrite(blueLED,LOW); // Turn off the blue LED
delay(blueOffTime);
}
Serial.println(" ");
Serial.println(yellowMessage);
for (int n=1; n<=numYellowBlink; n=n+1) {
Serial.print("You are on blink #:");
Serial.println(n);
digitalWrite(yellowLED,HIGH); // Turn the yellow LED on
delay(yellowOnTime);
digitalWrite(yellowLED,LOW); // Turn off the yellow LED
delay(yellowOffTime);
}
Serial.println(" ");
Serial.println(whiteMessage);
for (int o=1; o<=numWhiteBlink; o=o+1) {
Serial.print("You are on blink #:");
Serial.println(o);
digitalWrite(whiteLED,HIGH); // Turn the white LED on
delay(whiteOnTime);
digitalWrite(whiteLED,LOW); // Turn off the white LED
delay(whiteOffTime);
Serial.println(" ");
}
}
sketch_may05b.ino (5.93 KB)