Having trouble creating Easter egg like codes.

I'm trying to use the nintendo controller to control a lighting system. I want an action to occur when I push up three times. The code should expire if the last button push is longer than 500ms. I'm having a hard time sensing this for some reason. Here's the code so far.

#include <NESpad.h>

// put your own strobe/clock/data 
NESpad nintendo = NESpad(2,3,4);

unsigned long codeWindow = 0;
unsigned long time = 0;
int codeCount = 0;

byte state = 0;

void setup() {

}

void loop() {
  
  state = nintendo.buttons();
  

//testing for button push
if (state & NES_UP){
  //recording time when button was pushed
  time = millis();
  
  codeCount = 1;
}

//testing for button release inside a 500ms time window
if (!(state & NES_UP) && (millis() - time) < 500){
  codeCount = 2;
}



//Print out
Serial.begin(4800);
Serial.println(codeCount);

  delay(20); 

}

Just trying to sense the push and release of the up button within 500ms.

I don't know anything about the NES, but you keep resetting the time for every iteration of the loop, so when it finally does get released, the time difference is always going to be really short, just a few cpu cycles. From your description, this isn't what you want. I think you want to detect when the button changes state, not merely when it is pressed.

I want the code to also expire if its not competed in a timely fashion.

The simplest way is to define a stopwatch that is started at the first keypress. Then count the keypresses and stop the stopwatch after the 3rd. If the value of the stopwatch is lower than 500ms then do your thing else reset the system.

Check - Arduino Playground - StopWatchClass -