Traffic lights with photoresostor

Hi guys, I want to do traffic lights with photoresistor that when its day traffic lights function normal, but when its night I want the traffic light to function with the yellow light to on and off. My cercut is ok I want the code.
Below is the code i write:

int redcar1 = 13;   
int yellowcar1 = 12; 
int greencar1 = 11;  
int pedred1 = 10;  
int pedgreen1 = 9;  
int redcar2 = 8;  
int yellowcar2 = 7;  
int greencar2 = 6;  
int pedred2 = 5;  
int pedgreen2 = 4;  
int redcar3 = 1;  
int yellowcar3 = A0;  
int greencar3 = A1;  
int pedred3 = A2;  
int pedgreen3 = A3;  
int sensorPin = A5;

// Variables
int lightState = 0;
int lowThreshold = 200;
int highTreshold = 100;

void setup() {
  
  pinMode (redcar1 , OUTPUT);
  pinMode (yellowcar1 , OUTPUT);
  pinMode (greencar1 , OUTPUT);
  pinMode (pedred1 , OUTPUT);
  pinMode (pedgreen1 , OUTPUT);
  pinMode (redcar2 , OUTPUT);
  pinMode (yellowcar2 , OUTPUT);
  pinMode (greencar2 , OUTPUT);
  pinMode (pedred2 , OUTPUT);
  pinMode (pedgreen2 , OUTPUT);
  pinMode (button , INPUT);
  pinMode (redcar3 , OUTPUT);
  pinMode (yellowcar3 , OUTPUT);
  pinMode (greencar3 , OUTPUT);
  pinMode (pedred3 , OUTPUT);
  pinMode (pedgreen3 , OUTPUT);
  
  digitalWrite (redcar1 , LOW);
  digitalWrite (yellowcar1 , LOW);
  digitalWrite (greencar1 , HIGH);
  digitalWrite (pedred1 , HIGH);
  digitalWrite (pedgreen1 , LOW);
  digitalWrite (redcar2 , HIGH);
  digitalWrite (yellowcar2 , LOW);
  digitalWrite (greencar2 , LOW);
  digitalWrite (pedred2 , LOW);
  digitalWrite (pedgreen2 , HIGH);
  digitalWrite (redcar3 , HIGH);
  digitalWrite (yellowcar3 , LOW);
  digitalWrite (greencar3 , LOW);
  digitalWrite (pedred3, LOW);
  digitalWrite (pedgreen3 , HIGH);
  delay(15000); 
  Serial.begin(9600);
  
}

void loop() {   
    // Read the sensor pin
  int sensorValue = analogRead(sensorPin);

  // If light level is low is detected, switch light on
  if (sensorValue < lowThreshold){
digitalWrite(yellowcar1, LOW);
    delay (1000);
    digitalWrite (yellowcar1, HIGH); 
    delay (1000);
  }
  
 else{
  
  digitalWrite (redcar1 , LOW);
  digitalWrite (yellowcar1 , HIGH);
  digitalWrite (greencar1 , LOW);
  digitalWrite (redcar2 , HIGH);
  digitalWrite (yellowcar2 , LOW);
  digitalWrite (greencar2 , LOW);
  digitalWrite (redcar3 , HIGH);
  digitalWrite (yellowcar3 , LOW);
  digitalWrite (greencar3 , LOW);

  digitalWrite (pedred1 , HIGH);
  digitalWrite (pedgreen1 , LOW);
  digitalWrite (pedred2 , HIGH);
  digitalWrite (pedgreen2 , LOW);
  digitalWrite (pedred3, LOW);
  digitalWrite (pedgreen3 , HIGH);
  delay(3000); 
  
  digitalWrite (redcar1 , HIGH);
  digitalWrite (yellowcar1 , LOW);
  digitalWrite (greencar1 , LOW);
  digitalWrite (redcar2 , LOW);
  digitalWrite (yellowcar2 , LOW);
  digitalWrite (greencar2 , HIGH);
  digitalWrite (redcar3 , HIGH);
  digitalWrite (yellowcar3 , LOW);
  digitalWrite (greencar3 , LOW);

  digitalWrite (pedred1 , LOW);
  digitalWrite (pedgreen1 , HIGH);
  digitalWrite (pedred2 , HIGH);
  digitalWrite (pedgreen2 , LOW);
  digitalWrite (pedred3, LOW);
  digitalWrite (pedgreen3 , HIGH);
  delay(15000); 
  
  digitalWrite (redcar1 , HIGH);
  digitalWrite (yellowcar1 , LOW);
  digitalWrite (greencar1 , LOW);
  digitalWrite (redcar2 , LOW);
  digitalWrite (yellowcar2 , HIGH);
  digitalWrite (greencar2 , LOW);
  digitalWrite (redcar3 , HIGH);
  digitalWrite (yellowcar3 , LOW);
  digitalWrite (greencar3 , LOW);

  digitalWrite (pedred1 , LOW);
  digitalWrite (pedgreen1 , HIGH);
  digitalWrite (pedred2 , HIGH);
  digitalWrite (pedgreen2 , LOW);
  digitalWrite (pedred3, HIGH);
  digitalWrite (pedgreen3 , LOW);
  delay(3000); 
  
  digitalWrite (redcar1 , HIGH);
  digitalWrite (yellowcar1 , LOW);
  digitalWrite (greencar1 , LOW);
  digitalWrite (redcar2 , HIGH);
  digitalWrite (yellowcar2 , LOW);
  digitalWrite (greencar2 , LOW);
  digitalWrite (redcar3 , LOW);
  digitalWrite (yellowcar3 , LOW);
  digitalWrite (greencar3 , HIGH);
  digitalWrite (pedred1 , LOW);
  digitalWrite (pedgreen1 , HIGH);
  digitalWrite (pedred2 , LOW);
  digitalWrite (pedgreen2 , HIGH);
  digitalWrite (pedred3, HIGH);
  digitalWrite (pedgreen3 , LOW);
  delay(15000); 
  
  digitalWrite (redcar1 , HIGH);
  digitalWrite (yellowcar1 , LOW);
  digitalWrite (greencar1 , LOW);
  digitalWrite (redcar2 , HIGH);
  digitalWrite (yellowcar2 , LOW);
  digitalWrite (greencar2 , LOW);
  digitalWrite (redcar3 , LOW);
  digitalWrite (yellowcar3 , HIGH);
  digitalWrite (greencar3 , LOW);
  
  digitalWrite (pedred1 , HIGH);
  digitalWrite (pedgreen1 , LOW);
  digitalWrite (pedred2 , LOW);
  digitalWrite (pedgreen2 , HIGH);
  digitalWrite (pedred3, LOW);
  digitalWrite (pedgreen3 , HIGH);
  delay(3000); 
  
  digitalWrite (redcar1 , LOW);
  digitalWrite (yellowcar1 , LOW);
  digitalWrite (greencar1 , HIGH);
  digitalWrite (redcar2 , HIGH);
  digitalWrite (yellowcar2 , LOW);
  digitalWrite (greencar2 , LOW);
  digitalWrite (redcar3 , HIGH);
  digitalWrite (yellowcar3 , LOW);
  digitalWrite (greencar3 , LOW);

  digitalWrite (pedred1 , HIGH);
  digitalWrite (pedgreen1 , LOW);
  digitalWrite (pedred2 , LOW);
  digitalWrite (pedgreen2 , HIGH);
  digitalWrite (pedred3, LOW);
  digitalWrite (pedgreen3 , HIGH);
  delay(15000); 
  }
}

What exactly is your problem? Does it work?

aarg:
What exactly is your problem? Does it work?

When I run the code with only the code for photoresistor it work perfectly, but when I run the photoresistor with the "normal function lights" code it doesn't work.

go back to your post and hit EDIT on the bottom right

at the start of your code type "[" followed by the word code and then a close bracket

that that after the end of your code but use /code in brackets

you will get little help if you do not follow the forum rules.

they are posted at the FIRST or second post on EVERY forum.
http://forum.arduino.cc/index.php/topic,148850.0.html

this :

  else{

  
  

  digitalWrite (redcar1 , LOW);
  digitalWrite (yellowcar1 , HIGH);
  digitalWrite (greencar1 , LOW);
  digitalWrite (redcar2 , HIGH);
  digitalWrite (yellowcar2 , LOW);
  digitalWrite (greencar2 , LOW);
  digitalWrite (redcar3 , HIGH);
  digitalWrite (yellowcar3 , LOW);
  digitalWrite (greencar3 , LOW);

  digitalWrite (pedred1 , HIGH);
  digitalWrite (pedgreen1 , LOW);
  digitalWrite (pedred2 , HIGH);
  digitalWrite (pedgreen2 , LOW);
  digitalWrite (pedred3, LOW);
  digitalWrite (pedgreen3 , HIGH);
  delay(3000);

will run in a ms and then pause for 3 seconds.

do you want to have each write delay for a bit ? the human eye cannot see leds blink on and off that fast.

....will run in a ms and then pause for 3 seconds.

do you want to have each write delay for a bit ? the human eye cannot see leds blink on and off that fast.

I don't think it's supposed to be seen. The OP is setting up the light pattern and then holding it for 3 seconds. That part makes sense.

aarg:
I don't think it's supposed to be seen. The OP is setting up the light pattern and then holding it for 3 seconds. That part makes sense.

"
My probles is that when I run only the "if" works perfectly, but when I run the "if" with the rest of the code It doesn't work. It's like the "if" doesn't exist.

Define "it doesn't work" for us. What happens or doesn't happen?

aarg:
Define "it doesn't work" for us. What happens or doesn't happen?

when I press the button nothing happen, the pedestrian light would have to turn green but doesn't respond. When I run only the "if" in a separate file works perfectly.

You've defined "button", but it is not used in the code you've posted.

For what it's worth, and code implementation is subjective, it is very difficult to look at your code and follow what it is trying to do. As a minimum a comment for setting a group of pin states would be helpful.

// Set car1 to green, set car2 to yellow, set car3 to red, . . .

Better might be to use function calls such that the code essentially says as much.

Car1State(green) ;
Car2State(red) ;
. . .

where Car1State is a function defined as

// Set state of Car1 signal given sigState of {red_lamp, yellow_lamp, green_lamp}
void Car1State(int sigState[]) {
  digitalWrite (redcar1,sigState[0]) ;
  digitalWrite (yellowcar1,sigState[1]) ;
  digitalWrite (greencar1,sigState[2]) ;
}

and green is a state array for a signal light

// Define traffic signal states as {red_lamp, yellow_lamp, green_lamp}
int green[]  = { LOW,  LOW, HIGH} ;
int yellow[] = { LOW, HIGH,  LOW} ;
int red[]    = {HIGH,  LOW,  LOW} ;
int allOff[] = { LOW,  LOW,  LOW} ;

The benefit of making the code more readable and more modular is that silly mistakes are more easily spotted and when the code does not work as expected, it is easier to track down the offending bit.

(Disclaimer: code example is incomplete, untested, and probably has mistakes. A more competent programmer could almost certainly do better.)

After:

int sensorValue = analogRead(sensorPin);

add the line

Serial.println (sensorValue);

Then you'll be able to see in the Serial Monitor what the value is.

Fanaris u are dont use delay function, u can use millis or timer interrupt because delay is stoppted your system so u don't read analogRead(sensorPin),

for ex.

int read=analogRead(sensorpin);

digitalWrite (redcar1 , LOW);

if(sensorpin<<99)

{
digitalWrite (redcar1 , LOW);

}

delay(15000); no no this false ,u not read //analogRead(sensorpin);// this too important

using millis, interrupt.

aarg:
I don't think it's supposed to be seen. The OP is setting up the light pattern and then holding it for 3 seconds. That part makes sense.

yes, it does make sense. my bad.

delay is not the best to use, but certainly CAN be used.

if you do not use delay, then you need too set a flag that you are in the timing cycle.

here is a way to use millis()
of course, you would need to add in the pedestrian colors.

this is simple IF() statement to help you see how it might be done.

if(car_flag==LOW){    // this flag goes low AFTER the lights change.
  if ( car_sensed){
    car_flag=HIGH;
    timing=millis();
  }
}

if ( car_flag==HIGH){

  //this will pause then cycle the lights
  if (millis()-timing>1000  && <3000){  // more than 1 second and less than LT 3
    digitalWrite(red,LOW);
    digitalWrite(yellow,HIGH);
    digitalWrite(green,LOW);
  }

  if (millis()-timing>3000  && <5000){  // more than 3 second and less than LT 5
    digitalWrite(red,HIGH);
    digitalWrite(yellow,LOW);
    digitalWrite(green,LOW);
  }

  if (millis()-timing>5000){  // more than 5 seconds
    digitalWrite(red,LOW);
    digitalWrite(yellow,LOW);
    digitalWrite(green,HIGH);
    car_flag-LOW;  // only changes after lights cycles one time.

  }
}

this is incomplete and only to show one way it might be done.
this changes to green, then stops. it would need to time out, then change to green the other way.

(Disclaimer: code example is incomplete, untested, and probably has mistakes. A more competent programmer could EASILY certainly do better.)

the pattern is WRONG, but the idea is that you set a flag, then with some sort of timing, you step through your lights changing colors.

using delay() would lock out everything WHILE the lights are in the timing loop. not a bad thing.
once the loop is complete, the program will go back and scan for a car

if(car_flag==LOW){
 if ( car_sensed){
   car_flag=HIGH;
 
 }
}

if ( car_flag==HIGH){

 //this will pause then cycle the lights
   digitalWrite(red,LOW);
   digitalWrite(yellow,HIGH);
   digitalWrite(green,LOW);
   delay(2000); //  2 seconds yellow
 
   digitalWrite(red,HIGH);
   digitalWrite(yellow,LOW);
   digitalWrite(green,LOW);
   delay(2000); //  2 seconds red
 
   digitalWrite(red,LOW);
   digitalWrite(yellow,LOW);
   digitalWrite(green,HIGH);
   car_flag-LOW;  // only changes after lights cycles one time.
 }

(Disclaimer: code example is incomplete, untested, and probably has mistakes. A more competent programmer could almost certainly do better.)

aarg:
I don't think it's supposed to be seen. The OP is setting up the light pattern and then holding it for 3 seconds. That part makes sense.

you would be correct.
he changes the state of all the lights, then does the delay for his time.
then cycles the red/yellow/green in all lights for his pattern.
his use of delay() blocks everything while the pattern does the canned routine.

once the routine is complete, I assume that it would return to a 'holding pattern' and just keep looking for a sensor to change state to start the routine all over again.

one question is about the sensor.

did you check to see if it goes HIGH at night or HIGH during the day ?

it might be that the whole problem is a simple inverted signal.

dave-in-nj:
one question is about the sensor.

did you check to see if it goes HIGH at night or HIGH during the day ?

it might be that the whole problem is a simple inverted signal.

Hence my suggestion in reply #11 that he Serial.prints the value as soon as it is read.
The Serial Monitor will then give him the answer to that question.

Hi,
Fanaris we need to know somethings about your project.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
As has been asked, we need to know how you have the cell connected and the button.

Tom... :slight_smile: