Wireless Timing Project

Pages
Contributors: Gior Dior
Favorited Favorite 5

Finish Setup

The “Finish” setup is a little bit more involved than the “Start” setup. For the “Finish”, we have an ESP32, distance sensor, Metal Pushbutton, and display. To make all these components work in harmony, we will need considerably more code than the “Start”. We will begin by connecting the sensor and OLED display to the ESP32 via qwiic connect.

alt text

When your hardware is all set, we can move to flashing the device with our “Finish” code. The “Finish” code will run through its setup, getting the distance sensor up and running. When the “Start” ESP32 has been triggered, this code will receive a “1” integer and jump into the VOID loop “if” function. This function will set the distance sensor to begin taking measurements and wait for an object to trigger a measurement less than 1500mm. Meanwhile, the display will begin displaying the elapsed time of the race. When the sensor is triggered, the display will stop and the Void Loop will reach a “while(1)”. The purpose of the “while(1)” is to pause the code from going any further in the void loop. When the code is paused, the display will show the total elapsed time in seconds. This is where the reset pushbutton comes in handy, because the next step is to record your time on a piece of paper and restart the setup for the next race.

/*
*Wireless timing Finish Sensor example, Reciever sketch
*Giordan Thompson, SparkFun Electronics, September 2022
*This example is recieves data from the Start Sensor example 
*You can find the Start Sensor example in github here ()
*
*The purpose of this code is to provide functionality to our wireless timing system 
*This code will recieve data from the Start Sensor, Start and Stop time as the sensor is 
*triggered during our race. 
*
*Complete project details can be found here () where we dive into the creation and discription 
*of this system. 
*Please feel free to customize this code fit your needs and as always please feel free to 
*raise an issue
*
*Enjoy!!
*
*/



#include <esp_now.h> //ESP wireless communication library 
#include <Wire.h> //Used to establish serial communication on the I2C bus
#include <WiFi.h>
#include "SparkFun_VL53L1X.h" //Distance Sensor Library
//#include <Adafruit_GFX.h> //Font Library
#include <Adafruit_SSD1306.h> //OLED Display Library 
#include <Fonts/FreeSans9pt7b.h> //special font Library 

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 //OLED display hieght, in pixels
#define OLED_RESET -1  // GPIO -1
#define SCREEN_ADDRESS 0x3C //OLED diplay address

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

SFEVL53L1X distanceSensor2;

//define variables used in this example
int start=0;
int min1=0;
unsigned long tim=0;
unsigned long msec=0;
unsigned long mili=0;
int sec1=0;

//OLED display logo 
const unsigned char PROGMEM icon [] = {
0x00, 0x1F, 0xF8, 0x00, 0x00, 0x1F, 0xF8, 0x00, 0x00, 0x1F, 0xF8, 0x00, 0x00, 0x1F, 0xF8, 0x00,0x00, 0x1F, 0xF8, 0x00, 0x00, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0xE0,0x00, 0x1F, 0xF8, 0xF0, 0x00, 0x7F, 0xFE, 0x70, 0x00, 0xFF, 0xFF, 0x20, 0x01, 0xF0, 0x0F, 0x80,0x03, 0xC1, 0x83, 0xC0, 0x03, 0x81, 0x81, 0xC0, 0x07, 0x01, 0x80, 0xE0, 0x07, 0x01, 0x80, 0xE0,0x0E, 0x01, 0x80, 0x70, 0x0E, 0x01, 0x80, 0x70, 0x0E, 0x03, 0xC0, 0x70, 0x0E, 0x03, 0xC0, 0x70,0x0E, 0x03, 0xC0, 0x70, 0x0E, 0x01, 0x80, 0x70, 0x0E, 0x00, 0x00, 0x70, 0x07, 0x00, 0x00, 0xE0,0x07, 0x00, 0x00, 0xE0, 0x07, 0x80, 0x01, 0xE0, 0x03, 0xC0, 0x03, 0xC0, 0x01, 0xE0, 0x07, 0x80,0x00, 0xF8, 0x1F, 0x00, 0x00, 0x7F, 0xFE, 0x00, 0x00, 0x1F, 0xF8, 0x00, 0x00, 0x07, 0xE0, 0x00


};

//Structure example to send data
//Make sure to match the transmitter structure of Start Sensor "a" used for simplicity
typedef struct struct_message {
    int a;

} struct_message;

//Create a struct_message called incomingReading
struct_message incomingReading;

//Callback when data is recieved  
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  memcpy(&incomingReading, incomingData, sizeof(incomingReading));

  Serial.print("Incoming Reading Val:");
  Serial.print(incomingReading.a);
}


void setup() {
  Serial.begin(115200);

  //Pin for indecator LED
  pinMode(13, OUTPUT);

  Wire.begin();

  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  // Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  // Once ESPNow is successfully Init, we will register for recv CB to
  // get recv packer info
  esp_now_register_recv_cb(OnDataRecv);

  //Communicate sensor is online and ready for trigger
  if (distanceSensor2.init() == false)
    Serial.println("Sensor2 online!");

  //Display Setup
  display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
  display.display();
  display.clearDisplay();
  display.setCursor(50,0);
  display.print("Start");
  display.drawBitmap(48, 0,  icon, 32, 32, WHITE);
  display.display();
  delay(2000);

}


void loop() {

  //indicator light on, device is ready for trigger
  digitalWrite(13, HIGH);

  //Begin time when integer "1" has been recieved from the Start Sensor
  //Object has crossed Start Sensor when message recieved 
  if (incomingReading.a == 1)
  {
    display.clearDisplay();
   distanceSensor2.startRanging();

    display.setTextSize(2);
    display.setTextColor(SSD1306_WHITE);

    if(start==0)
          {
           start=1;
           tim=millis();  
         }
      msec=(millis()-tim); 



       min1=msec/60000;


        if((msec/1000)>59)
           {
            sec1=(msec/1000)-(min1*60);
           }else{
             sec1=msec/1000;
             }

         mili=(msec%1000)/10;

          display.setCursor(0,30);
          //if(min1<=9)
         //{

         // } 

            if(sec1<=9)
          {
           display.print("0");
           display.print(sec1);
           }else {display.print(sec1);}
           display.print(".");
           display.setFont(&FreeSans9pt7b);

            if(mili<=9)
          {
           display.print("0");
           display.print(mili);
           }else {display.print(mili);
           display.setTextSize(1);
                   display.print("sec");} 




   int distance = distanceSensor2.getDistance();

   //After testing, 1500 was a consistent sensor value with NO object interference 
    //pause code when the sensor has been triggered
    if (distance < 1500){
     Serial.print("Distance: ");
     Serial.print(distance);
     distanceSensor2.stopRanging();
     Serial.print("Sensor 2 Stop Ranging...");
      digitalWrite(13, LOW);
      while(1);
    }
  }

  display.display();

}