Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <Wire.h>
#include <AStar32U4.h>
#include <VL6180X.h>
VL6180X sensor;
AStar32U4LCD lcd;
AStar32U4ButtonA buttonA;
AStar32U4ButtonB buttonB;
AStar32U4ButtonC buttonC;
int ave;
int readings[200];
int value;
long lap_time[4];
long lap_time_raw[4];
int lap = 0;
int btn_pushes = 0;
void setup()
{
Serial.begin(9600);
Wire.begin();
sensor.init();
sensor.configureDefault();
sensor.setScaling(1);
sensor.setTimeout(500);
lcd.clear();
lcd.print("Press C");
lcd.gotoXY(0, 1);
lcd.print("to cal");
buttonC.waitForButton();
lcd.clear();
lcd.print("Calib..");
//wait half a second and then start calibrating
delay(500);
ave = 0;
for(int i=0; i<200; i++)
{
readings[i] = sensor.readRangeSingleMillimeters();
ave = (ave + readings[i])/2;
}
lcd.clear();
lcd.gotoXY(0, 0);
lcd.print("ave");
lcd.gotoXY(4, 0);
lcd.print(ave);
}
void loop()
{
lap = 0;
if (buttonA.getSingleDebouncedRelease())
{
//cycle through lap times and total time
btn_pushes++; //increment to track what push in cycle we are on
if (btn_pushes==1)
{
lcd.clear();
lcd.print("Lap 1");
lcd.gotoXY(0, 1);
lcd.print(lap_time[1]);
}
else if (btn_pushes==2)
{
lcd.clear();
lcd.print("Lap 2");
lcd.gotoXY(0, 1);
lcd.print(lap_time[2]);
}
else if (btn_pushes==3)
{
lcd.clear();
lcd.print("Lap 3");
lcd.gotoXY(0, 1);
lcd.print(lap_time[3]);
}
else if (btn_pushes==4)
{
lcd.clear();
lcd.print("Total");
lcd.gotoXY(0, 1);
lcd.print(lap_time[0]);
btn_pushes = 0;
}
}
if (buttonC.getSingleDebouncedRelease())
{
lcd.clear();
lcd.print("Ready");
while(lap<4)
{
//start watching for robots to time laps
value = sensor.readRangeSingleMillimeters();
Serial.print(value);
if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }
Serial.println();
//turn on LED if value read from sensor 10mm lower than calibrated average
if(ave - value > 10)
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
Serial.println("LED on");
if(lap == 0)
{
lap_time_raw[0] = millis();
lcd.clear();
lcd.print("Start!");
}
else
{
lap_time_raw[lap] = millis();
lap_time[lap] = lap_time_raw[lap] - lap_time_raw[lap-1];
lcd.clear();
lcd.print("Lap Time");
lcd.gotoXY(0, 1);
lcd.print(lap_time[lap]);
}
if (lap == 3)
{
lap_time[0] = lap_time_raw[3] - lap_time_raw[0];
lcd.clear();
lcd.print("Tot Time");
lcd.gotoXY(0, 1);
lcd.print(lap_time[0]);
}
lap++;
delay(4000);
}
else
{
digitalWrite(LED_BUILTIN, LOW); // turn the LED off
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment