Traffic Light with a day and night mode

Hello, I'm a student working on a project that takes project 3 and 14 from the Earthshine Design pdf and combine them to create a basic red yellow and green traffic light that runs normal during the day and when the light dependent resistor(LDR) is covered or reads "night time" the order of the lights changes. I'd like it so the yellow light stays on and flashes during night but once light hits the LDR the yellow light then turns off and the lights change back to normal.

Below I have attached two photos of how I have the board laid out currently along with the code I'm starting off with. The code is just a copy and paste of the two projects. I'm not sure if I have the board wired right so even if I was able to get the code finished I'm not sure if the board would light up. As for the code I was planning to put in an if statement that after every light turned on normally such as red, green, yellow then red it would check if it night. If night then it would turn just the yellow on for lets say 2 seconds then do a check if it's still night. Any help or input on this would be greatly appreciated.

//Project 14 - Light Sensor
// Pin we will connect to LED
int ledPin = 9;
// Pin connected to LDR
int ldrPin = 0;
// Value read from LDR
int lightVal = 0;

// Project 3
int ledDelay = 10000; // delay in between changes
int redPin = 10;
int yellowPin = 9;
int greenPin = 8;
void setup()
{
  // Set both pins as outputs
  pinMode(ledPin, OUTPUT);
  
  // Project 3
  pinMode(redPin, OUTPUT);
  pinMode(yellowPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
}
void loop()
{
  // Read in value from LDR
  lightVal = analogRead(ldrPin);
  // Turn LED on
  digitalWrite(ledPin, HIGH);
  // Delay of length lightVal
  delay(lightVal);
  // Turn LED off
  digitalWrite(ledPin, LOW);
  // Delay again
  delay(lightVal);
  
  // Project 3
  // turn the red light on
digitalWrite(redPin, HIGH);
delay(ledDelay); // wait 5 seconds
digitalWrite(yellowPin, HIGH); // turn on yellow
delay(2000); // wait 2 seconds
digitalWrite(greenPin, HIGH); // turn green on
digitalWrite(redPin, LOW); // turn red off
digitalWrite(yellowPin, LOW); // turn yellow off
delay(ledDelay); // wait ledDelay milliseconds
digitalWrite(yellowPin, HIGH); // turn yellow on
digitalWrite(greenPin, LOW); // turn green off
delay(2000); // wait 2 seconds
digitalWrite(yellowPin, LOW); // turn yellow off
// now our loop repeats
}


Hi

  1. Does those leds lit with that code ? You could test your leds by using code something like this:
void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(yellowPin, OUTPUT);
  pinMode(greenPin, OUTPUT);

  digitalWrite(redPin, HIGH);
  delay(500);
  digitalWrite(redPin, LOW);
  delay(500);

  digitalWrite(yellowPin, HIGH);
  delay(500);
  digitalWrite(yellowPin, LOW);
  delay(500);

  digitalWrite(greenPin, HIGH);
  delay(500);
  digitalWrite(greenPin, LOW);
  delay(500);

}
  1. How you've connected your ldr resistor ?, schematic would be helpful.

  2. Are you getting any results for lightVal with different luminous intensity ?

   // Read in value from LDR
   lightVal = analogRead(ldrPin);

If you want to debug this with serial monitor, you've to add some code in your setup-function..

void setup()
{
  Serial.begin(9600);
  
}

..and also in your loop-function

void loop()
{
	// Read in value from LDR
	lightVal = analogRead(ldrPin);
	
	Serial.println(lightVal);
}
  1. If you do, learn how to use if's.
if (someCondition >= something) // someCondition is more or equals value something
{
    //here you want to do something
}

else if (someCondition < somethingElse) someCondition is less than some other value
{
    //here you want to do something else
}

or

else if (someCondition != something) // someCondition does not equal value something
{
    //here you want to do something else
}

With if's you could compare value for lightVal and perform different actions

  1. If you doesn't, check your connections.

TommiP

PS. You've declared your pin 9 twice..