Trouble programming an ldr sensor for a traffic light system

Hi there,

I'm new to programming and I'm trying to add an LDR sensor to my pre-existing traffic light system, basically, I want it so that when my sensor hits a certain threshold of darkness the time that it takes for the light to change decreases to 7 seconds for my red and green light, rather than 15. But I have absolutely no clue how to go about coding this? I've attached my TinkerCad arduino design and my code, I hope you can help me.

void setup()
{
pinMode(13, OUTPUT); // Red LED
pinMode(12, OUTPUT); // Yellow LED
pinMode(11, OUTPUT); // Green LED
}
void loop()
{
digitalWrite(13, HIGH); //Turns on the red light
delay(15000); // red light for 15 seconds
digitalWrite(13, LOW); // Turns off the red light

digitalWrite(12, HIGH); // Turns amber light on
delay(6000); // Keeps amber light on for 6 seconds
digitalWrite(12, LOW); // Turns amber light off

digitalWrite(11, HIGH); // Turns on green gight
delay(15000); // Keeps on for 15 seconds
digitalWrite(11, LOW); // Turns green light off
}

Have you tried (analog) reading pin A0, and simply printing the result?
That might be a start.

Also compare the way that you have wired the LDR with a known good example.
The LDR and the 10K resistor should be in a voltage divider arrangement, with the Analog pin in the middle.

Have You heard about analogRead? Can be used for the LDR.

Does this make any sense?

#define LDR_pin xx
#define darkValue yy

void setup()
{
  pinMode(13, OUTPUT); // Red LED
  pinMode(12, OUTPUT); // Yellow LED
  pinMode(11, OUTPUT); // Green LED
  pinMode(LDR_pin, INPUT); //Input for LDR
}

void loop()
{
   if( analogRead(LDR_pin ) < darkvalue )
     delay_time = 7000;
   else
      delay_time = 15000;

  digitalWrite(13, HIGH); //Turns on the red light
  delay(delayTime); // red light for 15 seconds
  digitalWrite(13, LOW); // Turns off the red light
  
  digitalWrite(12, HIGH); // Turns amber light on
  delay(6000); // Keeps amber light on for 6 seconds
  digitalWrite(12, LOW); // Turns amber light off
  
  digitalWrite(11, HIGH); // Turns on green gight
  delay(delayTime); // Keeps on for 15 seconds
  digitalWrite(11, LOW); // Turns green light off
}
if( analogRead(LDR_pin < darkvalue )

Oops

 pinMode(LDR_pin, INPUT);

Unnecessary

Better now?

Railroader:
Have You heard about analogRead? Can be used for the LDR.

Does this make any sense?

#define LDR_pin xx

#define darkValue yy

void setup()
{
 pinMode(13, OUTPUT); // Red LED
 pinMode(12, OUTPUT); // Yellow LED
 pinMode(11, OUTPUT); // Green LED
 pinMode(LDR_pin, INPUT); Input for LDR
}

void loop()
{
  if( analogRead(LDR_pin ) < darkvalue )
    delay_time = 7000;
  else
     delay_time = 15000;

digitalWrite(13, HIGH); //Turns on the red light
 delay(delayTime); // red light for 15 seconds
 digitalWrite(13, LOW); // Turns off the red light
 
 digitalWrite(12, HIGH); // Turns amber light on
 delay(6000); // Keeps amber light on for 6 seconds
 digitalWrite(12, LOW); // Turns amber light off
 
 digitalWrite(11, HIGH); // Turns on green gight
 delay(delayTime); // Keeps on for 15 seconds
 digitalWrite(11, LOW); // Turns green light off
}

Thank you, but I am unsure what I am defining LDR_Pin as? I’m extremely new so I don’t have the foggiest idea :confused:

According to your Fritzing thing, it’s A0

Replace "xx" with A0 or A1 or..... #define ldr_pin A0
The same for the darkValue.

You need to make some tests to find out the value.

TheMemberFormerlyKnownAsAWOL:
According to your Fritzing thing, it’s A0

Whenever I type the altered code these problems come up.
void setup()
{
pinMode(13, OUTPUT); // Red LED
pinMode(12, OUTPUT); // Yellow LED
pinMode(11, OUTPUT); // Green LED
pinMode(A0, INPUT); // Input for LDR
}
void loop()
{
if analogRead(A0) < 300 ;
delay_time = 7000;

else
delay_time = 15000;
digitalWrite(13, HIGH); //Turns on the red light
delay(delay_time); // red light for 15 seconds
digitalWrite(13, LOW); // Turns off the red light

digitalWrite(12, HIGH); // Turns amber light on
delay(6000); // Keeps amber light on for 6 seconds
digitalWrite(12, LOW); // Turns amber light off

digitalWrite(11, HIGH); // Turns on green gight
delay(delay_time); // Keeps on for 15 seconds
digitalWrite(11, LOW); // Turns green light off
}

In function ‘void loop()’:
13:7: error: expected ‘(’ before ‘analogRead’
14:6: error: ‘delay_time’ was not declared in this scope
14:6: note: suggested alternative: ‘_delay_ms’
16:3: error: ‘else’ without a previous ‘if’
exit status 1

{
   if (analogRead(A0) < 300) 
     delay_time = 7000;

Please remember to use code tags when posting code.

If you look carefully, the error message

error: expected '(' before 'analogRead'

is giving massive hints about what is wrong.

See if you can fix 14:6: error: 'delay_time' was not declared yourself.
We’ll help of you get stuck.

You missed the 2 lines with #define

  if( analogRead(LDR_pin ) < 300 )

Not properly copied…