So, here we are, the last lesson before the final project.
An output device is the opposite of a sensor; it converts back the electrical signal to a physical motion.
There are many types of output devices: LEDs, LCDs, speakers, motors and many more.
In this lesson I will control an RGB LED using the measurements we had from the ultrasonic sensor.
An RGB LED is an LED that consists of three colors, Red, Green and Blue. It can show any of these three colors or any other color that exists, you only need to control the percentage of each color in the mix. Here is the pinout of such an LED:
I have to mention that there are two types of RGB LEDs:
Here I will be using the common cathod type, it is more common.
In this circuit I am using the setup from the previous lesson plus adding the four pins of the RGB LED.
Again, here in the beginning I am declaring the variables for the pins and the measurements.
I would like to remind you to always comment your code, that way it is even self explainatory. It is very helpful more than you'd ever thought.
In the void setup section I am declaring the pins modes, all getting a high voltage except the Echo pin.
In this section of the void loop I am sending a pulse and then listening to it, this is how the ultrasonic sensor works. Then I am doing some calculations and reading them through the serial monitor.
In this piece of code I am controlling which color would the light have depending on the distance from the sensor. This control is divided into three seciotns:
Here is the control of the RGB LEDt based on the distance from the sensor