Puzzle selector and timer

i am trying to create a program that will display on an LCD screen. Here are the parameters of the program.

  1. Welcome Screen.
  2. Select one of three Puzzles.
  3. If press a start button within 10 secs.
    -----if start not press, return to welcome screen.
    -----if start pressed, go to a countdown.
  4. each puzzle has a different time.

this is what I have so far as the code but what i need help with is with the start button.
I have the Welcome Screen, Puzzle is selected, Timer for start button to be pressed, if timer = 0 - goes back to welcome screen. When i press the start button, nothing happens. Please let me know what I can do to get the program working. Thanks.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12,11,10,9,8,7);
const int numRows=4;
const int numCols=16;
unsigned long time;

#define ON 0
#define READY 1
#define START 2
#define COUNTDOWN 3
#define STOPPED 4

int count;
int pState = 0;
int startPin = 3; //START pushbutton
int stopPin = 2; //STOP pushbutton
int ponePin = 4; //Puzzle 1 pushbutton
int ptwoPin = 5; //Puzzle 2 pushbutton
int pthreePin = 6; //Puzzle 3 pushbutton

void setup()
{
Serial.begin(9600);
lcd.begin(numRows, numCols);
}

void welcome()
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" WELCOME “);
lcd.setCursor(0,1);
lcd.print(” PLEASE SELECT “);
lcd.setCursor(0,2);
lcd.print(” A PUZZLE ");
delay(500);
pState = READY;
}

void puzzleselected()
{
int P1 = digitalRead(ponePin);
int P2 = digitalRead(ptwoPin);
int P3 = digitalRead(pthreePin);

if(P1 == HIGH)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Puzzle 1 Selected “);
lcd.setCursor(0, 1);
lcd.print(” 1:00 MIN “);
lcd.setCursor(0, 2);
lcd.print(” Press START “);
}
else if(P2 == HIGH)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(” Puzzle 2 Selected “);
lcd.setCursor(0, 1);
lcd.print(” 2:00 MIN “);
lcd.setCursor(0, 2);
lcd.print(” Press START “);
}
else if(P3 == HIGH)
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(” Puzzle 3 Selected “);
lcd.setCursor(0, 1);
lcd.print(” 3:00 MIN “);
lcd.setCursor(0, 2);
lcd.print(” Press START ");
}
pState = START;
}

void countdown()
{
int P1 = digitalRead(ponePin);
int P2 = digitalRead(ptwoPin);
int P3 = digitalRead(pthreePin);

if(P1 == HIGH)
{
lcd.clear();
lcd.setCursor(0,1);
lcd.print(" Puzzle 1 Started “);
lcd.setCursor(0,2);
lcd.print(” countdown “);
delay(1000);
}
else if(P2 == HIGH)
{
lcd.clear();
lcd.setCursor(0,1);
lcd.print(” Puzzle 2 Started “);
lcd.setCursor(0,2);
lcd.print(” countdown “);
delay(1000);
}
else if(P3 == HIGH)
{
lcd.clear();
lcd.setCursor(0,1);
lcd.print(” Puzzle 3 Started “);
lcd.setCursor(0,2);
lcd.print(” 00:00 ");
delay(1000);
}
}

void loop()
{
int P1 = digitalRead(ponePin);
int P2 = digitalRead(ptwoPin);
int P3 = digitalRead(pthreePin);
int start = digitalRead(startPin);
int stop = digitalRead(stopPin);

switch (pState)
{
/*
Welcome Screen
/
case ON:
if(P1 == LOW && P2 == LOW && P3 == LOW)
{
welcome();
}
else
{
pState = READY;
}
break;
/

READY - Puzzle Selected
/
case READY:
if(P1 == HIGH)
{
digitalWrite(ponePin, HIGH);
puzzleselected();
}
else if(P2 == HIGH)
{
digitalWrite(ptwoPin, HIGH);
puzzleselected();
}
else if(P3 == HIGH)
{
digitalWrite(pthreePin, HIGH);
puzzleselected();
}
break;
/

Wait for START to be pressed
*/
case START:
for(count = 10; count >= 0; count–)
{
digitalRead(startPin);

lcd.setCursor(1, 3);
lcd.print(“RESET in”);
lcd.setCursor(14, 3);
lcd.print(“00:”);
time = millis();
if(count <= 9)
{
lcd.print(‘0’);
}
lcd.print(count);
delay(1000);

if(count == 0);
pState = ON;

if(start == HIGH);
pState = COUNTDOWN;

}
break;

case COUNTDOWN:
if(P1 == HIGH)
{
countdown();
}
else if(P2 == HIGH)
{
digitalWrite(ptwoPin, HIGH);
countdown();
}
else if(P3 == HIGH)
{
digitalWrite(pthreePin, HIGH);
countdown();
}
break;

case STOPPED:
lcd.clear();
lcd.setCursor(0,1);
lcd.print(" CONGRATULATIONS “);
lcd.setCursor(0,2);
lcd.print(” PUZZLE SOLVED ");
delay(1000);
//insert delay time
break;
}//Closes "Switch
}//Closes “loop”

Switch_3.ino (3.98 KB)

i think its somewhere in here:

case START:
    for(count = 10; count >= 0; count--)
    {
      digitalRead(startPin);

      lcd.setCursor(1, 3);
      lcd.print("RESET in");
      lcd.setCursor(14, 3);
      lcd.print("00:");
      time = millis();
      if(count <= 9)
      {
        lcd.print('0');
      }
      lcd.print(count);
      delay(1000);

      if(count == 0);
      pState = ON;

      if(start == HIGH);
      pState = COUNTDOWN;

    }
    break;

should be :

case START:
    for(count = 10; count >= 0; count--)
    {
      digitalRead(startPin);

      lcd.setCursor(1, 3);
      lcd.print("RESET in");
      lcd.setCursor(14, 3);
      lcd.print("00:");
      time = millis();
      if(count <= 9)
      {
        lcd.print('0');
      }
      lcd.print(count);
      delay(1000);

      if(count == 0)
      {
          pState = ON;
      }

      if(startPin == HIGH)
      {
          pState = COUNTDOWN;
      }

    }
    break;

see the if statement format
and
if(start == HIGH), i think should be if(startPin == HIGH)

i just tried it but it won't go to the next state. I replaced it with the STOPPED case like below and it worked but only after the count was finished. It wouldn't jump to it as soon as the button was pressed. I tried to use interrupts but it wouldn't work for the countdown of the puzzle. Interrupts did and will work for the STOP case.

if(count == 0);
pState = ON;

if(startPin == HIGH);
pState = STOPPED;

" It wouldn’t jump to it as soon as the button was pressed."

of course, you are using a for loop so its “stuck” inside the current case until the for loop is done

case START:
    int count = 10;
    while(count >= 0 && pState =START) 
    {
      digitalRead(startPin);

      lcd.setCursor(1, 3);
      lcd.print("RESET in");
      lcd.setCursor(14, 3);
      lcd.print("00:");
      time = millis();
      if(count <= 9)
      {
        lcd.print('0');
      }
      lcd.print(count);
      delay(1000);

      if(count == 0)
      {
          pState = ON;
      }

      if(startPin == HIGH)
      {
          pState = COUNTDOWN;
      }

       count--;
    }

while(count >= 0 && pState !=COUNTDOWN) //once start button is pressed, pstate will equal COUNTDOWN and the while loop will stop and program will goto next case

and
if statements are like:

if(startPin == HIGH)
{
    pState = COUNTDOWN;
}

NOT:

if(startPin == HIGH);
pState = STOPPED;
      digitalRead(startPin);

You are reading the state of the pin, and discarding the result.

Do you even know that you are reading the switches correctly? That's an awful lot of code to have written without testing that. Internal pullup resistors are not being used, so external resistors (pull-down with that code) are needed. You do have them, right?

All externals are configured properly. I have tested the parts individually but when i try to mash it together, it won't do as I plan. It will either just freeze at the the START portion.

hmm always seems to do that , huh ? :smiley:

even with the while loop? you said that it worked before but it wouldn't change till after the countdown. changing to the while loop should have fixed that.

I was able to get the looping to stop with this;

for(count = 10; count >= 0 && digitalRead(startPin) == LOW; count --)

this worked for me and i wish i would have though it it sooner. When the Start is pressed, it holds the count for the delay and then goes into the function that i call it to after the delay. Thanks for all the help and advice though.