Waking from System.sleep(D3, FALLING, 30), PHOTON

I have a question about the behavior of when the photon wakes from stop mode.

At what point does the code start executing when woken? At the beginning of setup()? At the beginning of loop()?

In my below code, I have an interrupt triggering the wake up. Immediately after I wake up the photon it goes back in to sleep.

The only trigger to go to sleep is this code. I’m not sure if sleepBeginMillis gets reset to zero and millis() still increments during sleep?

Any help will be appreciated.

 if(millis() - sleepBeginMillis > 10000){
    RGB.control(true);
    RGB.color(0, 255, 0);
    delay(300);
    RGB.control(false);
    sleepBeginMillis = millis() + 2000000;
    sleep = true;
  }

#include "application.h"
#include "MMA7660.h"

#define TFT_RST 20 //Place holder not used
#define YP A0  // must be an analog pin, use "An" notation!
#define XM A1  // must be an analog pin, use "An" notation!
#define TFT_CS A2 //Chip select pin for display
#define VoltagePin A6 //Determines system/battery voltage
#define Buzzer A7 //Buzzer for sound notification
#define TFT_LED D2 //Screen brightness control
#define TFT_DC D3 //Screen control pin
#define centerPin D4 //Center switch for tray alignment
#define acclInterrupt D5 // Acclemeter interrupt pin -
#define XP D6  // can be a digital pin
#define YM D7  // can be a digital pin
#define Charger RX //Open drain on LiPo charger, High when complete or disabled, low charging, flashing timer fault
#define LidSensorPin TX //Button to detect when lid is fully on
MMA7660 accelemeter;
int8_t x;   //accelemter x direection
int8_t y;   //accelemter y direction
int8_t z;   //accelemter z direction
float ax,ay,az; //accelemter x,y,z acceleration from -1 to 1

boolean readTilt = false;
unsigned char tiltRegRead[8];

boolean sleep = false;
unsigned long sleepBeginMillis = 0;
unsigned long sleepMillis = 60000;

void wakeUp();
unsigned char getTilt();
void sleepNow();

void setup(){
  Serial.begin(9600);
  pinMode(TFT_CS, INPUT_PULLUP);
  accelemeter.init(); //Start MMA7660 accelemeter
  delay(15);
  attachInterrupt(TFT_DC, wakeUp, FALLING);
  RGB.control(true);
  RGB.color(255, 0, 255);
  delay(400);
  RGB.control(false);
  //sleepBeginMillis = millis();
}


//LOOP***************************************************************************************************************
void loop() {
  accelemeter.getAcceleration(&ax,&ay,&az); //Get readings from the accel
  Serial.print("Sleep timer: ");
  Serial.println(millis() - sleepBeginMillis);
  delay(50);

  if(millis() - sleepBeginMillis > 10000){
    RGB.control(true);
    RGB.color(0, 255, 0);
    delay(300);
    RGB.control(false);
    sleepBeginMillis = millis() + 2000000;
    sleep = true;
  }



  if(readTilt){
    Serial.print("Interrupt: ");
    Serial.println(Time.timeStr());
    readTilt = false;
    sleep = false;
    sleepBeginMillis = millis();
  }

  if(sleep){
    sleepNow();
  }

}

void wakeUp(){
    readTilt = true;
    sleep = false;
}

unsigned char getTilt(){
  Wire.begin();
  Wire.beginTransmission(MMA7660_ADDR);
  Wire.write(0x03);  // register to read
  Wire.endTransmission();
  Wire.beginTransmission(MMA7660_ADDR);
  Wire.requestFrom(MMA7660_ADDR,1); // read a byte
  byte count = 0;
   while(Wire.available()){
     if(count < 9){
       tiltRegRead[count] = Wire.read();
     }
     count++;
   }
   Wire.endTransmission();
}

void sleepNow(){
  RGB.control(true);
  RGB.color(255, 0, 0);
  delay(200);
  RGB.control(false);
  sleep = false;
  sleepBeginMillis = millis() + 2000000;
  Serial.println("SLEEP MODE ENABLED");
  //Serial.print("  SLEEP Time: ");
  //Serial.println(sleepMillis);
  delay(100);
  System.sleep(TFT_DC, FALLING, 30);

}

The behavior is different between the Core and the Photon. On the Photon, in stop mode, the system resumes from where it left off. The stack, memory and everything is as it was before sleep was called, including the result from millis(). On the Core, the system is reset, so setup() will be executed and variables initialized to default values.

This is laid out in the firmware docs.
https://docs.particle.io/reference/firmware/photon/#system-sleep-

Thanks @mdma , reordered one line and now its working.