Millis() Questions and printing values to serial readout

Hello I been working on a project that involves a TFT, motors, and Millis. I'm having some confusing outcomes when I print information to serial. I'm currently controlling the buttons of the program to go on for 60 seconds and then off. This is going to control motor control and direction. If you look at my code you will see that the first conditional for CW start button only seems to work when I set cwstateMillis to startMillis in the else clause. I would of thought like most millis examples I would of set it inside the first if statement. So how in the world is the first If statement looping and getting the value of cwstatemills from the else statement. I thought that when if statement is true it only runs the block of code inside the brackets. How is it that I can print the updated value of cwstateMillis value from else statement inside the first if statement.

Code is in next post.

how in the world is the first If statement looping

No code yet, but if statements don't loop

// of 800x480 pixels.
//
// This program requires the UTFT library.
//

#include <UTFT.h>
#include <URTouch.h>
#include <AccelStepper.h>

// Declare which fonts we will be using

extern uint8_t SmallFont[];
extern uint8_t BigFont[];
extern uint8_t SevenSegNumFont[];
extern uint8_t GroteskBold24x48[];

// ------------------------------------------------------------
// ElecHouse TFT LCD/SD Shield for Arduino Due : <display model>,22,23,31,33
// ------------------------------------------------------------


//Creating Objects

UTFT myGLCD(CTE70, 22, 23, 31, 33); // Model and pins on Due
URTouch myTouch (25, 26, 27, 29, 30); // Pins for Touchscreen

//Defining Variables

unsigned long testLength = 60000;
unsigned long startMillis = 0;
unsigned long cwstateMillis = 0;
unsigned long ccwstateMillis = 0;


int resetState = 0;
int forwardState = 0;
int reverseState = 0;
int motorState = 0;
int x, y;
int buttonState = 0;
int minTime = 0;
int timeDif = 0; // not used yet





void setup() {

 // Setup the LCD
 myGLCD.InitLCD();
 myGLCD.clrScr();
 myTouch.InitTouch();
 myTouch.setPrecision(PREC_MEDIUM);

 drawHomeScreen(); //Draws the Home Screen

 Serial.begin(9600);
}
void loop() {

 unsigned long startMillis = millis();


 cwStartButton(40, 100, 325, 200); //x1 = 40, x2 = 325, y1 = 100, y2 = 200
 ccwStartButton(40, 220, 325, 320);//x1 = 40, x2 = 325, y1 = 220, y2 = 320
 resetButton(40, 340, 325, 440);   //x1 = 40, x2 = 325, y1 = 340, y2 = 440



 if (buttonState == 1 && startMillis - cwstateMillis <= testLength)   {


   reverseState = 0;
   forwardState = 1;
   motorState = 1;


   myGLCD.setFont(SevenSegNumFont);
   myGLCD.setColor(0, 255, 0);
   myGLCD.setBackColor(0, 0, 0);
   myGLCD.printNumI(minTime, 250, 250, 2, '0');

   //Serial.println("buttonState");
   //Serial.println(buttonState);
   //Serial.println("forwardState");
   //Serial.println(forwardState);
   //Serial.println("reversestate");
   //Serial.println(reverseState);
   //Serial.println("motorState");
   //Serial.println(motorState);
   Serial.println("startMillis");
   Serial.println(startMillis);
   Serial.println("cwstateMillis");
   Serial.println(cwstateMillis);
   Serial.println("minTime");
   Serial.println(minTime);


 }
 else if (buttonState == 2 && startMillis - ccwstateMillis <= testLength) {

   forwardState = 0;
   reverseState = 1;
   motorState = 1;

   //myGLCD.print(startMillis,200,200);

   //Serial.println("forwardState");
   //Serial.println(forwardState);
   //Serial.println("reversestate");
   //Serial.println(reverseState);
   //Serial.println("motorState");
   //Serial.println(motorState);
   //Serial.println("startMillis");
   //Serial.println(startMillis);
   //Serial.println("cwstateMillis");
   //Serial.println(ccwstateMillis);
 }
else {

   cwstateMillis = startMillis;
   ccwstateMillis = startMillis;
   buttonState = 0;
   reverseState = 0;
   forwardState = 0;
   motorState = 0;
   drawHomeScreen();

   //Serial.println("forwardState");
   //Serial.println(forwardState);
   //Serial.println("reversestate");
   //Serial.println(reverseState);
   //Serial.println("motorState");
   //Serial.println(motorState);
   Serial.println("startMillis");
   Serial.println(startMillis);
   Serial.println("cwstateMillis");
   Serial.println(cwstateMillis);
   Serial.println("minTime");
   Serial.println(minTime);

 }
}

Did you forget to use code tags and also forget to post your whole program ?

What is "cwStartButton" and "ccwStartButton" and "resetButton" and how is "buttonState" initialized? And what do you mean by "the button are on for 60 seconds and then off"?

Sorry guys I posted it, and then realized there is a 5 minute waiting time on posting again.

Rest of the code is pretty much TFT functions.

//======= Custom Functions ===========
// drawHomeScreen - Custom Function
void drawHomeScreen() {
  //Title Screen
  myGLCD.setBackColor(0, 0, 0); // Sets the background color of area where text will be printed to black
  myGLCD.setColor(255, 255, 255); // Sets Text color to White
  myGLCD.setFont(GroteskBold24x48); //Sets Font to big
  myGLCD.print("LEAD TEST", CENTER, 10);

  //Yellow Line
  myGLCD.setColor(240, 184, 35);
  myGLCD.drawLine(25, 65, 775, 65);
  myGLCD.drawLine(25, 66, 775, 66);
  myGLCD.drawLine(25, 67, 775, 67);
  myGLCD.drawLine(25, 68, 775, 68);

  //CW Start Button

  myGLCD.setColor(240, 184, 35);     // Sets rectangle fill color to Yellow
  myGLCD.fillRect(40, 100, 325, 200); // Draws filled rectangle
  myGLCD.setColor(255, 255, 255);    // Sets Color of outer rectangle to white
  myGLCD.drawRect(40, 100, 325, 200); // Draws the rectangle to give button outer border
  myGLCD.setFont(GroteskBold24x48);  // Sets font to Grotesk Size
  myGLCD.setColor(0, 0, 0);          // Sets Font Color to black
  myGLCD.setBackColor(240, 184, 35); // Sets font background color to Yellow
  myGLCD.print("CW START", 87, 127); // Prints button label

  //CCW Start Button

  myGLCD.setColor(240, 184, 35);     // Sets rectangle fill color to Yellow
  myGLCD.fillRect(40, 220, 325, 320); // Draws filled rectangle
  myGLCD.setColor(255, 255, 255);    // Sets Color of outer rectangle to white
  myGLCD.drawRect(40, 220, 325, 320); // Draws rectangle to give button outer border
  myGLCD.setFont(GroteskBold24x48);  // Sets font to Grotesk Size
  myGLCD.setColor(0, 0, 0);          // Sets Font Color to black
  myGLCD.setBackColor(240, 184, 35); // Sets font background color to Yellow
  myGLCD.print("CCW START", 78, 246); // Prints button label

  //Rest Button

  myGLCD.setColor(240, 184, 35);     // Sets rectangle fill color to Yellow
  myGLCD.fillRect(40, 340, 325, 440); // Draws filled rectangle
  myGLCD.setColor(255, 255, 255);    // Sets color of outer rectangle to white
  myGLCD.drawRect(40, 340, 325, 440); // Draws rectangle to give button outer border
  myGLCD.setFont(GroteskBold24x48);  // Sets font to Grotesk Size
  myGLCD.setColor(0, 0, 0);          // Sets Font Color to black
  myGLCD.setBackColor(240, 184, 35); // Sets font background color to Yellow
  myGLCD.print("RESET", 122, 366);   // Prints button label
}

// Highlights buttons when pressed
void cwStartButton(int x1, int y1, int x2, int y2) {

  if (myTouch.dataAvailable()) { //Checks to see if new data from touchscreen is waiting
    myTouch.read(); //Reads data from touchscreen if dataAvailable = true
    x = myTouch.getX(); // x values of screen input
    y = myTouch.getY(); // y values of screen input

    if ((x >= 40) && (x <= 325) && (y >= 100) && (y <= 205)) {

      myGLCD.setColor(126, 126, 126);
      myGLCD.drawRect (x1, y1, x2, y2);
      myGLCD.setColor(0, 150, 0);        // Sets rectangle fill color to Yellow
      myGLCD.fillRect(x1, y1, x2, y2);   // Draws filled rectangle
      myGLCD.setColor(255, 255, 255);    // Sets Color of outer rectangle to white
      myGLCD.drawRect(x1, y1, x2, y2);   // Draws the rectangle to give button outer border
      myGLCD.setFont(GroteskBold24x48);  // Sets font to Grotesk Size
      myGLCD.setColor(255, 255, 255);          // Sets Font Color to black
      myGLCD.setBackColor(0, 150, 0); // Sets font background color to Yellow
      myGLCD.print("CW START", 87, 127); // Prints button label

      buttonState = 1;
    }
  }
}

void ccwStartButton(int x1, int y1, int x2, int y2) {

  if (myTouch.dataAvailable()) { //Checks to see if new data from touchscreen is waiting
    myTouch.read(); //Reads data from touchscreen if dataAvailable = true
    x = myTouch.getX(); // x values of screen input
    y = myTouch.getY(); // y values of screen input

    if ((x >= 40) && (x <= 325) && (y >= 220) && (y <= 320)) {

      myGLCD.setColor(126, 126, 126);
      myGLCD.drawRect (x1, y1, x2, y2);
      myGLCD.setColor(0, 150, 0);        // Sets rectangle fill color to Yellow
      myGLCD.fillRect(x1, y1, x2, y2);   // Draws filled rectangle
      myGLCD.setColor(255, 255, 255);    // Sets Color of outer rectangle to white
      myGLCD.drawRect(x1, y1, x2, y2);   // Draws the rectangle to give button outer border
      myGLCD.setFont(GroteskBold24x48);  // Sets font to Grotesk Size
      myGLCD.setColor(255, 255, 255);          // Sets Font Color to black
      myGLCD.setBackColor(0, 150, 0); // Sets font background color to Yellow
      myGLCD.print("CCW START", 78, 246); // Prints button label

      buttonState = 2;

    }
  }
}

void resetButton(int x1, int y1, int x2, int y2) {

  if (myTouch.dataAvailable()) { //Checks to see if new data from touchscreen is waiting
    myTouch.read(); //Reads data from touchscreen if dataAvailable = true
    x = myTouch.getX(); // x values of screen input
    y = myTouch.getY(); // y values of screen input

    if ((x >= 40) && (x <= 325) && (y >= 340) && (y <= 440)) {

      myGLCD.setColor(126, 126, 126);
      myGLCD.drawRect (x1, y1, x2, y2);
      myGLCD.setColor(0, 150, 0);        // Sets rectangle fill color to Yellow
      myGLCD.fillRect(x1, y1, x2, y2);   // Draws filled rectangle
      myGLCD.setColor(255, 255, 255);    // Sets Color of outer rectangle to white
      myGLCD.drawRect(x1, y1, x2, y2);   // Draws the rectangle to give button outer border
      myGLCD.setFont(GroteskBold24x48);  // Sets font to Grotesk Size
      myGLCD.setColor(255, 255, 255);          // Sets Font Color to black
      myGLCD.setBackColor(0, 150, 0); // Sets font background color to Yellow
      myGLCD.print("RESET", 122, 366);   // Prints button label

      buttonState = 0;
      cwstateMillis = 0;
      ccwstateMillis = 0;
      drawHomeScreen();


    }
  }
}

Rest of the code is pretty much TFT functions.

So where are the global variable declarations ?

The variables cwstateMillis and ccwstateMillis should be set when the buttonState changes:

//code left out

buttonState = 1;
cwstateMillis = millis(); //Must be added

//code left out

buttonState = 2;
ccwstateMillis = millis(); //Must be added

//code left out

buttonState = 0;
cwstateMillis = 0; //useless assignment?
ccwstateMillis = 0; //useless assignment?

And the 3 first lines of your "else" statement seems to reset all values, making the timing impossible:

//The following two lines will ensure that "startMillis - c*wstateMillis <= testLength" is always a valid condition:
cwstateMillis = startMillis;
ccwstateMillis = startMillis;
buttonState = 0; //useless assignment?

EDIT: You may have inverted a conditional test; Should "startMillis - cwstateMillis <= testLength" not be "startMillis - cwstateMillis >= testLength" to ensure that the time the button is pressed exceeds testLength?

The code works believe it or not, but I'm trying to learn understand why in the world does it work.

The first if statement prints the same as the else statement, that's why?

The button states are put to 1 in the function on button press.

// Highlights buttons when pressed
void cwStartButton(int x1, int y1, int x2, int y2) {

  if (myTouch.dataAvailable()) { //Checks to see if new data from touchscreen is waiting
    myTouch.read(); //Reads data from touchscreen if dataAvailable = true
    x = myTouch.getX(); // x values of screen input
    y = myTouch.getY(); // y values of screen input

    if ((x >= 40) && (x <= 325) && (y >= 100) && (y <= 205)) {

      myGLCD.setColor(126, 126, 126);
      myGLCD.drawRect (x1, y1, x2, y2);
      myGLCD.setColor(0, 150, 0);        // Sets rectangle fill color to Yellow
      myGLCD.fillRect(x1, y1, x2, y2);   // Draws filled rectangle
      myGLCD.setColor(255, 255, 255);    // Sets Color of outer rectangle to white
      myGLCD.drawRect(x1, y1, x2, y2);   // Draws the rectangle to give button outer border
      myGLCD.setFont(GroteskBold24x48);  // Sets font to Grotesk Size
      myGLCD.setColor(255, 255, 255);          // Sets Font Color to black
      myGLCD.setBackColor(0, 150, 0); // Sets font background color to Yellow
      myGLCD.print("CW START", 87, 127); // Prints button label

      buttonState = 1;
      
    }
  }
}

Everything seems to be working correctly and how I intended, but the only small problem I see is the fact that I'm running into an issues with the text flashing. It only seems to flash on the black buttons and not on the lcd heading font. Does anybody know a way I can eliminate this. I can get rid of the flashing if I remove the drawHomeScreen(); from the else statement. If I remove the drawHomeScreen from the else statement the buttons don't go back to original state.

// drawHomeScreen - Custom Function
void drawHomeScreen() {
  //Title Screen
  myGLCD.setBackColor(0, 0, 0); // Sets the background color of area where text will be printed to black
  myGLCD.setColor(255, 255, 255); // Sets Text color to White
  myGLCD.setFont(GroteskBold24x48); //Sets Font to big
  myGLCD.print("LEAD TEST", CENTER, 10);

  //Yellow Line
  myGLCD.setColor(240, 184, 35);
  myGLCD.drawLine(25, 65, 775, 65);
  myGLCD.drawLine(25, 66, 775, 66);
  myGLCD.drawLine(25, 67, 775, 67);
  myGLCD.drawLine(25, 68, 775, 68);

  //CW Start Button

  myGLCD.setColor(240, 184, 35);     // Sets rectangle fill color to Yellow
  myGLCD.fillRect(40, 100, 325, 200); // Draws filled rectangle
  myGLCD.setColor(255, 255, 255);    // Sets Color of outer rectangle to white
  myGLCD.drawRect(40, 100, 325, 200); // Draws the rectangle to give button outer border
  myGLCD.setFont(GroteskBold24x48);  // Sets font to Grotesk Size
  myGLCD.setColor(0, 0, 0);          // Sets Font Color to black
  myGLCD.setBackColor(240, 184, 35); // Sets font background color to Yellow
  myGLCD.print("CW START", 87, 127); // Prints button label

  //CCW Start Button

  myGLCD.setColor(240, 184, 35);     // Sets rectangle fill color to Yellow
  myGLCD.fillRect(40, 220, 325, 320); // Draws filled rectangle
  myGLCD.setColor(255, 255, 255);    // Sets Color of outer rectangle to white
  myGLCD.drawRect(40, 220, 325, 320); // Draws rectangle to give button outer border
  myGLCD.setFont(GroteskBold24x48);  // Sets font to Grotesk Size
  myGLCD.setColor(0, 0, 0);          // Sets Font Color to black
  myGLCD.setBackColor(240, 184, 35); // Sets font background color to Yellow
  myGLCD.print("CCW START", 78, 246); // Prints button label

  //Rest Button

  myGLCD.setColor(240, 184, 35);     // Sets rectangle fill color to Yellow
  myGLCD.fillRect(40, 340, 325, 440); // Draws filled rectangle
  myGLCD.setColor(255, 255, 255);    // Sets color of outer rectangle to white
  myGLCD.drawRect(40, 340, 325, 440); // Draws rectangle to give button outer border
  myGLCD.setFont(GroteskBold24x48);  // Sets font to Grotesk Size
  myGLCD.setColor(0, 0, 0);          // Sets Font Color to black
  myGLCD.setBackColor(240, 184, 35); // Sets font background color to Yellow
  myGLCD.print("RESET", 122, 366);   // Prints button label
}

How much of the above screen painting needs to be done when the else clause calls the function ? Cut out what is needed and put it into a function that is called in the else clause and also from the drawHomeScreen() function so that it fulfils its original purpose,