Traffic sequence that changes

WIN_20240121_08_59_32_Pro
WIN_20240121_08_59_40_Pro

that is my circuit, but the code keeps on getting stuck when it turns on the green light. I want it to use the button to change priorities, 1 mode to turn green when the ultrasonic sensor reads something close enough, and the other time based.
here is my code.

#include <SR04.h>
#include <LiquidCrystal.h>

#define TRIG_PIN 48
#define ECHO_PIN 49

const byte BUTTON = 50;
const byte redLED = 44;
const byte yellowLED = 45;
const byte greenLED = 46;

bool Bool_mode = false; 
bool Bool_greenProcessStart = false;
bool Bool_yellowProcessStart = false;
bool trafficStart = true;

unsigned int photoValue;
long ultrasonicInput;
int ultrasonicInterval = 500;
unsigned long ultrasonicPastTime = 0;
int trafficLightProccesRestartInterval = 60000;
unsigned long trafficLightProccesRestartPastTime = 0;
unsigned long greenPastTime = 0;
int greenInterval = 15000;
unsigned long yellowPastTime = 0;
int yellowInterval = 40000;

LiquidCrystal lcd(12, 11, 10, 9, 8, 7, 6);
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);

void setup() {
  delay(1000);
  pinMode(BUTTON, INPUT);
  lcd.home();
  lcd.print("start");
  Serial.begin(9600);
}

void loop() {
  unsigned long currentTime = millis();
  if (digitalRead(BUTTON) == HIGH) {
    delay(100);
    if (Bool_mode == true) {
      Bool_mode = false;
    } else {
      Bool_mode = true;
    }
  }  
  if (currentTime-ultrasonicPastTime >= ultrasonicInterval) {
    ultrasonicPastTime = currentTime;
    ultrasonicInput = sr04.Distance();
    Serial.println(ultrasonicInput);
  }
  if (Bool_mode == false) {
    if (currentTime-trafficLightProccesRestartPastTime >= trafficLightProccesRestartInterval || trafficStart == true) {
      trafficStart = false;
      trafficLightProccesRestartPastTime = currentTime;
      Serial.println("reset");
      digitalWrite(redLED, LOW);
      digitalWrite(yellowLED, LOW);
      digitalWrite(greenLED, LOW);
      digitalWrite(redLED, HIGH);
      Bool_greenProcessStart = true;
      Serial.println("Bool_greenProcessStart = " + Bool_greenProcessStart);
      greenPastTime = currentTime;
      lcd.clear();
      lcd.home();
      lcd.print("reset");
  }
    if (Bool_greenProcessStart == true && currentTime -greenPastTime >= greenInterval) {
      Serial.print("green");
      digitalWrite(redLED, LOW);
      digitalWrite(greenLED, HIGH);
      yellowPastTime = currentTime;
      Bool_greenProcessStart = false;
      Serial.println("Bool_greenProcessStart = " + Bool_greenProcessStart);
      Bool_yellowProcessStart = true;
      Serial.println("Bool_yellowProcessStart = " + Bool_yellowProcessStart);
      yellowPastTime = currentTime;
      lcd.clear();
      lcd.home();
      lcd.print("greenOn");
  }
    if (Bool_yellowProcessStart == true && currentTime - yellowPastTime >= yellowInterval) {
      Serial.println("yellow");
      digitalWrite(greenLED, LOW);
      digitalWrite(yellowLED, HIGH);
      Bool_yellowProcessStart = false;
      Serial.println("Bool_yellowProcessStart = " + Bool_yellowProcessStart);
      lcd.clear();
      lcd.home();
      lcd.print("yellowOn");
    }
  }
  
  
  
}

how do I fix this?
WIN_20240121_08_59_26_Pro

that constant doesn't fit into that sized variable. An int on a Mega is -32678..32767

ALL you time based variables should be unsigned long

Is this for a class, if so which one. The pictures do not do much good. The best thing you can do is post an annotated schematic showing exactly how you have wired it, be sure to include all power, ground connections and power sources. Links to hardware items that give technical information would also help. LEDs etc we understand.

here is the schmatic

ok thanks, i did it.

You did a great job. You are missing some annotation such as LED color and pot value. That schematic just saved a page of explaining what is happening. The LEDs should each have its own 330 Ohm resistor. Without that they will behave weirdly and the green and maybe yellow will be very dim or not light. To my knowledge the Mega does not support pull down resistors. You can use the pull up resistor on D50 and connect the switch to ground. The input will then work but it will be inverted so a 1 = off and 0 = on. Look at the not operator for a hint on how to read it. IE if pin is not on then the signal is true. Good work, keep on going.

the left most LED is red, middle yellow, right is green. the pot is up to 10k. yet to test solutions

That is the Fun part! Do not forget to add resistors for each LED unless only one is going to be on at any given time.

yeah, I'm trying the solutions. I think it'll work though

it worked... but red and yellow turn on at the same time now.

1 Like

working fine now! thanks guys

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.