Help me please. IR breakbeam sensor

I work with an IR breakbeam sensor, a green, red and yellow LED.

If the sensor is "high", a green LED must be lit and the red and yellow LED must be off.
If the sensor is "low", the green LED must be off, the yellow LED must be on and after 3 seconds the yellow LED must be off, the red LED must be on. I have the problem that the yellow LED is still on.

How should I resolve this? Someone who knows?

My program is:
/*
IR Breakbeam sensor demo!
*/

#define LEDPIN 8
#define geelPin 9
#define roodPin 10
// Pin 13: Arduino has an LED connected on pin 13
// Pin 11: Teensy 2.0 has the LED on pin 11
// Pin 6: Teensy++ 2.0 has the LED on pin 6
// Pin 13: Teensy 3.0 has the LED on pin 13

#define SENSORPIN 2

// variables will change:
int sensorState = 0, lastState=0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(LEDPIN, OUTPUT);
pinMode(roodPin,OUTPUT);
pinMode(geelPin,OUTPUT);
// initialize the sensor pin as an input:
pinMode(SENSORPIN, INPUT);
digitalWrite(SENSORPIN, HIGH); // turn on the pullup

Serial.begin(9600);
}

void loop(){
// read the state of the pushbutton value:
sensorState = digitalRead(SENSORPIN);

// check if the sensor beam is broken
// if it is, the sensorState is LOW:
if (sensorState == HIGH) {
// turn LED on:
digitalWrite(LEDPIN, HIGH);
digitalWrite(roodPin,LOW);
digitalWrite(geelPin,LOW);
}
else {
// turn LED off:
digitalWrite(LEDPIN, LOW);
digitalWrite(geelPin,HIGH);
delay(3000);
digitalWrite(geelPin,LOW);
digitalWrite(roodPin,HIGH);
}
digitalWrite(geelPin,LOW);

if (sensorState && !lastState) {
Serial.println("Unbroken");
}
if (!sensorState && lastState) {
Serial.println("Broken");
}
lastState = sensorState;
}

i believe you need to recognize the transition from HIGH to LOW, not simply its state. Otherwise you repeatedly do the same thing whenever it is LOW.

so your else case should be

else if (HIGH == lastState)

I tried it, but it's just the same problem.

what exactly is it doing? which LED is on?

Post your latest attempt using a code block.

gciurpita

when the sensor is LOW, the green LED is off, the yellow LED is on. after 3 seconds, the red LED is on, but also the yellow LED is on.
I think I did what you said, like this

if (lastState == LOW){
// turn LED off:
digitalWrite(groenPin, LOW);
digitalWrite(geelPin,HIGH);
delay(3000);
digitalWrite(geelPin,LOW);
digitalWrite(roodPin,HIGH);
}
else if (lastState == HIGH) {
// turn LED on:
digitalWrite(groenPin, HIGH);
digitalWrite(roodPin,LOW);
digitalWrite(geelPin,LOW);
}

but it is still the same problem

when i try your code with LEDs,
with pin 2 grounded, i see the LEDs on pins 9 & 10 lit,
with pin 2 floating, i see the LED on pin 8 turn on, sometimes with a delay

when I add the test I suggested else if (lastState == HIGH)
with pin 2 grounded, i see the LED on pins 9 turn on and after a delay turn off the LED on pin 10 turn on,
with pin 2 floating, i see the LED on pin 8 turn on immediately.

since there's no debounce, the delayed case sometimes cycles before settling on the sensor HIGH case.

to set the sensor pin as an input with pullups, do
pinMode (SENSORPIN, INPUT_PULLUP)
the digitalWrite does not "turn on the pullup"

I don't understand that from the "pinMode(sensorpin,INPUT_PULLUP)"

and yes, I know, there is a little delay from the LOW state to the HIGH state

but with the LOW state I want the yellow LED on, after 3 seconds the yellow LED off and the red LED on
but with the program, the yellow LED stays on while the red LED is on

Joris_Damman:
I work with an IR breakbeam sensor, a green, red and yellow LED.

If the sensor is "high", a green LED must be lit and the red and yellow LED must be off.
If the sensor is "low", the green LED must be off, the yellow LED must be on and after 3 seconds the yellow LED must be off, the red LED must be on. I have the problem that the yellow LED is still on.

First - this is worded quite badly. It's not "if the sensor is high", but "when the sensor changes from low to high". Think in terms of events.

Second - what is supposed to happen if the sensor goes high during the three second delay?