/* This example shows how to get single-shot range
measurements from the VL53L0X. The sensor can optionally be
configured with different ranging profiles, as described in
the VL53L0X API user manual, to get better performance for
a certain application. This code is based on the four
"SingleRanging" examples in the VL53L0X API.
The range readings are in units of mm. */
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <NewPing.h>
#include <Wire.h>
#include <VL53L0X.h>
const int RunningAverageCount = 20;
float RunningAverageBuffer[RunningAverageCount];
int NextRunningAverage;
#define laser 3
#define button 6 // Pushbutton on D3
#define resetPin 7
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
VL53L0X sensor;
int state = 0; // integer to hold current state
int old = 0; // interger to hold last state
int buttonPress = 0; // intereger to hold button state
// Uncomment this line to use long range mode. This
// increases the sensitivity of the sensor and extends its
// potential range, but increases the likelihood of getting
// an inaccurate reading because of reflections from objects
// other than the intended target. It works best in dark
// conditions.
//#define LONG_RANGE
// Uncomment ONE of these two lines to get
// - higher speed at the cost of lower accuracy OR
// - higher accuracy at the cost of lower speed
//#define HIGH_SPEED
#define HIGH_ACCURACY
void setup()
{
Serial.begin(9600);
Wire.begin();
digitalWrite(resetPin, HIGH);
pinMode(resetPin, OUTPUT);
pinMode(laser, OUTPUT);
digitalWrite(laser, LOW);
pinMode(button, INPUT); // button set as INPUT
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32)
sensor.setTimeout(500);
if (!sensor.init())
{
Serial.println("Failed to detect and initialize sensor!");
while (1) {}
}
sensor.setSignalRateLimit(0.1);
// increase laser pulse periods (defaults are 14 and 10 PCLKs)
sensor.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, 18);
sensor.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, 14);
display.setTextSize(3);
display.setTextColor(WHITE);
display.clearDisplay();
display.setCursor(20, 20);
display.print("READY");
display.display();
}
void Average() {
int distance = (sensor.readRangeSingleMillimeters());
Serial.print("distance = ");
Serial.println(distance);
RunningAverageBuffer[NextRunningAverage++] = distance;
if (NextRunningAverage >= RunningAverageCount)
{
NextRunningAverage = 0;
}
float RunningAveragedistance = 0;
for (int i = 0; i < RunningAverageCount; ++i)
{
RunningAveragedistance += RunningAverageBuffer[i];
}
RunningAveragedistance /= RunningAverageCount;
delay(100);
//if (NextRunningAverage == RunningAverageCount - 1);{
Serial.print(" RunningAverageCount ");
Serial.println( RunningAverageCount);
Serial.print ("Next Running Average ");
Serial.println (NextRunningAverage);
Serial.print("RunningAveragedistance ");
Serial.println(RunningAveragedistance, 0);
}
void loop() {
digitalWrite(resetPin, HIGH);
display.setTextSize(2);
if (sensor.timeoutOccurred()) {
Serial.print(" TIMEOUT");
}
delay(500);
}
// De Bouncing routine to read button
buttonPress = digitalRead(button); // check state of button
if (buttonPress == 1) // check if it has been pressed
delay (50); // wait 50 ms
{
buttonPress = digitalRead(button); // check button again
if (buttonPress == 0) { // if it is 0 considered 1 press
state = old + 1; //Increase state by 1
}
if (measure.RangeStatus != 4) { // phase failures have incorrect data
delay(50);
}
else { // if button has not been pressed
//delay (100); // Wait 100 ms
Serial.println();
switch (state) { // React to buttonPress and state
case 1:
digitalWrite(laser, HIGH);
Serial.println ("Case 1");
display.clearDisplay();
display.setCursor(0, 0);
display.print("1 - ");
display.setCursor(40, 0);
Average();
display.print(average);
Serial.println(average);
average = 0;
display.setCursor(96, 0);
display.print("mm");
display.display();
delay(10);
old = state; // Set old state as current state
break;
case 2: // If state is 1
Serial.println ("Case 2");
display.setCursor(0, 16);
display.print(" ");
display.setCursor(0, 16);
display.print("2 - ");
display.setCursor(40, 16);
Average();
display.println(average);
Serial.println(average);
average = 0;
display.setCursor(95, 16);
display.print("mm");
display.display();
delay(10);
old = state; // Set old state as current state
break;
case 3: // If state is
Serial.println ("Case 3");
display.setCursor(0, 34);
display.print("3 - ");
display.setCursor(40, 34);
Average();
display.println(average);
Serial.println(average);
average = 0;
display.setCursor(95, 34);
display.print("mm");
display.display();
delay(10);
old = state; // Set old state as current state
break;
case 4:
Serial.println ("Case 4");
display.setCursor(0, 52);
display.print("4 - ");
display.setCursor(40, 52);
Average();
display.println(average);
Serial.println(average);
average = 0;
display.setCursor(95, 52);
display.print("mm");
display.display();
delay(10);
old = state; // Set old state as current state
break;
case 5: // If state is 1
Serial.println("Case 5");
display.clearDisplay();
display.setCursor(30, 20);
display.print("RESET");
display.display();
delay(10);
digitalWrite(resetPin, LOW);
delay(1000);
digitalWrite(laser, LOW);
Serial.println ("END");
default: // if state is not 1,2,3,4
delay(100);
digitalWrite(resetPin, HIGH);
}
}
}
All C/C++ code (with a few minor exceptions) must be in a function. The loop function ended with the curly right bracket right after the delay(500) statement that wastes the considerable power of the Arduino for 1/2 second. The word buttonPress after this is out of place. Perhaps the compiler's message could be more clear but it is trying to tell you that something is wrong. Move the curly right bracket to a more appropriate place. Use the IDE auto-format feature (CTRL-T on a PC) to better see these things.