22
6. If the distance is different from the previous distance, clear OLED and displays the new distance
on OLED.
7. If the distance is the same as the previous distance, don’t need to upgrade the display on OLED.
Code usage
Import the libraries for using the ultrasonic ranging sensor and OLED. “Wire.h” is the library of I2C
devices, we need to import this library to tell Arduino IDE before we use I2C devices. "Ultrasonic.h" is
a library of ultrasonic ranging sensors. With the built-in method of this library, we no longer need to
perform complex distance calculations to obtain the distance. “U8glib.h” is the library of OLED.
Import libraries: #include <Wire.h> #include <Ultrasonic.h> #include "U8glib.h"
Create an ultrasonic instance object so we can use the function of the ultrasonic library to get the
distance. Inside the parentheses, there are two parameters we need to pass. The first one is the pin
of trig and the second one is the pin of echo.
Create ultrasonic instance: Ultrasonic ultrasonic(2,3);
Char is short for Character, which is to declare that we want to create character-related.“distance_
value_temp[5] = {0}” means that we are creating an array that can contain 5 elements. Since we used
char to declare it, it is 5 characters. Finally, we assign it a value of 0.
Character array: char distance_value_temp[5] = {0};
Since we need to display the distance value on the OLED, the OLED can only display strings, but the
distance value is an integer variable, so we have to convert it to a string to display on the OLED.
“value”: the data to be converted; “string”: the address of the target string; “radix”: the converted
hexadecimal number, which can be decimal, hexadecimal, etc.
Convert numbers to strings: itoa( int value, char *string,int radix);
"Ultrasonic.Ranging ()" is the function used to obtain the ultrasonic distance. You need to fill in the
brackets in which units you want to get the distance value. Optional parameters are CM (centimeter),
INC (feet). After getting the distance, we assign the distance to the variable "Distance".
Get distance: Distance=ultrasonic.Ranging(CM);
Crowtail- Thumb Joystick is a Crowtail compatible module that is very similar to the 'analog' joystick
on PS2 (PlayStation 2) controllers. Two direction movements will output different analog signals as
Lesson 10 – Use of the joystick
Introduction
Summary of Contents for Crowtail Deluxe Kit
Page 47: ......