Project 11 of the Arduino starter kit is a digital 8-ball. Give the system a shake and it gives a yes/no type answer on the LCD screen. So far, mine hasn’t been wrong even once.

On start-up, the screen shows a message to ask a question. Once shaken, it waits until the shaking is done and then shows a message. However, if left alone it does nothing. After showing a message, it will continue to show the message until shaken again, where it will show a new message. I decided to have it reset after 5 seconds of inactivity and show the intro question.

To achieve this, I used the milli() function shown in project 8, the digital hourglass.

At the top of the program, I defined some variables to keep track of activity, the time, and the interval between resets.

int activity;
unsigned long previousTime = 0;
long interval = 5000;

The setup function has the starting message and sets up the LCD screen, nothing fancy. The loop function has a few modifications, utilizing the variables above to keep track of system status.

void loop() {
  unsigned long currentTime = millis(); //Get the current time
  activity=0; //Assume that there is no activity
  switchState = digitalRead(switchPin); //Get the status of the level sensor
  if(switchState != prevSwitchState){ //If the tilt sensor feels movement
    if(switchState == LOW){
      activity = 1; //There is activity so change the status
      reply = random(10); //Choose a message
      lcd.clear(); //Clear the screen
      lcd.setCursor(0,0); //Set the cursor to the first column, first row
      lcd.print("The ball says:"); //Print to first line
      lcd.setCursor(0,1); //Set the cursor to the first column, second row
         //All of the possible answers
  if(activity == 0){ //If there is no activity in this cycle
    if(currentTime - previousTime > interval){ //If more than the desired time has passed
      lcd.clear(); //Reset the screen
      lcd.setCursor(0,0); //Set cursor to first column, first row.
      lcd.print("Ask the"); //Print
      lcd.setCursor(0,1); //Set cursor to first column, second row.
      lcd.print("Crystal ball!"); //Print
      previousTime = currentTime; //Reset the timer
  else if(activity = 1){ //If there is activity this cycle
    previousTime = currentTime; //Reset the timer
prevSwitchState = switchState; //Keep track of the switch

The gist of the modification is the variable activity acts as a flag. If there is activity, defined by the the tilt sensor feeling movement, the variable previousTime is reset to the current time, setting back the interval. If there is no activity, the program then checks if enough time has passed. If so, we clear the screen, reset the message, and reset the timer. As written, the screen will continue to reset every 5 seconds, but it’s not noticeable so if it ain’t broke, don’t fix it. If the timer is not reset alongside the message, the Arduino will try to reset the screen with each cycle, which doesn’t look very nice.

Also, I learned the Arduino editor has a “Copy text as HTML option” so that’s pretty convenient.