I am currently doing part 2: Pedestrian Mode of my project below:
- Regular Mode: This is the regular mode of operation. In this mode the three lights (Red, Yellow and Green LEDs) should operate in the following order. The Red LED lights for 2 seconds, followed by the Yellow LED for 1 second, the Green LED for 2 seconds and then the Yellow LED again for 1 second. The cycle should repeat until the mode changes.
- 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
My code works, however it waits for the entire regular cycle to finish before sensing a pulse with the ultrasonic range sensor to determine if an object is near or not. Therefore, when I put my hand near the sensor, it has to finish running the regular mode before it detects my hand and switches to pedestrian mode. What I would like is for the range finder to continuously fire the pulse and immediately switch to pedestrian mode when it detects an object less than 10 cm away.
Code below:
int red_light = 12;
int yellow_light = 10;
int green_light = 8;
int ECHOPIN = 3;
int TRIGPIN = 5;
int buzzer = 7;
float distance;
void setup() {
Serial.begin(9600);
pinMode(red_light, OUTPUT);
pinMode(yellow_light, OUTPUT);
pinMode(green_light, OUTPUT);
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop() {
range_finder();
if (distance < 10) {
pedestrian_mode();
} else {
regular_mode();
}
}
void regular_mode() {
digitalWrite(red_light, HIGH);
delay(2000);
digitalWrite(red_light, LOW);
digitalWrite(yellow_light, HIGH);
delay(1000);
digitalWrite(yellow_light, LOW);
digitalWrite(green_light, HIGH);
delay(2000);
digitalWrite(green_light, LOW);
digitalWrite(yellow_light, HIGH);
delay(1000);
digitalWrite(yellow_light, LOW);
}
void range_finder() {
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN,HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN,LOW);
distance = pulseIn(ECHOPIN, HIGH);
// return 0 if no high pulse in 1 sec
distance = distance/58;
Serial.print(distance);
Serial.println("cm");
delay(200);
}
void pedestrian_mode() {
digitalWrite(buzzer, HIGH);
digitalWrite(yellow_light, HIGH);
delay(100);
digitalWrite(yellow_light, LOW);
delay(100);
digitalWrite(yellow_light, HIGH);
delay(100);
digitalWrite(yellow_light, LOW);
delay(100);
digitalWrite(yellow_light, HIGH);
delay(100);
digitalWrite(yellow_light, LOW);
delay(100);
digitalWrite(yellow_light, HIGH);
delay(100);
digitalWrite(yellow_light, LOW);
delay(100);
digitalWrite(yellow_light, HIGH);
delay(100);
digitalWrite(yellow_light, LOW);
delay(100);
digitalWrite(yellow_light, HIGH);
delay(100);
digitalWrite(yellow_light, LOW);
delay(100);
digitalWrite(yellow_light, HIGH);
delay(100);
digitalWrite(yellow_light, LOW);
delay(100);
digitalWrite(yellow_light, HIGH);
delay(100);
digitalWrite(yellow_light, LOW);
delay(100);
digitalWrite(yellow_light, HIGH);
delay(100);
digitalWrite(yellow_light, LOW);
delay(100);
digitalWrite(yellow_light, HIGH);
delay(100);
digitalWrite(yellow_light, LOW);
delay(100);
digitalWrite(yellow_light, HIGH);
delay(100);
digitalWrite(yellow_light, LOW);
delay(100);
digitalWrite(yellow_light, HIGH);
delay(100);
digitalWrite(yellow_light, LOW);
delay(100);
}
Also I have a feeling some parts of my code may be unnecessarily long and could be shortened. If so, I would appreciate any advice and how to do that. Thank you.