Switch Case halting sketch

I'm using a Nano with a TFT display communicating via SPI. I'm trying to display the lyric's of a song. I'm using a switch case function to write each verse on the screen. I have a delay set for 2 seconds, every 2 seconds it should increment the switch case variable (verse_number).

When I run the code the time delay doesn't appear to be working and does not increment the variable, I have a Serial.print statement to monitor it. Halting the program may not be the correct term but it doesn't do anything. If I comment out the switch case statement then the time delay works and the variable increments.

I've used the switch case statement before, I've compared what I have to the previous project and checked the reference on it.

Probably doing something dumb but this dummy can't figure it out.

Here's the code

// mp3 player with TFT display
// Rev 1, display lyrics

#include <SPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_HX8357.h"

// These are 'flexible' lines that can be changed
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8 // RST can be set to -1 if you tie it to Arduino's reset

// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC, TFT_RST);

int verse_number = 0;
long int delay_time = millis();
long int test = 0;



void setup() {
#include <SPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_HX8357.h"

Serial.begin(9600);
 
// These are 'flexible' lines that can be changed
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8 // RST can be set to -1 if you tie it to Arduino's reset

// Color definitions
#define BLACK    0x0000
#define BLUE     0x001F
#define RED      0xF800
#define GREEN    0x07E0
#define CYAN     0x07FF
#define MAGENTA  0xF81F
#define YELLOW   0xFFE0 
#define WHITE    0xFFFF


// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC, TFT_RST);

 tft.begin();
  tft.fillScreen(BLACK); //clears screen, sets to Black
  tft.setRotation(3);  // rotates screen 90' for landscape mode

} // end setup

void loop() {
  // cycle through verses

// Verse delays
if ((millis() - delay_time) > 2000){
  verse_number = verse_number + 1;
  Serial.println(verse_number);
  delay_time = millis();
} // end ((millis() - delay_time) > 2000)


switch (verse_number){
  case 1:
    tft.fillScreen(BLACK); //clears screen, sets to Black
    tft.setCursor(10, 70);
    tft.setTextColor(WHITE);
    tft.setTextSize(2);
    tft.print("Opening Screen"); 
    Serial.println("case 1");
    
   break;

  case 2:
   tft.fillScreen(BLACK); //clears screen, sets to Black
  
    // Print verse 1
  tft.setCursor(10, 70);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("Every day is so wonderful"); 

  tft.setCursor(10, 100);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("Then suddenly it's hard to breathe");

  tft.setCursor(10, 130);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("Now and then I get insecure");

  tft.setCursor(10, 160);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("From all the pain");

  tft.setCursor(10, 190);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("I'm so ashamed");
  
  break;

 case 3:

  tft.fillScreen(BLACK); //clears screen, sets to Black
   
    // Print verse 2
  tft.setCursor(10, 70);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("I am beautiful no matter what they say"); 

  tft.setCursor(10, 100);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("Words can't bring me down");

  tft.setCursor(10, 130);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("I am beautiful in every single way");

  tft.setCursor(10, 160);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("Yes, words can't bring me down, oh no");

  tft.setCursor(10, 190);
  tft.setTextColor(WHITE);
  tft.setTextSize(2);
  tft.print("So don't you bring me down today");

  break;

 default:
  tft.fillScreen(BLACK); //clears screen, sets to Black
    
    tft.setCursor(10, 70);
    tft.setTextColor(WHITE);
    tft.setTextSize(2);
    tft.print("Ending Screen"); 
} // end switch

} // end loop

Thanks for any help
John

What does your serial output say?

Taking a chance....

Could You, just for testing, go for a 5 second period instead of 2 seconds?

I get the same thing at 5 seconds. With the switch case enabled, I get nothing on the serial monitor. If I comment out the switch case then every 5 seconds, or 2, the serial monitor shows the variable incrementing by 1.

thanks

millis() requires unsigned long

Loop () starts with this conditional
and then pops out to the switch-case --

I think that you want to enter the switch-case only once per verse change.
If (some condition/s) then proceed to switch-case.

#include "Adafruit_HX8357.h"

Can you run the library example for the display?

char *verse[] = {"zero", "one", "two", "three", "four"};
byte verseNumber;
const unsigned long verseChange = 2000;
unsigned long markTime;
unsigned long currentTime;

void setup() 
{
  Serial.begin(19200);
  Serial.println(verse[verseNumber]);
}

void loop() 
{
  currentTime = millis();
  
  if((currentTime - markTime) > verseChange)
  {
    verseNumber ++;
    switch (verseNumber)
    {
      case 1:
       Serial.println(verse[verseNumber]);
       markTime = millis();
       break;
      case 2:
       Serial.println(verse[verseNumber]);
       markTime = millis();
       break;
      case 3:
       Serial.println(verse[verseNumber]);
       markTime = millis();
       break;
      case 4:
       Serial.println(verse[verseNumber]);
       markTime = millis();
       break;    
      default:
       verseNumber = 0; 
       Serial.println(verse[verseNumber]);
       markTime = millis();
       break;
    }
  }
}

@anon85221860 I made the changes as you have suggested, makes sense to have the switch case inside the if((currentTime - markTime) statement, but it still didn't work. It was getting into case 1: but wasn't breaking out of case 1 to start the time delay from the if statement again. I put a bunch of Serial.print statements in there to monitor.

I did find how to make it work but doesn't seem right to me. First at the beginning of the loop I had to add tft.begin(), I have that statement in the setup and didn't think I had to do it again in loop. When I do that it does step through the cases but the screen blinks from a white screen back to the verse for the 2 second delay. I then removed the tft.begin statement from the beginning of loop and added it to each case. Now it flashes a white screen when it switches to the next case

I went back to the other sketch I used this TFT display and I only have the tft.begin() statement in setup. The only difference between these two projects is on the older one I had the display wired with the 8 bit data lines and this one is using the SPI lines, I wouldn't think that should make a difference but maybe it does.

Here is my revised sketch

// mp3 player with TFT display
// Rev 1, display lyrics

#include <SPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_HX8357.h"

// These are 'flexible' lines that can be changed
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8 // RST can be set to -1 if you tie it to Arduino's reset

// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC, TFT_RST);

char *verse[] = {"zero", "one", "two", "three", "four"};
byte verseNumber;
const unsigned long verseChange = 2000;
unsigned long markTime = millis();
unsigned long currentTime;
long int test = 0;



void setup() {
#include <SPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_HX8357.h"

Serial.begin(9600);
 
// These are 'flexible' lines that can be changed
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8 // RST can be set to -1 if you tie it to Arduino's reset

// Color definitions
#define BLACK    0x0000
#define BLUE     0x001F
#define RED      0xF800
#define GREEN    0x07E0
#define CYAN     0x07FF
#define MAGENTA  0xF81F
#define YELLOW   0xFFE0 
#define WHITE    0xFFFF


// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC, TFT_RST);

  tft.begin();
  tft.fillScreen(BLACK); //clears screen, sets to Black
  tft.setRotation(3);  // rotates screen 90' for landscape mode

  } // end setup

void loop() {
  // cycle through verses
  currentTime = millis();
  Serial.print("start of loop");
  Serial.print("\t");
  Serial.println(verse[verseNumber]);

//tft.begin();
  
// Verse delays
if ((currentTime - markTime) > verseChange)
{
  verseNumber ++;
  Serial.print("in if");
  Serial.print("\t");
  Serial.println(verseNumber);

 
  switch (verseNumber)
  {
    case 1:
      Serial.print("case 1");
      Serial.print("\t");
      Serial.println(verse[verseNumber]);
      tft.begin();
      tft.setRotation(3);  // rotates screen 90' for landscape mode
      tft.fillScreen(BLACK); //clears screen, sets to Black
      tft.setCursor(10, 70);
      tft.setTextColor(WHITE);
      tft.setTextSize(2);
      tft.print("Opening Screen"); 
      markTime = millis();
      
     break;
  
    case 2:
      Serial.print("case 2");
      Serial.print("\t");
      Serial.println(verse[verseNumber]);
      tft.begin();
      tft.setRotation(3);  // rotates screen 90' for landscape mode
      tft.fillScreen(BLACK); //clears screen, sets to Black
      
        // Print verse 1
      tft.setCursor(10, 70);
      tft.setTextColor(WHITE);
      tft.setTextSize(2);
      tft.print("Every day is so wonderful"); 
    
      tft.setCursor(10, 100);
      tft.setTextColor(WHITE);
      tft.setTextSize(2);
      tft.print("Then suddenly it's hard to breathe");
    
      tft.setCursor(10, 130);
      tft.setTextColor(WHITE);
      tft.setTextSize(2);
      tft.print("Now and then I get insecure");
    
      tft.setCursor(10, 160);
      tft.setTextColor(WHITE);
      tft.setTextSize(2);
      tft.print("From all the pain");
    
      tft.setCursor(10, 190);
      tft.setTextColor(WHITE);
      tft.setTextSize(2);
      tft.print("I'm so ashamed");
      markTime = millis();
      
      break;
  
   case 3:
      Serial.print("case 3");
      Serial.print("\t");
      Serial.println(verse[verseNumber]);
      tft.begin();
      tft.setRotation(3);  // rotates screen 90' for landscape mode
      tft.fillScreen(BLACK); //clears screen, sets to Black
       
        // Print verse 2
      tft.setCursor(10, 70);
      tft.setTextColor(WHITE);
      tft.setTextSize(2);
      tft.print("I am beautiful no matter what they say"); 
    
      tft.setCursor(10, 100);
      tft.setTextColor(WHITE);
      tft.setTextSize(2);
      tft.print("Words can't bring me down");
    
      tft.setCursor(10, 130);
      tft.setTextColor(WHITE);
      tft.setTextSize(2);
      tft.print("I am beautiful in every single way");
    
      tft.setCursor(10, 160);
      tft.setTextColor(WHITE);
      tft.setTextSize(2);
      tft.print("Yes, words can't bring me down, oh no");
    
      tft.setCursor(10, 190);
      tft.setTextColor(WHITE);
      tft.setTextSize(2);
      tft.print("So don't you bring me down today");
      markTime = millis();
    
      break;
    
   default:
      Serial.print("default");
      Serial.print("\t");
      Serial.println(verse[verseNumber]);
      tft.begin();
      tft.setRotation(3);  // rotates screen 90' for landscape mode
      tft.fillScreen(BLACK); //clears screen, sets to Black
        
      tft.setCursor(10, 70);
      tft.setTextColor(WHITE);
      tft.setTextSize(2);
      tft.print("Ending Screen"); 
      markTime = millis();
    } // end switch
  } // end ((currentTime - markTime))
} // end loop

Thanks so much for your help
John

You have all of your declarations repeated in setup().
Change up your cases to minimize text and take advantage of the
*char verse array (incorporate the other stuff once you have nailed down the process).

I don't have that display. I think it should be able to manage 'tft.print(verse[verseNumber]);'
Spend some time familiarizing yourself with its operation (outside your project).
It should suffice to have
tft.begin(); and
tft.setRotation(3);
in setup() (once).
And likewise
tft.setTextColor(WHITE); and
tft.setTextSize(2);
Once established they shouldn't have to be repeated.

Noticed an oversight in my sketch

char *verse[] = {"zero", "one", "two", "three", "four"};
byte verseNumber;
const unsigned long verseChange = 2000;
unsigned long markTime;
unsigned long currentTime;

bool Init;

void setup() 
{
  Serial.begin(19200);
  verseNumber = 0;
  //Serial.println(verse[verseNumber]);
  markTime = millis();
  Init = true;
}

void loop() 
{
  currentTime = millis();
  
  if(((currentTime - markTime) > verseChange)||(Init == true))
  {
    switch (verseNumber)
    {
      case 1:
       Serial.println(verse[verseNumber]);
       break;
      case 2:
       Serial.println(verse[verseNumber]);
       break;
      case 3:
       Serial.println(verse[verseNumber]);
       break;
      case 4:
       Serial.println(verse[verseNumber]);
       break;    
      default:
       Init = false;
       Serial.println(verse[verseNumber]);
       break;
    }
    verseNumber ++;
    if (verseNumber > 4)
    {
      verseNumber = 0;
    }
    markTime = millis();
  }
}

Thanks very much, I'll look into all of that

Much appreciated

This still has the repetitions of the things I mentioned above.

#include <SPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_HX8357.h"

// These are 'flexible' lines that can be changed
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8 // RST can be set to -1 if you tie it to Arduino's reset

// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC, TFT_RST);

char *verse[] = {"zero", "one", "two", "three", "four"};
byte verseNumber;
const unsigned long verseChange = 2000;
unsigned long markTime = millis();
unsigned long currentTime;
//long int test = 0;

// Color definitions
#define BLACK    0x0000
#define BLUE     0x001F
#define RED      0xF800
#define GREEN    0x07E0
#define CYAN     0x07FF
#define MAGENTA  0xF81F
#define YELLOW   0xFFE0 
#define WHITE    0xFFFF

bool Init;

void setup() 
{
  Serial.begin(9600);
  tft.begin();
  tft.fillScreen(BLACK); //clears screen, sets to Black
  tft.setRotation(3);  // rotates screen 90' for landscape mode

  verseNumber = 0;
  markTime = millis();
  Init = true;
} // end setup

void loop() 
{
  currentTime = millis();
  
  if(((currentTime - markTime) > verseChange)||(Init == true))
  {
    switch (verseNumber)
    {
      case 1:
       Serial.println(verse[verseNumber]);
       tft.fillScreen(BLACK); //clears screen, sets to Black
       tft.setCursor(10, 70);
       tft.setTextColor(WHITE);
       tft.setTextSize(2);
       tft.print(verse[verseNumber]); 
       break;
      case 2:
       Serial.println(verse[verseNumber]);
       tft.fillScreen(BLACK); //clears screen, sets to Black
       tft.setCursor(10, 70);
       tft.setTextColor(WHITE);
       tft.setTextSize(2);
       tft.print(verse[verseNumber]); 
       break;
      case 3:
       Serial.println(verse[verseNumber]);
       tft.fillScreen(BLACK); //clears screen, sets to Black
       tft.setCursor(10, 70);
       tft.setTextColor(WHITE);
       tft.setTextSize(2);
       tft.print(verse[verseNumber]);        
       break;
      case 4:
       Serial.println(verse[verseNumber]);
       tft.fillScreen(BLACK); //clears screen, sets to Black
       tft.setCursor(10, 70);
       tft.setTextColor(WHITE);
       tft.setTextSize(2);
       tft.print(verse[verseNumber]);        
       break;    
      default:
       Init = false;
       Serial.println(verse[verseNumber]);
       tft.fillScreen(BLACK); //clears screen, sets to Black
       tft.setCursor(10, 70);
       tft.setTextColor(WHITE);
       tft.setTextSize(2);
       tft.print(verse[verseNumber]); 
       break;
    }
    verseNumber ++;
    if (verseNumber > 4)
    {
      verseNumber = 0;
    }
    markTime = millis();
  }
}

Here I have that stuff in setup()
(I don't have this display to experience with.)

#include <SPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_HX8357.h"

// These are 'flexible' lines that can be changed
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8 // RST can be set to -1 if you tie it to Arduino's reset

// Use hardware SPI (on Uno, #13, #12, #11) and the above for CS/DC
Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC, TFT_RST);

char *verse[] = {"zero", "one", "two", "three", "four"};
byte verseNumber;
const unsigned long verseChange = 2000;
unsigned long markTime = millis();
unsigned long currentTime;
//long int test = 0;

// Color definitions
#define BLACK    0x0000
#define BLUE     0x001F
#define RED      0xF800
#define GREEN    0x07E0
#define CYAN     0x07FF
#define MAGENTA  0xF81F
#define YELLOW   0xFFE0 
#define WHITE    0xFFFF

bool Init;

void setup() 
{
  Serial.begin(9600);
  tft.begin();
  tft.fillScreen(BLACK); //clears screen, sets to Black
  tft.setRotation(3);  // rotates screen 90' for landscape mode
       tft.setTextColor(WHITE);
       tft.setTextSize(2);
  verseNumber = 0;
  markTime = millis();
  Init = true;
} // end setup

void loop() 
{
  currentTime = millis();
  
  if(((currentTime - markTime) > verseChange)||(Init == true))
  {
    switch (verseNumber)
    {
      case 1:
       Serial.println(verse[verseNumber]);
       tft.fillScreen(BLACK); //clears screen, sets to Black
       tft.setCursor(10, 70);
       tft.print(verse[verseNumber]); 
       break;
      case 2:
       Serial.println(verse[verseNumber]);
       tft.fillScreen(BLACK); //clears screen, sets to Black
       tft.setCursor(10, 70);
       tft.print(verse[verseNumber]); 
       break;
      case 3:
       Serial.println(verse[verseNumber]);
       tft.fillScreen(BLACK); //clears screen, sets to Black
       tft.setCursor(10, 70);
       tft.print(verse[verseNumber]);        
       break;
      case 4:
       Serial.println(verse[verseNumber]);
       tft.fillScreen(BLACK); //clears screen, sets to Black
       tft.setCursor(10, 70);
       tft.print(verse[verseNumber]);        
       break;    
      default:
       Init = false;
       Serial.println(verse[verseNumber]);
       tft.fillScreen(BLACK); //clears screen, sets to Black
       tft.setCursor(10, 70);
       tft.print(verse[verseNumber]); 
       break;
    }
    verseNumber ++;
    if (verseNumber > 4)
    {
      verseNumber = 0;
    }
    markTime = millis();
  }
}

I have made some headway, I was playing around with switching from SPI to 8 bit and swapping the Nano with an UNO while you had sent me this updates, I have not revised my sketch with these changes but I will, I like how you have it. It's good to see how others do it and your's is more efficient.

Here's what I found. I am back to the Nano but I have changed the wiring to the 8 bit data lines and it works fine, no flashing screen and I only have tft.begin() in the setup, it setups through the verses fine. So either there's a hardware problem with the display in the SPI configuration or because they use two different libraries. In the SPI mode it uses "Adafruit_HX8357.h" in the 8 bit data mode it uses "Adafruit_TFTLCD.h". These are the libraries that Adafruit uses in their tutorial and example sketches.

I would prefer to use the SPI mode as it requires less pins and I still have the DFPlayer mp3 module to connect.

I'll make the changes you have sent me and try going back to SPI to see what happens.

Thanks again
John

The DFPlayer gets run with SoftwareSerial.
The "analog pins" (A0-A5) can be used as 'digital', too (or be assigned in SoftwareSerial).

I haven't use a DFPlayer yet, the ones I order will be here Wednesday. I did see that I have to use SoftwareSerial, I have used that before. So the only other pin I'll need is for a pushbutton to either restart it or more than likely I'll have it battery powered and will put the Nano to sleep and wire the button to an interrupt to wake it up.

Thanks so much for all your help and sticking with me
John

I think that's not going to put the display to sleep.

That's true, I did think about that. I don't think I could power the display from an output pin so I'll add a small relay to shut the power off on the display before the nano goes to sleep. that's the plan any ways.

On second thought, some sort of electronic switch, transistor or something would be better. I'll figure that out later