2

(sharon) #1
FORGE

int height=20;
void drawBar(int x, int y, int w, int h, int value)
{
float posReal;
//needed to calculate the width of the bar to fill
int posInt;
// converts the real width to an integer one
display.drawRect(x, y, w, h);
// draw the outline box
posReal= 1.0valuewidth/maxDistance;
posInt=posReal;
display.fillRect(x, y, posInt, h);
display.drawString(x+w+2,y,String(value));
}
The main control loop takes readings every 0.5
seconds from the sensor using the instruction
sensor.readRangeSingleMillimeters and shows these
on the display, with drawbar, and on the Serial Monitor
with Serial.print.
The axes on the display are as follows:
x goes from 0 – 127 pixels, left to right
y goes from 0 – 63 pixels, top to bottom.
All drawing instructions use this convention. The
image is assembled in memory first and then the
information copied in one move to the display using
the instruction display.display. A do while loop is
used to trap any error codes returned and only display
valid readings.


void loop() {
display.clear();
display.drawString(0,0,”ESP Laser Rangefinder”);
display.drawString(0,18, “Distance in mm”);
do {
reading = sensor.readRangeSingleMillimeters();
} while (reading > 5000);
Serial.print(“Reading / mm - “);
Serial.println(reading);
if (sensor.timeoutOccurred()) { Serial.print(“
TIMEOUT”); }
drawBar(0,34,width,height,reading);
display.display();
delay(500);
}
This project demonstrates how to use three
commonly available modules, which can be used
in many devices. In the photograph, the VL53L0X
IR rangefinder is at the top, the ESP8266 based
NodeMCU board is in the middle and the small OLED
display at the bottom. The sensor module can be
mounted on flying leads for more flexibility, but these
should be kept to less than 30 cm in length to preserve
the correct operation of the I^2 C interface.


The I^2 C serial interface has a data line, SDA, a clock
line (CLK) for timing, and a GND line for voltage
reference. One device on the bus is the Master,
usually the host processor, and the other devices
connected are Slaves, each with their own address.
Data is sent in a standard format, which contains the
address of the Slave and the data being transmitted.
The SDA line is bi-directional, as it can carry
messages from the Master to a Slave and messages
from a Slave to the Master. The CLK line is controlled
by the Master only. The SDA and SCL lines must be
pulled up to a positive supply line with resistors.

WIRING UP I2C


SCL
SDA
GND

VCC

resistorsPull-up

Master

Slave 1 Slave 1 Slave 1

Many devices can
be connected to
the same I^2 C bus
provided they have
different addresses

Left
Our final build.
Now find those
xenomorphs!
Free download pdf