1

enter image description here

/*
MODIFIED TRAFFIC LIGHT SYSTEM W/ A COUNTDOWN TIMER FUNCTION
BY JOHN SEONG

THIS IS A QUITE ACCURATE SIMULATION OF TORONTO'S TRAFFIC LIGHT SYSTEM
WHICH INCLUDES TWO 7-SEGMENT DISPLAYS THAT SHOW THE SECONDS LEFT
TILL THE PEDESTRIAN LIGHT SWITCHES BACK TO RED.

ENJOY!
*/

const int redLED = 4;
const int yellowLED = 5;
const int greenLED = 6;
const int pedestrianRedLED = 13;
const int pedestrianGreenLED = A5;
const int buzzer = 12;
const int button = 7;

int segmentPins[] = {11, 10, 9, 8};

int buttonHitCount = 0;

void setup() {
  
  Serial.begin(9600);
  
  // Car Traffic LEDs
  pinMode(redLED, OUTPUT);
  pinMode(yellowLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
  
  // Pedestrian Traffic LEDs
  pinMode(pedestrianRedLED, OUTPUT);
  pinMode(pedestrianGreenLED, OUTPUT);
  
  // 7-Segment Display Pins
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  
  // Piezo Speaker Pin
  pinMode(buzzer, OUTPUT);
  
  // Pedestrian Push Button
  pinMode(button, INPUT_PULLUP);
  
  displayNumber(9, 2);
  displayNumber(9, 3);
}

void loop() {
  
  // Read the state of the button
  int buttonState = digitalRead(button);
  
  Serial.println(buttonState);
  
  if (buttonState == LOW) {
    buttonHitCount = 1;
  }
  
  if (buttonHitCount == 1) {
    switchToRed();
    
  } else if (buttonHitCount == 0) {
    defaultState();
  }
}

void displayNumber(int num, int displayPin) {
  
  bool binaryValue[][4] = {
    {0, 0, 0, 0}, // 0
    {0, 0, 0, 1}, // 1
    {0, 0, 1, 0}, // 2
    {0, 0, 1, 1}, // 3
    {0, 1, 0, 0}, // 4
    {0, 1, 0, 1}, // 5
    {0, 1, 1, 0}, // 6
    {0, 1, 1, 1}, // 7
    {1, 0, 0, 0}, // 8
    {1, 0, 0, 1}, // 9
  };
  
  for(int i = 0; i < 4; i++) {
    digitalWrite(segmentPins[i], binaryValue[num][i]);
  }
  
  digitalWrite(displayPin, HIGH);
  delayMicroseconds(10);
  
  digitalWrite(displayPin, LOW);
}

void switchToRed() {
  
  delay(5000);
  
  // YELLOW
  digitalWrite(yellowLED, HIGH);
  digitalWrite(redLED, LOW);
  digitalWrite(greenLED, LOW);
  delay(2000);
  
  // RED
  digitalWrite(redLED, HIGH);
  digitalWrite(greenLED, LOW);
  digitalWrite(yellowLED, LOW);
  
  // GREEN
  digitalWrite(pedestrianGreenLED, HIGH);
  digitalWrite(pedestrianRedLED, LOW);
  
  switchToGreen();
}

void switchToGreen() {
  
  // 15
  noTone(buzzer);
  displayNumber(1, 2);
  displayNumber(5, 3);
  tone(buzzer, 1000);
  delay(1000);
  
  // 14
  noTone(buzzer);
  displayNumber(1, 2);
  displayNumber(4, 3);
  tone(buzzer, 1000);
  delay(1000);
  
  // 13
  noTone(buzzer);
  displayNumber(1, 2);
  displayNumber(3, 3);
  tone(buzzer, 1000);
  delay(1000);
  
  // 12
  noTone(buzzer);
  displayNumber(1, 2);
  displayNumber(2, 3);
  tone(buzzer, 1000);
  delay(1000);
  
  // 11
  noTone(buzzer);
  displayNumber(1, 2);
  displayNumber(1, 3);
  tone(buzzer, 1000);
  delay(1000);
  
  // 10
  noTone(buzzer);
  displayNumber(1, 2);
  displayNumber(0, 3);
  tone(buzzer, 1000);
  delay(1000);
  
  // 9
  noTone(buzzer);
  displayNumber(0, 2);
  displayNumber(9, 3);
  tone(buzzer, 1000);
  delay(1000);
  
  // 8
  noTone(buzzer);
  displayNumber(0, 2);
  displayNumber(8, 3);
  tone(buzzer, 1000);
  delay(1000);
  
  // 7
  noTone(buzzer);
  displayNumber(0, 2);
  displayNumber(7, 3);
  tone(buzzer, 1000);
  delay(1000);
  
  // 6
  noTone(buzzer);
  displayNumber(0, 2);
  displayNumber(6, 3);
  tone(buzzer, 1000);
  delay(1000);
  
  // 5
  noTone(buzzer);
  displayNumber(0, 2);
  displayNumber(5, 3);
  tone(buzzer, 1000);
  delay(1000);
  
  // 4
  noTone(buzzer);
  displayNumber(0, 2);
  displayNumber(4, 3);
  tone(buzzer, 1000);
  delay(1000);
  
  // 3
  noTone(buzzer);
  displayNumber(0, 2);
  displayNumber(3, 3);
  tone(buzzer, 1000);
  delay(1000);
  
  // 2
  noTone(buzzer);
  displayNumber(0, 2);
  displayNumber(2, 3);
  tone(buzzer, 1000);
  delay(1000);
  
  // 1
  noTone(buzzer);
  displayNumber(0, 2);
  displayNumber(1, 3);
  tone(buzzer, 1000);
  delay(1000);
  
  noTone(buzzer);
  
  // YELLOW
  digitalWrite(yellowLED, HIGH);
  digitalWrite(redLED, LOW);
  digitalWrite(greenLED, LOW);
  delay(2000);
  
  // GREEN
  digitalWrite(redLED, LOW);
  digitalWrite(greenLED, HIGH);
  digitalWrite(yellowLED, LOW);
  
  // PEDESTRIAN - RED
  digitalWrite(pedestrianGreenLED, LOW);
  digitalWrite(pedestrianRedLED, HIGH);
  
  // Reset the value
  buttonHitCount = 0;
}

void defaultState() {
    
    // GREEN
    digitalWrite(greenLED, HIGH);
    digitalWrite(yellowLED, LOW);
    digitalWrite(redLED, LOW);
    
    // PEDESTRIAN - RED
    digitalWrite(pedestrianRedLED, HIGH);
    digitalWrite(pedestrianGreenLED, LOW);
}

Clearly something is wrong with my code and the 7-segment displays are off the whole time the program is running. I also posted a link below which directs to the tinkercad project I made.

https://www.tinkercad.com/things/7pnnbXoaqrG-improved-stop-light-john-seong/editel?sharecode=jR86i_srV7QgP11u1CxOFzeVwehzjIRr8wCC7vbbLaY

8
  • 1
    Please try to color code the wires more. With nearly all wires being green and overlapping at many places it is really difficult to follow the wiring diagram. Also you have some more components besides the 7-segment displays, that aren't really relevant here. It is a common debug method to remove all code and components, that aren't relevant to a specific problem, then getting the relevant problem solved and then add the rest of the code and components one by one, always checking for errors inbetween. You could try building the circuit for only the displays and check again Commented Jan 21, 2021 at 11:12
  • I debugged; which turns out, all the connections are right however when I press the button the segment displays turn off. Commented Jan 21, 2021 at 11:43
  • 1
    You are never touching the segment display anywhere in your loop() function. So if does something (like going off) but none of the LEDs act as intended (i.e. switching from green to red), then something in your wiring must be wrong or the pin numbers in the code don't match. Commented Jan 21, 2021 at 12:02
  • It's supposed to multiplex^ Commented Jan 21, 2021 at 12:48
  • 1
    You could make it easier for anyone trying to help you by structuring the wires in a more logical manner (for example don't let wires of the same color overlap) and maybe enlarging the schematic (so that pin numbers are readable for example). It's very hard to understand at the moment. Commented Jan 21, 2021 at 15:00

1 Answer 1

0

Not really a solution, but changing the code for the displayNumber function as shown below, will make it more apparent where the real problem is.

void displayNumber(int num, int displayPin) {
  
  bool binaryValue[][4] = {
    {0, 0, 0, 0}, // 0
    {0, 0, 0, 1}, // 1
    {0, 0, 1, 0}, // 2
    {0, 0, 1, 1}, // 3
    {0, 1, 0, 0}, // 4
    {0, 1, 0, 1}, // 5
    {0, 1, 1, 0}, // 6
    {0, 1, 1, 1}, // 7
    {1, 0, 0, 0}, // 8
    {1, 0, 0, 1}, // 9
  };

  // turn off both displays
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);

  for(int i = 0; i < 4; i++) {
    digitalWrite(segmentPins[i], binaryValue[num][i]);
  }
  
  digitalWrite(displayPin, HIGH);
  /* // remove these two lines
  delayMicroseconds(10);
  digitalWrite(displayPin, LOW);
  */
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.