Need help for final code part

Hi,

I am making a trafficlight with a speed camera that flashes when the traffic light is red and a car passes by. I am using a ir sensor to check if a car is passing.

The last part of the code is something i cant manage to do.
This is what i have already:

int trafficLights1[] = {2, 3, 4, 6};	// red, yellow, green, pedestrians led pins

int situations = 4;
int duration[] = {8000, 3000, 10000, 3000}; // duration of each situation
long previousCars = 0;
long previousPeds = 0;
long interval = 300;
long interval2 = 100;	//blink interval for pedestrians
int ledState = LOW;
int state;
int i = 0;
int photocellPin = A1;// Photocell connected to analog pin A1
int photocellVal = 0; // define photocell variable
int ledPin = 10;// LED connected to digital pin 10
int ledStatet = 0;//state of the led
int fadeDown = 30;//delay per fade
int fadeUp = 30;//delay per fade
int minLight = 100;//min light threshold
int maxLight = 100;//max light threshold
int led = 5;

void setup()
{
  for (int i = 0; i < 4; i++) {
    pinMode(trafficLights1[i], OUTPUT);

  }
  Serial.begin(9600);
  pinMode(photocellPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(0, OUTPUT);
}

void loop()
{
  unsigned long currentMillis = millis();
  if (currentMillis - previousCars < duration[i]) {
    situation(i);
  } else {
    previousCars = currentMillis;
    if (i >= situations) {
      i = 0;
    } else {
      i++;
    }
  }

  photocellVal = analogRead(photocellPin);

  if (photocellVal < minLight and ledStatet == 0) {
    fadeLed(1);
    //Serial.println("fade up");
  }
  else if (photocellVal > maxLight and ledStatet == 1) {
    fadeLed(0);
    // Serial.println("fade down");
  }
  int sensorValue = analogRead(A0);
  Serial.println(sensorValue);
  delay(1);        // delay in between reads for stability
  if (sensorValue < 600) {
    digitalWrite(5, HIGH);
    delay(200);
    digitalWrite(5, LOW);
    delay(1000);

  }
}


void activateTrafficLight1(String lights, int pedestrians)
{
  for (int x = 0; x < 3; x++)
  {
    if (lights[x] == '0') state = LOW;
    if (lights[x] == '1') state = HIGH;
    digitalWrite(trafficLights1[x], state);
  }
  if (pedestrians == 1) {
    blinkPed(trafficLights1[3]);
  } else {
    digitalWrite(trafficLights1[3], LOW);
  }
}


void situation(int i)
{
  switch (i) {
    case 0:
      activateTrafficLight1("100", 1); // 100 means red ON, yellow OFF, green OFF

      break;	// 1 is ON and 0 is OFF
    case 1:
      activateTrafficLight1("100", 0); // 110: red ON, yellow ON, green OFF

      break;
    case 2:
      activateTrafficLight1("001", 0);

      break;
    case 3:
      activateTrafficLight1("010", 0);

      break;
  }
}

void blinkPed(int ped) {
  unsigned long currentMillis = millis();
  if (currentMillis - previousPeds > interval) {
    previousPeds = currentMillis;
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;
    digitalWrite(ped, ledState);




  }
}

void fadeLed(int num) {
  if (num == 1) {
    for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
      analogWrite(ledPin, fadeValue);
      delay(fadeUp);
    }
    ledStatet = 1;

  }
  else {
    for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
      analogWrite(ledPin, fadeValue);
      delay(fadeDown);
    }
    ledStatet = 0;
  }


}

Led 5 should flash 1 time if a car passes by.
In the void loop you can see that i have a code that makes led 5 always flashing if a car passes by.

So I hope someone can help me with the last part of the code that makes led 5 flash 1 time if a car passes by and only if the red light is high.
(sorry for my bad english)

So the second car to run the red light will not get their photo taken?

Yep. A friend made a car that follows a line. I am making a trafficlight with a speedcamera, but instead of measuring speed it only checks if the car is not driving trough red light.

So actually there is no second car.
But what i want is:
Led 5 to blink 1 time if the car drives trough red light & if the sensor value of the ir is < 600 (wich is a value that something is in front of the ir.

I think this is what you want from your description:

String currentLightState = "000";

void activateTrafficLight1(String lights, int pedestrians)
{
....
   currentLightState = lights;
}

and then check the current RED light state in the appropriate "if" statement.

Just a sidenote, a String Object is not the best choice for this storage/parameter. Either separate booleans for each light, or a single byte using bitwise tests would be more efficient.

thanks for your respond. I am not very good with arduino and i dont understand your awnser. Can you please describe it a littlebit more. thanks in advance!

String currentLightState = "000";

void activateTrafficLight1(String lights, int pedestrians)
{
  for (int x = 0; x < 3; x++)
  {
    if (lights[x] == '0') state = LOW;
    if (lights[x] == '1') state = HIGH;
    digitalWrite(trafficLights1[x], state);
  }
  if (pedestrians == 1) {
    blinkPed(trafficLights1[3]);
  } else {
    digitalWrite(trafficLights1[3], LOW);
  }

   currentLightState = lights;
}

and change this part of loop() to

// check RED light is ON
if (currentLightState[0] == '1' && sensorValue < 600) {
    digitalWrite(5, HIGH);
    delay(200);
    digitalWrite(5, LOW);
    delay(1000);

  }

If i replace the code it says:

FINAL.ino: In function 'void activateTrafficLight1(String, int)': FINAL.ino:76:5: error: 'currentLightState' was not declared in this scope FINAL.ino:76:36: error: 'sensorValue' was not declared in this scope Error compiling

But by replacing it i lose the blinking light for the pedestrians.

Thymen: If i replace the code it says:

FINAL.ino: In function 'void activateTrafficLight1(String, int)': FINAL.ino:76:5: error: 'currentLightState' was not declared in this scope FINAL.ino:76:36: error: 'sensorValue' was not declared in this scope Error compiling

But by replacing it i lose the blinking light for the pedestrians.

put this line: String currentLightState = "000";

near the top of your program. Variables have to be declared before they are used in C/C++.