Millis timer has a mind of its own

I have very limited knowledge on C++ and have a project i started 2 years ago and just getting back to.
I have spent 4 days solid on trying yo use Millis function to create a timer, but I find its not running at the 'event time' i have set,
Using Serial.Print funtion it shows me it is randomly progressing through to the next IF statement, as if the timer is stuck in its own loop without resetting.

As I'm a new user I cannot upload my code which is frustrating.
How long before i can show you my code please so as to get an idea of where i've gone wrong?

In the absence of code, I'm going to guess "inappropriate data type" or "scope issue"

I don't know how long you have to wait - I'm hoping someone will come along soon who can tell us.

The only thing "wrong" with millis() that I'm aware of is a 1ms jitter. Other than that it works as advertised.

I'm sure it'll become clear when you can post your code. Remember to use the </> tags or you'll get told off!

I'll take a wild stab in the dark and guess that you're not updating the captured time to be the current time, inside the scope after the checks for the expired timer.

But we'll need to wait for the code.

Hello tapaz
Did you run the BLINKWITHOUTDELAY example of the IDE to learn how the millis() function operates?
Have a nice day and enjoy coding in C++.

Yes this is how the code is written, but as the code is pending a sequence of input date which sets the IF statements, via serial communications from another Arduino, i'm way out of my depth to see what may well be very obvious to the experienced.

So, just post the code.
In code tags.

Hello tapaz
Post your sketch, well formated, with comments and in so called code tags "</>" and schematic to see how we can help.
Currently my crystal ball is in the dish washer.
Have a nice day and enjoy coding in C++.

Sorry, looking on the site to see what you mean

Most of us are not eager on attachments. A new user should be able to do the following:

  1. Not necessary but use tools -> autoformat in the IDE to get the code properly indented; it might already help to show errors if you know what you have to look for.
  2. In the IDE, use edit -> copy for forum
  3. Past in a reply here on the forum
  4. Save / post the reply.

Maybe you're frustrated because you did not read or just glanced over How to get the best out of this forum which explains, amongst other things, how to post code.

//Touch sreen attempt for door control 8th Feb 2022
#if 1

#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(52,53); // TX, RX

#define MINPRESSURE 200
#define MAXPRESSURE 1000



// ALL Touch panels and wiring is DIFFERENT
// copy-paste results from TouchScreen_Calibr_native.ino
const int XP=8,XM=A2,YP=A3,YM=9; //240x320 ID=0x8230
const int TS_LEFT=920,TS_RT=94,TS_TOP=69,TS_BOT=896;


TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

Adafruit_GFX_Button Aon_btn, Aoff_btn; //Aon = TX A, Aoff = STOP A
Adafruit_GFX_Button Bon_btn, Boff_btn; //Bon = TX B, Boff = STOP B
Adafruit_GFX_Button Con_btn, Coff_btn;


                //Global variables

int Reset = 47;                       // Reset function to reset TFT display
const int wakePB = 48;                // This is wakeup Push Button at TFT LCD display
int wake = 49;                        // This picks up relay to power on TFT LCD display
int timeup1 = 0;                      //Variable for off delay picked up
int doorStateOpen;                    //Variable for current door position - reading
int doorStateClosed;                    //Variable for current door position - reading
int lastdoorStateOpen = HIGH;         //Variable for last know button reading
int doorStateA;                       //Variable to hold doorA position from serial read
int doorStateB;                       //Variable to hold doorB position from serial read
int timerStart = LOW;

int buttonStateZ;                     //Variable for current 'RESET' button reading
int lastButtonStateZ;                 //Variable for last know button reading
unsigned long lastDebounceTimeZ = 0;  //last time the pin was toggled, used to keep track of time
unsigned long debounceDelayZ = 50;    //the debounce time which user sets prior to run
int ledStateA = LOW;                  //Variable for current LED state
int wakeStateY= HIGH;                 //Variable for current RELAY state
static unsigned long wakeCameON = 0;      // Unused variable
int doorWarning = LOW;                    // Unused variable

unsigned long lastA = millis();       //set timer
unsigned long lastB = millis();       //set timer
unsigned long lastC = millis();       //set timer
unsigned long lastD = millis();       //set timer
unsigned long lastE = millis();       //set timer
unsigned long doorOpen;        //Variable for door timer
unsigned long doorClose;       //Variable for door timer
unsigned long dooropenTime;           // Variable for millis door timer
unsigned long dooreventTime = 15000; //Variable for door timer 15sec
unsigned long previousdoorTime = 0; //Variable for door timer
int timerLatch = 0;

void(* resetFunc) (void) = 0; // create a standard reset function 


//Buzzer variables....The following sketch beeps every second for 200ms, 5 times, and then goes silent. The sketch repeats every 1 minutes.

// Timer control
unsigned long currentTime;
// timer 1 variables
unsigned long previousTime1;
unsigned long delayTime1 = 1000;    // 1 second
// timer 2 variables
unsigned long previousTime2;
unsigned long delayTime2 = 60000;   // 60 seconds

int inDex = 0;

int buzzer = 50;                    // This is the digital IO pin that controls the buzzer
int buzVal;                         //value of buzzer output
int frequency = 3000;
int duration = 200;

// End of buzzer timer functions-variables





int pixel_x, pixel_y;     //Touch_getXY() updates global vars
bool Touch_getXY(void)
{
    TSPoint p = ts.getPoint();
    pinMode(YP, OUTPUT);      //restore shared pins
    pinMode(XM, OUTPUT);
    digitalWrite(YP, HIGH);   //because TFT control pins
    digitalWrite(XM, HIGH);
    bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
    if (pressed) 
  {
        pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width()); //.kbv makes sense to me
        pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
  }
    return pressed;
}

#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
#define GREY    0x8410

void setup(void)
{

  pinMode(buzzer, OUTPUT);         // set pin mode as “output” for Door/s open warning
  pinMode(wake, OUTPUT);          // set pin mode as “output” to turn on relay for TFT Display
  pinMode(wakePB, INPUT);         // set pin mode as "input" for wake up Push Button
  pinMode(Reset,OUTPUT);          //Reset for TFT display to "RESET"
  previousTime1 = millis();
  previousTime2 = millis();
  previousdoorTime = millis();
  
  
    mySerial.begin(9600);
    Serial.begin(9600);
    uint16_t ID = tft.readID();
    Serial.print("TFT ID = 0x");
    Serial.println(ID, HEX);
    Serial.println("Calibrate for your Touch Panel");
    if (ID == 0xD3D3) ID = 0x9486; // write-only shield
    tft.begin(ID);
    tft.setRotation(0);            //PORTRAIT
    tft.fillScreen(BLACK);
    Aon_btn.initButton(&tft,  60, 180, 100, 40, WHITE, CYAN, BLACK, "TX A", 2);
    Aoff_btn.initButton(&tft, 60, 230, 100, 40, WHITE, RED, BLACK, "STOP A", 2);
    Bon_btn.initButton(&tft,  180, 180, 100, 40, WHITE, CYAN, BLACK, "TX B", 2);
    Boff_btn.initButton(&tft, 180, 230, 100, 40, WHITE, RED, BLACK, "STOP B", 2);
    Con_btn.initButton(&tft,  60, 280, 100, 40, WHITE, YELLOW, BLACK, "CONF", 2);
    Coff_btn.initButton(&tft, 180, 280, 100, 40, WHITE, GREEN, BLACK, "RESET", 2);
  
    Aon_btn.drawButton(false); // door A setup
    Aoff_btn.drawButton(false);
   /* tft.setTextColor(WHITE, RED); // garage door A message
    tft.setTextSize(12);
    tft.setCursor(15, 30);
    tft.print("A");
    tft.setTextColor(WHITE, RED); // garage door A closed message
    tft.setTextSize(2);
    tft.setCursor(15, 125);
    tft.print("closed");
    */

    Bon_btn.drawButton(false); // door B setup
    Boff_btn.drawButton(false);
   /*  tft.setTextColor(WHITE, RED); // garage door message
    tft.setTextSize(12);
    tft.setCursor(140, 30);
    tft.print("B");
    tft.setTextColor(WHITE, RED); // garage door B closed message
    tft.setTextSize(2);
    tft.setCursor(140, 125);
    tft.print("closed");
    */
    Con_btn.drawButton(false); // TX/EXE
    Coff_btn.drawButton(false);
   
}


void loop(void)
{
  

  if(mySerial.available() > 1)
  {    
    int input = mySerial.parseInt();     //read serial input and convert to integer (-32,768 to 32,767)   
    
     if(input == 0001)                   //if on code is received Door A open
    {                 
      tft.setTextColor(WHITE, GREEN);   // garage door A message
      tft.setTextSize(12);
      tft.setCursor(15, 30);
      tft.print("A");
      tft.setTextColor(WHITE, GREEN);   // garage door A open message
      tft.setTextSize(2);
      tft.setCursor(15, 125);
      tft.print(" open ");
      doorStateA = 1;                    // Set door A position OPEN
      mySerial.flush();                 //clear the serial buffer for unwanted inputs 
      /*Serial.print(" doorState A open: ");
      Serial.println(doorStateA); */
    }
     
    if(input == 0010)                   //if off code is received Door A unknown
    {                  
       tft.setTextColor(BLACK, YELLOW); // garage door A unknown state
      tft.setTextSize(12);
      tft.setCursor(15, 30);
      tft.print("!");
      tft.setTextColor(BLACK, YELLOW); // garage door A open message
      tft.setTextSize(2);
      tft.setCursor(15, 125);
      tft.print(" move ");
      doorStateA = 2;                   // Set door A position MOVE
      mySerial.flush();                 //clear the serial buffer for unwanted inputs  
      /*Serial.print(" doorState A move: ");
      Serial.println(doorStateA); */
    }
           
    if(input == 0011)                   //if on code is received Door A closed
    {                  
      tft.setTextColor(WHITE, RED);     // garage door A message
      tft.setTextSize(12);
      tft.setCursor(15, 30);
      tft.print("A");
      tft.setTextColor(WHITE, RED);     // garage door A closed message
      tft.setTextSize(2);
      tft.setCursor(15, 125);
      tft.print("closed");
      doorStateA = 3;                   // Set door A position CLOSED
      mySerial.flush();                 //clear the serial buffer for unwanted inputs 
      /*Serial.print(" doorState A closed: ");
      Serial.println(doorStateA);  */
    }
      
    if(input == 0100)                   //if off code is received
    {                  
     
      mySerial.flush();                 //clear the serial buffer for unwanted inputs  
    }
     
    
    if(input == 0101)                   //if on code is received Door B open
    {                  
      tft.setTextColor(WHITE, GREEN);   // garage door message
      tft.setTextSize(12);
      tft.setCursor(140, 30);
      tft.print("B");
      tft.setTextColor(WHITE, GREEN);    // garage Door B open message
      tft.setTextSize(2);
      tft.setCursor(140, 125);
      tft.print(" open ");
      doorStateB = 1;                   // Set door B position OPEN
      mySerial.flush();                 //clear the serial buffer for unwanted inputs
      /*Serial.print (" doorState B open: ");
      Serial.println (doorStateB);  */ 
      
    }
    if(input == 0110)                   //if off code is received Door B unknown
    {                  
      tft.setTextColor(BLACK, YELLOW);  // garage door message
      tft.setTextSize(12);
      tft.setCursor(140, 30);
      tft.print("!");
      tft.setTextColor(BLACK, YELLOW);  // garage door B move message
      tft.setTextSize(2);
      tft.setCursor(140, 125);
      tft.print(" move ");
      doorStateB = 2;                   // Set door B position MOVE
      mySerial.flush();                 //clear the serial buffer for unwanted inputs  
      /*Serial.print(" doorState B move: ");
      Serial.println(doorStateB);*/
    }
      
    if(input == 0111)                   //if on code is received Door B closed
    {                  
      tft.setTextColor(WHITE, RED);     // garage door message
      tft.setTextSize(12);
      tft.setCursor(140, 30);
      tft.print("B");
      tft.setTextColor(WHITE, RED);     // garage door B closed message
      tft.setTextSize(2);
      tft.setCursor(140, 125);
      tft.print("closed");
      doorStateB = 3;                   // Set door B position CLOSED
      mySerial.flush();                 //clear the serial buffer for unwanted inputs 
      /*Serial.print(" doorState B closed: "); 
      Serial.println(doorStateB);*/
    }

    if(input == 1000)                   //if off code is received light switch
    {                  
     
      mySerial.flush();                 //clear the serial buffer for unwanted inputs  
           
    }
         
    if(input == 1001)                   //if on code is received - light switc
    {                  
      
      mySerial.flush();                 //clear the serial buffer for unwanted inputs  
     
    }
    if(input == 1010)                   //if off code is received - light switc
    {                 
    
      mySerial.flush();                 //clear the serial buffer for unwanted inputs  
    }

      mySerial.flush();//clear the serial buffer for unwanted inputs     
      delay(10);//delay little for better serial communication
  

        
     

    tft.setTextColor(WHITE, BLACK); // garage door message
    tft.setTextSize(2);
    tft.setCursor(50, 10);
    tft.print("GARAGE DOORS");

    
    bool down = Touch_getXY();
    Aon_btn.press(down && Aon_btn.contains(pixel_x, pixel_y));
    Aoff_btn.press(down && Aoff_btn.contains(pixel_x, pixel_y));
    Bon_btn.press(down && Bon_btn.contains(pixel_x, pixel_y));
    Boff_btn.press(down && Boff_btn.contains(pixel_x, pixel_y));
    Con_btn.press(down && Con_btn.contains(pixel_x, pixel_y));
    Coff_btn.press(down && Coff_btn.contains(pixel_x, pixel_y));
    if (Aon_btn.justReleased())
        Aon_btn.drawButton();
    if (Aoff_btn.justReleased())
        Aoff_btn.drawButton();
    if (Bon_btn.justReleased())
        Bon_btn.drawButton();
    if (Boff_btn.justReleased())
        Boff_btn.drawButton();
    if (Con_btn.justReleased())
        Con_btn.drawButton();
    if (Coff_btn.justReleased())
        Coff_btn.drawButton();
     
    if (Aon_btn.justPressed()) 
    {
        Aon_btn.drawButton(true);
                                      //WILL SEND CLOSE DOOR A COMMAND
    }
    if (Bon_btn.justPressed()) 
    {
        Bon_btn.drawButton(true);
                                      // WILL SEND CLOSE DOOR B COMMAND
    }
    if (Aoff_btn.justPressed()) 
    {
        Aoff_btn.drawButton(true);
                                     // WILL SEND DOOR A STOP/ REVERSE
    }
    if (Boff_btn.justPressed()) 
    {
        Boff_btn.drawButton(true);
                                    //WILL SEND DOOR B STOP/REVERSE
    }
    if (Con_btn.justPressed()) 
    {
        Con_btn.drawButton(true);
                                    // CONFIRM WILL GO GREEN
    }
    if (Coff_btn.justPressed())               //WILL SEND 'RESET' COMMAND
    {
        Coff_btn.drawButton(true);
        mySerial.println(1110);              //send unique code to the receiver to reset timer for doors open (RESET). In this case 1110 
        doorStateOpen = LOW;                 // Reset door open count
        /*digitalWrite(buzzer, LOW);          // set buzzer OFF
        delay(1000);*/
        mySerial.flush();                   //clear the serial buffer for unwanted inputs 
    }                                        
  
  }


    // Door buzzer alarm if doors open > 30min  

    
     if (doorStateA == 3 && doorStateB == 3)                                     //if on code is received - door A and B closed
    {                                        
     doorStateClosed = LOW; 
     timerStart = LOW;                                                           //Want timed delay but no millis function is working in this programme
     doorStateOpen = LOW;
     timerLatch = 0;
     doorStateOpen = LOW;
                         
    }

      if (doorStateClosed == LOW)                                                  // If door A or door B CLOSED > 'X' seconds then turn WARNING about to sound.
      {   
        doorClose = millis();        
        Serial.println("Doors CLOSED");
          
        if(millis() - doorClose > 2000)                                           // Doors CLOSED 'X' seconds (1 Seconds) " This timer Does not work"
        {
                                                                                  // Reset timers as required  but no millis function working                      
        }
      }


      if (doorStateA == 1 || doorStateB == 1 || doorStateB == 2 || doorStateB ==2) //if on code is received - door A or B open or in abnormal position A/B are (moving)
      {   
        doorStateOpen = HIGH;
        doorStateClosed = HIGH;  
        dooropenTime = millis();                       
            }



      if (doorStateOpen == HIGH && timerLatch == 0)                                  // If door A or door B moving or open > 'X' seconds then turn WARNING about to sound.
        {             
                             
                        
          if (dooropenTime - previousdoorTime > dooreventTime)                      // Doors open/move 'X' seconds (15 Seconds)
          {
            timerStart = HIGH; 
            timerLatch = 1; 
            previousdoorTime = dooropenTime;  
            Serial.println("OPEN");                                 
            Serial.println(dooropenTime);                  
          } 
                      
        
        
          if (timerStart == HIGH)   
          {                
                  currentTime = millis();                        
                                                                // beep every second for 200ms, 5 times

                if (currentTime >= previousTime1 + delayTime1)
                {
                Serial.println("Buzz Time loop");
                inDex++;                                        // increment every 1000ms
                if (inDex < 6)
                  {                                             // 5 times
                tone(buzzer, frequency, duration);
                                 }   
                previousTime1 = currentTime;
                }
                                                                // reset inDex after 300000ms (5 minutes)
                if (currentTime >= previousTime2 + delayTime2)
                    { 
                inDex = 0;
                previousTime2 = currentTime; 
                    }
          }
        }      
     
        
}
#endif          
      
    

Top end is just touch screen function and works fine, the millis function is at the end where I'm trying to add it to use the data being received by the serial port.

Section starts
// Door buzzer alarm if doors open > 30min
Currently set at 15 seconds for testing.

Blockquote

if (dooropenTime - previousdoorTime > dooreventTime) // this is my demon

Blockquote

Why octal?

Hello
What is the design goal to have different time bases for currentTime, doorCloseTime, doorOpneTime per time slice?
Have a nice day and enjoy coding in C++.

As this is my very first project and i have limited knowledge I used code that I knew worked for the HC-12 Serial module. Its definitely a project outside my knowledge. So it will be full of land mines i'm sure

OK, one time is for doors to be open, 30min. Then the other time is to give a warning buzzer repeating at half hour intervals that the doors are open. (such that I can either reset the timer and or close the door by remote control. I have not included the rest of the code for using this as its a work in progress, that without fully understanding this Millis function and why i cant get it to run as it should, will not be accurate and confuse the issue further.

That doesnt explain why you want to use octal representation

I'm not sure that answers what I asked.

As a new user, you will have limited posts, so use them wisely.

As far as I know its Binary, and i'm currently using 4 bits, that when I have everything up and running I will try to change further to make a more secure code over the serial link. (So I wouldnt say its Octal. just 4 bit binary.

It's not, it's octal.