Hello world!
I'm only a 19 years old guy, passionate for electronics and stuff like that, and few days ago, my dad bought a real Proximity Sensor for his car, and then, I try to copy that, but on DIY method hehe.
This thing, can detect distances, and, with leds, says if the object it's near, or far.
If it is close enough, a alarm (buzzer) start to make noise.
OBVIOUSLY, this is not optimized for real cars, it's only for entertainment purposes only.
This is my first project made by me, I don't based my project on anything more than my Arduino Guide.
Now, let's explain the things that I done.
int TRIG = 5;
int ECO = 6;
int LED = 3;
int DURATION;
int DISTANCE;
int LED2 = 2;
int LED3 = 4;
int ALERT = 7;
void setup() {
// put your setup code here, to run once:
pinMode(TRIG, OUTPUT);
pinMode(ECO, INPUT);
pinMode(LED, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(ALERT, OUTPUT);
Serial.begin(9600);
}
The first thing, I declared the initial values for all the project, as the leds and pins. Later, before the void setup, I put the pinModes and if it were in or output. And, a communication with the serial port. I used the serial port, because I wanted to check if the sensor was working. It's not needed, but I do it.
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(TRIG, HIGH);
delay(1);
digitalWrite(TRIG, LOW);
DURATION = pulseIn(ECO, HIGH);
DISTANCE = DURATION / 58.2;
Serial.println(DISTANCE);
delay(200);
After that, in the Void Loop, I put all the mathematical operations to do.
Here, there's something important.
The 58.2, in my manual, says that is a constant given by the manufacturer of the sensor. My manual don't say anything else, so... I don't have anything else to explain xd. The sensor I used, it's the classic sensor, a HC-SR04.
Then, I upload the code to Arduino's memory, and I got something like this:
The number printed on the PC, it's the distance that the sensor it's reading. Nothing else.
Then, the really hard work (For me...), has being started...
if (DISTANCE <= 15 && DISTANCE >= 8 ) {
digitalWrite(LED, HIGH); // Yellow led Middle Risk
}
else {
digitalWrite(LED, LOW);
if (DISTANCE <= 20 && DISTANCE > 15) {
digitalWrite(LED2, HIGH); // Green led Low Risk
}
else {
digitalWrite (LED2, LOW);
if (DISTANCE > 20) {
digitalWrite(LED3, HIGH); //Blue led Out Of Risk
}
else {
digitalWrite (LED3, LOW);
if (DISTANCE < 7 && DISTANCE >= 0) {
digitalWrite (ALERT, HIGH); //Red led HIGH RISK
delay(150);
digitalWrite (ALERT, LOW);
delay(150);
}
else {
digitalWrite (ALERT, LOW);
}
}
}
}
}
On this piece of code, I really take too long to devolp it.
It's not in order, because I was adding leds as crazy, and... The point is that it works... I promise that in the next "tutorial", I will work with order...
However...
The led number 3 (Blue), says that you're not in risk, because the distance is more than 20 centimeters.
The led 2 (Green), says that you're in low risk. This low risk it's between the 20 and the 15.
The led zero (Or only called as LED (Yellow)), says that you're in middle risk. This middle risk it's between the 15 and the 8.
The led ALERT (Red), says that you're too close to the object, less than 7 centimeters.
I try to add a buzzer apart, but, it doesn't works... So, I add it on the led ALERT. You're going to see it in the protoboard image...
The last led, flashes at the same time that the buzzer make noise.
After all that, I compile it (correcting thousands of HORRORS (Like the classic " ; error "))
And then, it worked, just I as expected.
I think that it's good for my first try in Arduino.
Here's the entire code...
int TRIG = 5;
int ECO = 6;
int LED = 3;
int DURATION;
int DISTANCE;
int LED2 = 2;
int LED3 = 4;
int ALERT = 7;
void setup() {
// put your setup code here, to run once:
pinMode(TRIG, OUTPUT);
pinMode(ECO, INPUT);
pinMode(LED, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(ALERT, OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(TRIG, HIGH);
delay(1);
digitalWrite(TRIG, LOW);
DURATION = pulseIn(ECO, HIGH);
DISTANCE = DURATION / 58.2;
Serial.println(DISTANCE);
delay(200);
if (DISTANCE <= 15 && DISTANCE >= 8 ) {
digitalWrite(LED, HIGH); // Yellow led Middle Risk
}
else {
digitalWrite(LED, LOW);
if (DISTANCE <= 20 && DISTANCE > 15) {
digitalWrite(LED2, HIGH); // Green led Low Risk
}
else {
digitalWrite (LED2, LOW);
if (DISTANCE > 20) {
digitalWrite(LED3, HIGH); //Blue led Out Of Risk
}
else {
digitalWrite (LED3, LOW);
if (DISTANCE < 7 && DISTANCE >= 0) {
digitalWrite (ALERT, HIGH); //Red led HIGH RISK
delay(150);
digitalWrite (ALERT, LOW);
delay(150);
}
else {
digitalWrite (ALERT, LOW);
}
}
}
}
}
.:MRX:.
Comments