Iám currently interfacing the arduino code with a python GUI and Iám plotting the output of the sensor( LIDR) in real time. My problem is in offsetting or resetting the time to 0 if a certain condition is met.
My by using the serial monitor to send data as follows:
[1,23,232,23] -- to start the reading the sensors in realtime ( runs the void Zero loop)
[0,23,232,23] -- to stop reading the sensors ( runs the void one loop)
When I stop the reading of the sensors then start again the time still continue and it does not reset to zero.
#include "Adafruit_VL53L0X.h"
Adafruit_VL53L0X lox = Adafruit_VL53L0X();
const int redPin = 3;
const int redPinkp = 9;
const int redPinki = 10;
const int redPinkd = 11;
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
boolean newData = false;
unsigned long StartTime;
int InitVal = 0; // change to init value or red
int red = 0;
int redkp = 0;
int redki = 0;
int redkd = 0;
double Input;
void setup() {
// initialize serial:
Serial.begin(115200);
while (! Serial) {
delay(1);
}
Serial.println("Adafruit VL53L0X test");
if (!lox.begin()) {
Serial.println(F("Failed to boot VL53L0X"));
while(1);
}
// power
Serial.println(F("VL53L0X API Simple Ranging example\n\n"));
// make the pins outputs:
pinMode(redPin, OUTPUT);
pinMode(redPinkp, OUTPUT);
pinMode(redPinki, OUTPUT);
pinMode(redPinkd, OUTPUT);
}
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
One();
newData = false;
}
else {
Zero();
}
}
///////////////////// ///////////////////// /////////////////////
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
///////////////////// ///////////////////// /////////////////////
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars,","); // get the first part - the string
InitVal = atoi(strtokIndx); // copy it to messageFromPC
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
red = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
redkp = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
redki = atoi(strtokIndx); // convert this part to an integer
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
redkd = atoi(strtokIndx); // convert this part to an integer
}
///////////////////// ///////////////////// /////////////////////
void One() {
if (InitVal == 0){
delay(5);
//Serial.print(0);
// Serial.print(",");
// Serial.print(0);
// Serial.print(",");
// Serial.print(0);
// Serial.print(",");
//Serial.println(0);
//Serial.print(0);
//Serial.print(",");
// Serial.print(0);
// Serial.print(",");
//Serial.print(0);
// Serial.print(",");
// Serial.println(0);
// Serial.print(0);
// Serial.print(",");
// Serial.print(0);
// Serial.print(",");
/// Serial.print(0);
// Serial.print(",");
// Serial.println(0);
// Serial.print(0);
//Serial.print(",");
// Serial.print(0);
// Serial.print(",");
// Serial.print(0);
// Serial.print(",");
// Serial.println(0);
analogWrite(redPin, 0);
delay(5);
}
}
///////////////////// ///////////////////// /////////////////////
void Zero() {
if (InitVal == 1){
//// make sure to output 0 when running///
//// make sure to output 0 when running///
readPosition() ;
Serial.print(",");
Serial.println(red);
}
}
///////////////////// ///////////////////// /////////////////////
float readPosition()
{
VL53L0X_RangingMeasurementData_t measure;
lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout!
if (measure.RangeStatus != 4) { // phase failures have incorrect data
StartTime = millis();
Serial.print(measure.RangeMilliMeter);
Serial.print(",");
Serial.print(measure.RangeMilliMeter);
Serial.print(",");
Serial.print(StartTime);
} else {
StartTime = millis();
Serial.print(0);
Serial.print(",");
Serial.print(0);
Serial.print(",");
Serial.print(StartTime);
}
delay(10);
return measure.RangeMilliMeter; //Returns distance value.
}