LED traffic light system with 3 different modes

can anyone help??
I have a traffic light project I need to do, I need 3 modes, regular mode, pedestrian mode and night mode. I am doing this project on tinkercad, and I'm currently stuck on pedestrian mode as I need to use an ultrasonic range/distance finder. Can anyone help me fix this code so that regular mode runs first and then pedestrian mode, by using a mode system. Can anyone help or see where I have gone wrong!??
The code I have at the moment is:
}

void loop(){
changeLights();
delay(2000);
{

distanceThreshold = 350;

cm = 0.01723 * readUltrasonicDistance(7, 7);
Serial.print(cm);
Serial.print("cm \n");
delay(100);
}
}
void changeLights(){
// turn off yellow and green, then turn red on for 2 seconds
digitalWrite(yellow, LOW);
digitalWrite(green, LOW);
digitalWrite(red, HIGH);
delay(2000);
// turn off red and green, then turn yellow on for 1 second
digitalWrite(yellow, HIGH);
digitalWrite(green, LOW);
digitalWrite(red, LOW);
delay(1000);
// turn off red and yellow, then turn on green for 2 seconds
digitalWrite(yellow, LOW);
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
delay(2000);
// turn green and red off, then turn yellow on for 1 second
digitalWrite(yellow, HIGH);
digitalWrite(green, LOW);
digitalWrite(red, LOW);
delay(1000);
}

float distanceThreshold = 0;

float cm = 0;

long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT);
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);

digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);

return pulseIn(echoPin, HIGH);
}

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

...missing a setup function and probably a bunch of declarations.
(And code tabs)
(Do you really have the echo and trigger tied to the same pin?)

how do i go about that and put that in my work?

i have been using code from online and in class, it's all mixed

Do you mean "muddled" or "confused"?

both, more confused than anything as ive tried to get it to work with different solutions and its just confused me even more now.

So, do we get to see the rest of the code?

You're only measuring the range every eight seconds or so.
Is that planned?

all code above is what i have at the moment:
this is all of it:
</>
int red = 10;
int yellow = 9;
int green = 8;

void setup(){
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
Serial.begin(9600);
}

void loop(){
changeLights();
delay(2000);
{

distanceThreshold = 350;

cm = 0.01723 * readUltrasonicDistance(7, 7);
Serial.print(cm);
Serial.print("cm \n");
delay(100);
}
}
void changeLights(){
// turn off yellow and green, then turn red on for 2 seconds
digitalWrite(yellow, LOW);
digitalWrite(green, LOW);
digitalWrite(red, HIGH);
delay(2000);
// turn off red and green, then turn yellow on for 1 second
digitalWrite(yellow, HIGH);
digitalWrite(green, LOW);
digitalWrite(red, LOW);
delay(1000);
// turn off red and yellow, then turn on green for 2 seconds
digitalWrite(yellow, LOW);
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
delay(2000);
// turn green and red off, then turn yellow on for 1 second
digitalWrite(yellow, HIGH);
digitalWrite(green, LOW);
digitalWrite(red, LOW);
delay(1000);
}

float distanceThreshold = 0;

float cm = 0;

long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT);
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);

digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);

return pulseIn(echoPin, HIGH);
}
</>

The auto format tool will make the structure of your code easier to follow.

Code tags will make it easier to read.

this is the scenario:

Pedestrian Mode: - This mode uses the SRF05 Ultrasonic Range Finder to
detect the proximity of any object. The lights should operate as defined in
the regular mode UNTIL an object is detected less than 10 cm from the
TLC. The buzzer should then beep and the Yellow LED must blink ON/OFF
(flashing intervals of 0.1 sec) repeatedly 10 times. The buzzer should then
stop beeping and the operation of the lights (not the system) must behave
as it would in the regular mode. The operation stays the same until the
mode changes.

yeah it has alot of errors and mistakes within the code

ive tried to add them, however i am new to this forum.

Ok.
Put that code aside, and concentrate on getting reliable range measurements.
Should take no more than about 20-30 lines of code

how do i go about that?im completely new to arduino

How do you start anything new?
You hit the library.

Google also works well with simple search terms.

ok thankyou.

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