Hi there,
I wonder if anyone will be able to help me with a problem I'm having. I'm relatively new to using an Arduino and its programming environment and I've come face-to-face with an issue that I can't seem to find a resolution for.
Essentially, I'm doing a project involving two seven-segment displays and a VL53L0X range sensor. My goal is to be able to take a reading from the sensor when a button is pressed - this much I've achieved. I then want to output to both seven-segment displays the reading taken in cm.
I've succeeded in taking a reading with the press of the button and outputting these readings to the serial monitor. I'm able to multiplex the displays to show values with two digits...however I haven't been able to keep these values visible on the screens by using some kind of loop within my program.
I've been able to make it work for one value only (i.e. when the sensor reads 20mm) where a loop has been entered and this number appears across both displays - the only issue is I cannot seem to break out of this loop once I've entered it!
I've tried many different things; do-while loops inside a switch statement, individual while loops, breaks, nothing seems to be working!
I've attached my working code below...this is the bare-bones of what I have. This program takes a reading from the sensor when a button is pressed, converts it from mm to cm, and outputs that to the serial monitor as expected.
Any guidance will be greatly appreciated!
Thank you
#include <Wire.h>
#include <VL53L0X.h>
VL53L0X sensor;
int distanceReading;
int button = 12;
int timer = 500;
void setup()
{
pinMode(button, INPUT);
pinMode(2, OUTPUT); //output A
pinMode(3, OUTPUT); //output B
pinMode(4, OUTPUT); //output C
pinMode(5, OUTPUT); //output D
pinMode(6, OUTPUT); //output E
pinMode(7, OUTPUT); //output F
pinMode(8, OUTPUT); //output G
pinMode(9, OUTPUT); //output display 1
pinMode(11, OUTPUT); //output display 2
Serial.begin(9600);
Wire.begin();
sensor.setTimeout(500);
if (!sensor.init())
{
Serial.println("Failed to detect and initialize sensor!");
while (1) {}
}
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
}
void loop()
{
if (digitalRead(button) == HIGH)
{
distanceReading = sensor.readRangeSingleMillimeters();
if (distanceReading >= 500)
{
Serial.println("Object is too far, please move closer");
delay(200);
}
else
{
int distance_cm = distanceReading / 10;
Serial.print(distance_cm); Serial.println("cm");
delay(200);
}
}
}
test_project.ino (1.67 KB)