Noob help: Cases and timer

Hey guys,

Just to start off, I am a complete noob and just started using a simulator while I waiting for my Arduino starter kit to arrive. I would like your advice on how to best set up my code.

I am trying to get 6ish cases to display on an LCD screen, each incrementing a counter depending on the lcd.print (millis() / X),

However with the current code, the cases seem to be running and are displayed simultaneously which is not what I want.

I would love your advice on how to set it up so that each randomly selected case runs after each other, having been been displayed for a certain time (ex. 30-60 seconds). I have tried to use the millis() function to decide, and even tried with:

startMillis;
and
currentMillis;

But cannot seem to get this to work so deleted them pending some more info/guidance so that I understand what I am doing.

Ideally, the loop would stop after around 10-20mins, depending on how much is incremented to the screen (ex. 15,000). The idea is that each random case provides a higher/lower count on the screen depending on the milis()/X displayed, so the exact timing is hard to calculate. Any advice on how to implement this would also be much appreciated.

Apologies for the simple questions, but coding has never been my strong suit.

Below is the current code:

#include <LiquidCrystal.h>

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 1000;

void setup() {
  
}

void loop()
{
   byte randNum = random(0, 4);
   switch (randNum)
   {
       case 1:
       {
         lcd.begin(16, 2);
         lcd.print("Test1!");
         lcd.setCursor(0, 1);
         lcd.print (millis() / 500);
     	 currentMillis = millis();
         delay(250);
         break;
       }
      case 2:
       {
         lcd.begin(16, 2);
         lcd.print("Test2!");
  		 lcd.setCursor(0, 1);
 	 	 lcd.print (millis() / 500);	
         delay(250);
    	 break;
       }
      case 3:
       { 
         lcd.begin(16, 2);
         lcd.print("Test3!");
  		 lcd.setCursor(0, 1);
 	 	 lcd.print (millis() / 500);
         delay(250);
     	 break;
       }
   }
}

You're picking a new random case every 1/4 second. Is that what you wanted?

1 Like

You might want to elaborate a bit more on your requirements. I've read it a few times and don't understand what you want to achieve.

One comment though
lcd.begin() belongs in setup(), not in loop(). If the reason is that you want to clar the display, use lcd.clear().

1 Like

Start by calling lcd.begin() only once and do it in setup()

Remove the braces around the code for each case. They are not needed

As written you will get a new random case number every 250 milliseconds and the associated message will be printed on the LCD. What is it that you want to happen ?

I note that you do not have any code for case 0: of randNum which will occur one in 4 times when loop() repeats

1 Like

Add
delay(20000ul);
just before last
}

1 Like

Thanks a lot for the help!

So to clear things up, I am trying to get a loop of four random cases with text show up on the top row of the LCD screen, incrementing the number on the bottom part of the LCD screen in different speeds (ex. lcd.print (millis() / 500) vs lcd.print (millis() / 1200)).

I want these cases to be random and with text shown on the screen at random times (ex 20-30sec), but the counter on the lower LCD row still showing the correct amount.

I have tweeked my code as per your suggestions, and managed to get things running without an error. However, I am a bit confused as what I was doing with the delay(20000ul) at the end. Will this stop the counter at 20,000?

As of now, the figure on the screen seems to be incrementing correctly, however jumping up chunk by chunk which I assume have to do with me using the delay function rather than some other function to keep each case within a wanted time interval. Any suggestions on how to better do this?

In the future I hope to implement an active buzzer for each increment of the figure on the LCD screen, so any suggestions on how to do that with the current code would be greatly appreciated. If it does not fit with the current code, then no worries! I am a bit in above my head.

Thanks! And again, much appreciated help :slight_smile:

Current code


#include <LiquidCrystal.h>


const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
  randomSeed(analogRead(0));
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("StartScreen!");
  delay(2000);
}

void loop()
{
   byte randNum = random(1, 3);
   switch (randNum)
   {
       case 1:
       {
         lcd.clear();
         lcd.print("Test1!");
         lcd.setCursor(0, 1);
         lcd.print (millis() / 500);
         delay(random(20000, 30000));
         break;
       }
      case 2:
       {
         lcd.clear();
         lcd.print("Test2!");
  		 lcd.setCursor(0, 1);
 	 	 lcd.print (millis() / 500);	
         delay(random(20000, 30000));
    	 break;
       }
      case 3:
       { 
         lcd.clear();
         lcd.print("Test3!");
  		 lcd.setCursor(0, 1);
 	 	 lcd.print (millis() / 500);
         delay(random(15000, 30000));
     	 break;
       }
   }
delay(20000ul);
}

It will pause the program for 20000 milli seconds.
So 20 seconds...
The ul are not needed here as 20000 will fit into a 16 bit int.
Since you have long delays in your cases now, this might not be needed...

1 Like

Your random number will never be 3....
You should have random(1,4);
Also you might want a seed for your random numbers as you will find out that random numbers are far from random...
You can read a floating pin to get a seed.
Or measure the time it takes to push a button...

1 Like

Ahh got it! Thanks! So might not need it then since I got the delays implemented after each case?

Haha yeah figured that out after a while and changed to random(1,4);

Now I got to read up on using a floating pin to get a seed for the randomness, and also find a smart way to display the amount as it is incremented rather than incremented as chucks after each delay.

Starting to get a hang of it at least, even though there is a long way to go, with much to learn :slight_smile:

Cheers again, have a wonderful night!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.