I'm trying to create a "Magic 8-ball" sketch. The LCD should show "Your question please", then once the switch is pressed, it should choose a random number from 0-9 and output the appropriate response. My "if" statement is not working, as soon as the Arduino starts, it shows one of the responses, waits 5 seconds, then shows another response and loops endlessly. The switch on pin 7 has no effect. What am I missing?
void setup()
{
pinMode(7, INPUT); //declare switch as input
digitalWrite(7, HIGH); //connects internal resistor to input, connecting pin 7 to ground will show a high on pin 7
pinMode(backLight, OUTPUT); //declare backlight pin as output
lcd.begin(20,4); //columns, rows
lcd.clear(); //start with blank screen
lcd.setCursor(0,0); //set cursor to column 0, row 0}
void loop()
{
analogWrite(backLight, 85); //turn backlight on
lcd.print("Your question please");//int val = digitalRead(inputPin); //read input value
if (digitalRead(7) == HIGH)
{
lcd.begin(20,4); //columns, rows
lcd.clear(); //start with blank screen
lcd.setCursor(0,0); //set cursor to column 0, row 0
randomNumber = random(0, 9);
switch (randomNumber) {
case 0:
lcd.print("Highly unlikely");
delay(5000);
break;
case 1:
lcd.print("Yes");
delay(5000);
break;
case 2:
lcd.print("No");
delay(5000);
break;
case 3:
lcd.print("Could you repeat thequestion?"); //no space to make output look good
delay(5000);
break;
case 4:
lcd.print("Go ask your mother");
delay(5000);
break;
case 5:
lcd.print("Results are cloudy");
delay(5000);
break;
case 6:
lcd.print("It is almost certain");
delay(5000);
break;
case 7:
lcd.print("Conditions are");
lcd.setCursor(0,1);
lcd.print("almost perfect");
delay(5000);
break;
case 8:
lcd.print("No, no, no, a");
lcd.setCursor(0,1);
lcd.print("thousand times, no!");
delay(5000);
break;
case 9:
lcd.print("I'm checking...");
delay(5000);
}}
else
{delay(1000);};
}