Week 9 Assignment: Crossroad Warning Sensors

Concept:

In this week’s assignment, I have utilized the analog output features. From all the knowledge that I have gained from the classes, I have decided to create a prototype that shows drivers how far they are from the crossroad walking area in front of the signals. I decided to pursue this assignment because I noticed that there are a lot of people who get way too close to the crossroad walking which will interfere with the individual’s path and can be dangerous in some instances. The way the prototype warns the drivers is by portraying an LED output under the signal for each lane, if the driver is at a good distance, it shows green and the closer they the color fades into yellow, and finally into the red region where if the driver gets too close to the crossroad, it will illuminate a bright red LED showing that they shouldn’t go anywhere after that point. If the driver goes into the red region, there’s an option for the pedestrians to click a button on the pole to warn the driver to back off by illuminating the red light.

Prototype:

I used the ultrasonic sensor to detect the distance, and a common cathode LED to create green, red, and yellow colors in one LED. For the pedestrian warning button, I used a push button that connects the circuit to an external LED which will switch on with each push of a button.

Code:

const int trigPin = 9;
const int echoPin = 10;
const int bluePin = 6;
const int greenPin = 5;
const int redPin = 3;

// Declare variables for LED colors
int redValue = 0;
int greenValue = 0;
int blueValue = 0;

void setup() {
//initiate pins as inputs and outputs depending on its application
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop() {
//sends a short pulse of ultrasonic sensor and waits a bit then receives it
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

//Time it takes for the pulse to travel back from the object
  long duration = pulseIn(echoPin, HIGH);
//Universal conversion of time into distance in cm
  int distance = duration * 0.034 / 2;
  int brightness = 0;

//If conditions to produce light at specific distances
 if (distance < 10) {
  brightness = map(distance, 3, 10, 255, 0);
  //RED COLOR
  analogWrite(redPin, brightness);
  analogWrite(greenPin, 0);
  analogWrite(bluePin, 0);

} else if (distance >= 10 && distance < 20) {
  brightness = map(distance, 10, 20, 0, 255);
  //YELLOW COLOR
  analogWrite(redPin, brightness);
  analogWrite(greenPin, brightness);
  analogWrite(bluePin, 0);

} else if (distance >= 20 && distance <= 50) {
  brightness = map(distance, 20, 50, 0, 255);
  //GREEN COLOR
  analogWrite(redPin, 0);
  analogWrite(greenPin, brightness);
  analogWrite(bluePin, 0);

} else {
  // Default color
  setColor(0, 0, 0);  
}

//output codes to know what distance I am at as well as the proportionate RGB color
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.print(" cm | RGB Color: ");
  Serial.print("R: ");
  Serial.print(redValue);
  Serial.print(" G: ");
  Serial.print(greenValue);
  Serial.print(" B: ");
  Serial.println(blueValue);

  delay(20);
}

Reflection:

Overall, I learned a lot about the use of analog inputs/outputs and how digital pins differentiate from analog ones. Some of the difficulties I have faced were probably more on the code than the setup. Such as the mapping of the distance of the ultrasonic sensor to the LED’s increasing/decreasing brightness. Another issue was the conversion of time into distance, after looking through Google, I found a universal conversion from the time measured in pulse to the distance in centimeters.

 

Leave a Reply