Photo Resistor with a set of Traffic Lights in Arduino (Coding Problem)

Hey,

I'm making a traffic light that turns green when a photo resistor detects motion (touch it). The physical component of it is working (with a simple code), however when I add the code into the traffic light code it just doesn't work. Please help, I have been trying for a long time. Thanks!

int NorthRed = 11; //North Red Traffic light being assigned
int NorthYellow = 12; //North Yellow Traffic light being assigned
int NorthGreen = 13; //North Green Traffic light being assigned
int pedRed = 8; //pedistrian red light being assigned
int pedGreen = 10; //pedistrian green light being assigned
int button = 7;//button pin being assigned
int EastRed = 6; //East Red Traffic light being assigned
int EastYellow = 5; //East Yellow Traffic light being assigned
int EastGreen = 4; //East Green Traffic light being assigned
int SouthRed = 3; //South Red Traffic light being assigned
int SouthYellow = 2; //South Yellow Traffic light being assigned
int SouthGreen = 1; //South Green Traffic light being assigned
int lightPin = 0;
int ledPin = 1;

int crossTime = 3000; //crossing time given to pedestrians (3 seconds)
unsigned long changeTime ; // collects the time since the button was last pressed

void setup () {

// setup digital I/O pins

pinMode(ledPin, OUTPUT); //sets the led pin to output
pinMode(NorthRed, OUTPUT);
pinMode(NorthYellow, OUTPUT);
pinMode(NorthGreen, OUTPUT);
pinMode(pedRed, OUTPUT);
pinMode(pedGreen, OUTPUT);
pinMode(button, INPUT);
pinMode(EastRed,OUTPUT);
pinMode(EastYellow, OUTPUT);
pinMode(EastGreen, OUTPUT);
pinMode(SouthRed,OUTPUT);
pinMode(SouthYellow, OUTPUT);
pinMode(SouthGreen, OUTPUT);

// set intial state for lights

digitalWrite(pedRed, HIGH); //start with red ped light on
digitalWrite(EastGreen, HIGH); //start with east green light on
digitalWrite(NorthRed,HIGH); //start with north red light on
digitalWrite(SouthRed,HIGH); //start with south red light on

}

void loop(){
/* check if button is pressed
and it is over 3 sec since last button press*/
int state = digitalRead(button);
if(state==HIGH && (millis() - changeTime) > 3000) {
changeLights(); //function to change lights

int lightLevel = analogRead(lightPin); //Read the
// lightlevel
lightLevel = map(lightLevel, 0, 900, 0, 255);
//adjust the value 0 to 900 to
//span 0 to 255

lightLevel = constrain(lightLevel, 0, 255);//make sure the
//value is between
//0 and 255
analogWrite(ledPin, lightLevel); //write the value
}

}

void changeLights() {

digitalWrite(EastGreen, LOW); //East green light off
digitalWrite(EastRed, LOW); //East red light off
digitalWrite(EastYellow, HIGH); //East yellow light on
delay(2000); //wait 2 seconds
digitalWrite(EastYellow, LOW); //East yellow light off
digitalWrite(EastGreen, LOW); //East green light off
digitalWrite(NorthRed, LOW); //North red light off
digitalWrite(SouthRed, LOW); //South red light off
digitalWrite(EastYellow, LOW); //East yellow light off
digitalWrite(NorthGreen, HIGH); //North green light on
digitalWrite(SouthGreen, HIGH); //South green light on
digitalWrite(EastGreen, LOW); //East green light off
digitalWrite(EastRed, HIGH); //East red light on
digitalWrite(NorthGreen,LOW); //North green light off
digitalWrite(SouthGreen,LOW); //South green light off
digitalWrite(NorthRed, HIGH); //North red light on
digitalWrite(SouthRed,HIGH); //South red light on
digitalWrite(EastRed, LOW); //East red light off
digitalWrite(EastYellow, HIGH); // East yellow light on
delay(2000); //wait 2 seconds

digitalWrite(EastYellow,LOW); // East yellow light off
digitalWrite(NorthYellow,LOW); //North yellow light off
digitalWrite(SouthYellow,LOW); //South yellow light off
digitalWrite(NorthRed,LOW); //North red light off
digitalWrite(SouthRed,LOW); //South red light off
digitalWrite(EastGreen, LOW); //East green light off
digitalWrite(EastYellow, LOW); // East yellow light off
digitalWrite(EastRed, HIGH); //East red light on
digitalWrite(pedRed,HIGH); //ped red light on
digitalWrite(pedGreen,LOW); //ped green light on
digitalWrite(NorthRed, HIGH); //North red light on
digitalWrite(SouthRed, HIGH); //South red light on
digitalWrite(NorthRed, LOW); //North red light off
digitalWrite(SouthRed, LOW); //South red light off
digitalWrite(SouthGreen,HIGH); //South green light on
digitalWrite(NorthGreen,HIGH); //North green light on
delay(500); //wait 0.5 seconds to turn on ped light

digitalWrite(pedRed,LOW); //red ped light off
digitalWrite(pedGreen,HIGH); //green ped light on. allow crossing
delay(crossTime); //delay preset time of 3 seconds
//flashing of ped green light
for (int x=0; x<10; x++){
digitalWrite(pedGreen,HIGH); //ped green light on
delay(250); //wait 0.25 seconds
digitalWrite(pedGreen,LOW); //ped green light off
delay(250); //wait 0.25 seconds
}

digitalWrite(pedGreen, LOW); //green ped light off
delay(100); //wait 0.1 seconds
digitalWrite(NorthGreen, LOW); //North green light off
digitalWrite(SouthGreen, LOW); //South green light off
digitalWrite(SouthYellow, HIGH); //South yellow light on
digitalWrite(NorthYellow, HIGH); //North yellow light on
delay(1000); //wait 1 second
digitalWrite(NorthYellow, LOW); //North yellow light off
digitalWrite(SouthYellow, LOW); //South yellow light off
digitalWrite(pedRed, HIGH); //ped red light on
digitalWrite(NorthGreen,LOW); //North green light off
digitalWrite(SouthGreen,LOW); //South green light off
digitalWrite(EastRed, LOW); //East red light off
digitalWrite(NorthRed,HIGH); //North red light off
digitalWrite(SouthRed,HIGH); //South red light off
digitalWrite(pedGreen,LOW); //ped green light off
digitalWrite(EastGreen, HIGH); //East green light on

changeTime = millis(); //record time since last lights change

}

Nice to see that you have read and embraced the topic pinned to the top of this section: How to use this forum - please read.

We don't get many traffic light programmers asking for advice on the forum. Is this a school / college project?

Yes it is a school project, however my teacher also didn't know what was wrong and told me to post in this forum for help.

In that case read the topic I mentioned: How to use this forum - please read.. Format your code using the Auto Format function - Ctrl + T - and post it using the code tags as explained in the topic referred to. It might not be the most exciting five minutes of your life but will probably help you immeasurably.

I understand, but how is this going to help me solve my coding problems?

Change
if( state == HIGH && (millis( ) - changeTime) > 3000 ) {
to
if( (state == HIGH) && ((millis( ) - changeTime) > 3000) )
(extra parens)

There are multiple settings for some LEDs within the same period between delay( )s. You'll never see those changes. Also it's extremely hard to read with the LEDs in random order. Group your East, North, South changes together. Always put colors in the same order. It will make it much easier to read.

What does it not do? What do you expect it to do? What board?

Thanks! What it doesn't do is turn on a green light when something is touching the motion sensor/photo resistor. Which is what I expect it to do. And it is an arduino UNO.

Heres the new code I have:

int NorthRed = 11; //North Red Traffic light being assigned
int NorthYellow = 12; //North Yellow Traffic light being assigned
int NorthGreen = 13; //North Green Traffic light being assigned
int pedRed = 8; //pedistrian red light being assigned
int pedGreen = 10; //pedistrian green light being assigned
int button = 7;//button pin being assigned
int EastRed = 6; //East Red Traffic light being assigned
int EastYellow = 5; //East Yellow Traffic light being assigned
int EastGreen = 4; //East Green Traffic light being assigned
int SouthRed = 3; //South Red Traffic light being assigned
int SouthYellow = 2; //South Yellow Traffic light being assigned
int SouthGreen = 1; //South Green Traffic light being assigned
int lightPin = 0;
int ledPin = 1;

int crossTime = 3000; //crossing time given to pedestrians (3 seconds)
unsigned long changeTime ; // collects the time since the button was last pressed

void setup () {

// setup digital I/O pins

pinMode(ledPin, OUTPUT); //sets the led pin to output
pinMode(NorthRed, OUTPUT);
pinMode(NorthYellow, OUTPUT);
pinMode(NorthGreen, OUTPUT);
pinMode(pedRed, OUTPUT);
pinMode(pedGreen, OUTPUT);
pinMode(button, INPUT);
pinMode(EastRed,OUTPUT);
pinMode(EastYellow, OUTPUT);
pinMode(EastGreen, OUTPUT);
pinMode(SouthRed,OUTPUT);
pinMode(SouthYellow, OUTPUT);
pinMode(SouthGreen, OUTPUT);

// set intial state for lights

digitalWrite(pedRed, HIGH); //start with red ped light on
digitalWrite(EastGreen, HIGH); //start with east green light on
digitalWrite(NorthRed,HIGH); //start with north red light on
digitalWrite(SouthRed,HIGH); //start with south red light on

}

void loop(){
/* check if button is pressed
and it is over 3 sec since last button press*/
int state = digitalRead(button);
if(state==HIGH && (millis() - changeTime) > 3000) {
changeLights(); //function to change lights

}

int lightLevel = analogRead(lightPin); //Read the
// lightlevel
lightLevel = map(lightLevel, 0, 900, 0, 255);
//adjust the value 0 to 900 to
//span 0 to 255

lightLevel = constrain(lightLevel, 0, 255);//make sure the
//value is between
//0 and 255
analogWrite(ledPin, lightLevel); //write the value
}

void changeLights() {

digitalWrite(EastGreen, LOW); //East green light off
digitalWrite(EastRed, LOW); //East red light off
digitalWrite(EastYellow, HIGH); //East yellow light on
delay(2000); //wait 2 seconds
digitalWrite(EastYellow, LOW); //East yellow light off
digitalWrite(EastGreen, LOW); //East green light off
digitalWrite(NorthRed, LOW); //North red light off
digitalWrite(SouthRed, LOW); //South red light off
digitalWrite(EastYellow, LOW); //East yellow light off
digitalWrite(NorthGreen, HIGH); //North green light on
digitalWrite(SouthGreen, HIGH); //South green light on
digitalWrite(EastGreen, LOW); //East green light off
digitalWrite(EastRed, HIGH); //East red light on
digitalWrite(NorthGreen,LOW); //North green light off
digitalWrite(SouthGreen,LOW); //South green light off
digitalWrite(NorthRed, HIGH); //North red light on
digitalWrite(SouthRed,HIGH); //South red light on
digitalWrite(EastRed, LOW); //East red light off
digitalWrite(EastYellow, HIGH); // East yellow light on
delay(2000); //wait 2 seconds

digitalWrite(EastYellow,LOW); // East yellow light off
digitalWrite(NorthYellow,LOW); //North yellow light off
digitalWrite(SouthYellow,LOW); //South yellow light off
digitalWrite(NorthRed,LOW); //North red light off
digitalWrite(SouthRed,LOW); //South red light off
digitalWrite(EastGreen, LOW); //East green light off
digitalWrite(EastYellow, LOW); // East yellow light off
digitalWrite(EastRed, HIGH); //East red light on
digitalWrite(pedRed,HIGH); //ped red light on
digitalWrite(pedGreen,LOW); //ped green light on
digitalWrite(NorthRed, HIGH); //North red light on
digitalWrite(SouthRed, HIGH); //South red light on
digitalWrite(NorthRed, LOW); //North red light off
digitalWrite(SouthRed, LOW); //South red light off
digitalWrite(SouthGreen,HIGH); //South green light on
digitalWrite(NorthGreen,HIGH); //North green light on
delay(500); //wait 0.5 seconds to turn on ped light

digitalWrite(pedRed,LOW); //red ped light off
digitalWrite(pedGreen,HIGH); //green ped light on. allow crossing
delay(crossTime); //delay preset time of 3 seconds
//flashing of ped green light
for (int x=0; x<10; x++){
digitalWrite(pedGreen,HIGH); //ped green light on
delay(250); //wait 0.25 seconds
digitalWrite(pedGreen,LOW); //ped green light off
delay(250); //wait 0.25 seconds
}

digitalWrite(pedGreen, LOW); //green ped light off
delay(100); //wait 0.1 seconds
digitalWrite(NorthGreen, LOW); //North green light off
digitalWrite(SouthGreen, LOW); //South green light off
digitalWrite(SouthYellow, HIGH); //South yellow light on
digitalWrite(NorthYellow, HIGH); //North yellow light on
delay(1000); //wait 1 second
digitalWrite(NorthYellow, LOW); //North yellow light off
digitalWrite(SouthYellow, LOW); //South yellow light off
digitalWrite(pedRed, HIGH); //ped red light on
digitalWrite(NorthGreen,LOW); //North green light off
digitalWrite(SouthGreen,LOW); //South green light off
digitalWrite(EastRed, LOW); //East red light off
digitalWrite(NorthRed,HIGH); //North red light off
digitalWrite(SouthRed,HIGH); //South red light off
digitalWrite(pedGreen,LOW); //ped green light off
digitalWrite(EastGreen, HIGH); //East green light on

changeTime = millis(); //record time since last lights change

}

I guess the green light is "ledPin"?
Write a simple program that just reads the sensor and writes ledPin in a loop. Be sure to wait the required time between analogRead( )s.
Take any leds off the digital 0 & 1 and use Serial to send your sensor data to the monitor. That should give you some idea.
Are you sure you have the sensor connected correctly?

Yes im sure because when I used the CIRC 09 photo resistor code from the arduino tutorial booklet the photo resistor works.

//PhotoResistor Pin
int lightPin = 0; //the analog pin the photoresistor is
//connected to
//the photoresistor is not calibrated to any units so
//this is simply a raw sensor value (relative light)
//LED Pin
int ledPin = 1; //the pin the LED is connected to
//we are controlling brightness so
//we use one of the PWM (pulse width
// modulation pins)
void setup()
{
pinMode(ledPin, OUTPUT); //sets the led pin to output
}
/*

  • loop() - this function will start after setup
  • finishes and then repeat
    */
    void loop()
    {
    int lightLevel = analogRead(lightPin); //Read the
    // lightlevel
    lightLevel = map(lightLevel, 0, 900, 0, 255);
    //adjust the value 0 to 900 to
    //span 0 to 255

lightLevel = constrain(lightLevel, 0, 255);//make sure the
//value is between
//0 and 255
analogWrite(ledPin, lightLevel); //write the value
}

Comment out every mention of southGreen and see what happens.

Ok I will try that tomorrow thanks for all the help! I really do appreciate it!