Daniel Shin – Assignment 6: Arduino Introduction

This week, we learned how to use Arduino and programming to produce the desired output, depending on the input.

 

Motive

Within the default kit, there was an ultrasonic device that calculates the distance to an obstacle. When I saw this device, I remembered how these devices were used in cars to let the driver know how far they are from an obstacle. My mother used to drive a vehicle without a device like this. She used to tell me that installing such a device was too expensive. I realized that I should be able to develop a device similar to that using Arduino.

 

Device setup

I used a total of 3 devices. One input device (ultrasonic) and two output devices (buzzer, LED). The ultrasonic device would detect the distance from the obstacle, LED would be a visual indicator of the distance, and the buzzer would be a sound indicator of the distance. For the ultrasonic device, I had to connect to 4 ports: power, ground, trigger, and echo. For the output devices, I just had to connect one port to a designated number and ground. 

 

Code

I have found a tutorial online that shares a code on how to setup using the ultrasonic device. Once I followed the code, the serial monitor would display the distance from the ultrasonic device in such manner: 50 if nothing is nearby, jumps to 5 if something comes within the detection range, decreases to 1 based on the distance within the range. To trigger the output devices when an obstacle is within range, I had to write code as such:

if (distance <= 5)

{

digitalWrite(buzzer, HIGH);

digitalWrite(led, HIGH);

}

else

{

digitalWrite(buzzer, LOW);

digitalWrite(led, LOW);

}

The problem with this code is that it only turns the buzzer and led on whenever the obstacle gets in range without telling how far the obstacle is within the range. To solve this problem, I had to write a code to make the buzzer/LED blink/beep based on the distance from the ultrasonic device. I needed to use the “delay” code. Since the distance within range is indicated between 5 to 1, the following code will set the delay longer or shorter depending on the distance:

delay(distance*100);

The following line will delay for 0.5 seconds if the obstacle is “5” away from the ultrasonic and 0.1 seconds if the obstacle is “1” away from the ultrasonic. In other words, the beep/buzz will be faster when the obstacle is closer to the device. Since the beep/buzz includes a turning off condition, I won’t need the else statement anymore. Here is the final code:

if (distance <= 5)

{

  digitalWrite(buzzer, HIGH);

  digitalWrite(led, HIGH);

  delay(distance*100);

  digitalWrite(buzzer, LOW);

  digitalWrite(led, LOW);

  delay(distance*100);

}

 

Video

Conclusion

Overall, the device and the code functioned as expected. I had some trouble setting up specific devices since that was not covered in class, but I was able to figure it out by using tutorials online. The coding part was entertaining and was not too difficult because I had some previous experience with coding. Only having to use if/else statements were simple enough not to trigger the nightmares I had while taking a CS course in this school.

 

Facebook
Twitter
LinkedIn
Pinterest