Red Light Green Light- with PIR Sensor

Hi guys,

So I am a beginner and I'm building a little bot that can play red light - greed light. To summarize, it's a game where once the light is green you can sneak up on the bot and once it turns red you must freeze in position. If you move then you're out.

I am using a PIR-sensor to detect movement, but once the state is fired off, it declares that motion was detected, whether or not it actually detected movement.

Here is an example of my code. ~

#define WAITING 0
#define PLAY 1
#define PIRSENSOR 2
#define END 3
unsigned long greenLightStartTime = 0;
unsigned long redlightStartTime = 0;
const int pirPin = 1;
const int buttonPin = 2;
const int ledPin1 = 13;
const int ledPin2 = 12;

int state = WAITING;
int buttonState = 0;
bool buttonwaspressed = false;

void setup() {
Serial.begin(9600);
pinMode(pirPin,INPUT);
pinMode(ledPin1,OUTPUT);
pinMode(ledPin2,OUTPUT);
pinMode(buttonPin,INPUT);
}

void loop() {

if(state == WAITING) {
int buttonState = digitalRead(buttonPin);

if(buttonState == HIGH) { //waiting for buttonstate - if pressed will flash green for 3000ms//
Serial.println("on");
digitalWrite(ledPin1, HIGH);
greenLightStartTime = millis();
state = WAITING;

} else if(millis() - greenLightStartTime > 1000) { //change to 10 or 15 sec later//
digitalWrite(ledPin1, LOW);
buttonwaspressed = true;
delay(50);
state = PIRSENSOR;
}
}

if(state == PIRSENSOR){
int pirVal = digitalRead(pirPin);
Serial.println(pirVal);

digitalWrite(ledPin2, HIGH);
redlightStartTime = millis();

if (pirVal == LOW) {
Serial.println("Motion Detected");
state = END;

} else (millis() - redlightStartTime > 3000); { //change to 10 sec later//
digitalWrite(ledPin2, LOW);
state = PLAY;
}
}

if(state == PLAY) {
digitalWrite(ledPin1, HIGH);
delay(1000); //edit later - change maybe to 5000ms//
digitalWrite(ledPin1, LOW);
state = PIRSENSOR;
}

if(state == END){
digitalWrite(ledPin2, LOW);
delay(50);
digitalWrite(ledPin2, HIGH);
delay(500);
digitalWrite(ledPin2, LOW);
delay(500);
digitalWrite(ledPin2, HIGH);
delay(500);
digitalWrite(ledPin2, LOW);
delay(500);
digitalWrite(ledPin2, HIGH);
delay(500);
digitalWrite(ledPin2, LOW);
state = WAITING;
}
}

Please help.

Just an FYI....

there are tools for this:

#define WAITING 0
#define PLAY 1
#define PIRSENSOR 2
#define END 3

int state = WAITING;

check out the enum resource:

enum GameState{
  GAME_WAITING,  // prefer to create Very unique names
  GAME_PLAY,     // so that you don't run over any other names
  GAME_PIRSENSOR,
  GAME_OVER 
};

GameState state = GAME_WAITING;