Hockey shot speed Leaderboard

I created a project to test the speed of a hockey shot and I want to implement a new feature, a leaderboard system. Whenever the system turns on, I want to print the top 3 fastest shots ever recorded on the system.

From my knowledge, the arduino has an built-in hard drive that can store small amounts of updating information permanently on the board. This can be done using the EEPROM library. I would like advice on how to go about adding this feature. My idea was to compare the previous shot speed with the most recent shot speed, and the shot that is faster will be added to the top of the leaderboard. This process continues with the leaderboard shot speed being compared with the most recent shot speed, and the leaderboard being updated accordingly. Any suggestions on the logic and how I can start writing the code?

an built-in hard drive

Just a small amount of reprogrammable, permanent memory. To use it, study the examples on the EEPROM reference page.

I created a leaderboard system on python and converted it to arduino syntax. The leaderboard should work in its current state, but the problem that I’m having is that I want to put the leaderboard in a function to organize the code. It says that I can’t reference a variable in the function that I defined inside an if/else statement outside the function. In the code below, the variable that I defined outside the function is FinalVelocity. FinalVelocity represents the scores that I want to be inputted into the leaderboard.

int highscore[0];


int score = FinalVelocity;
if (score > highscore) {
   int highscore = score;
  Serial.print("The high score is: ");

} else if (score < highscore) {
    Serial.print("The high score is still ");

  for(int i = 0; i < 0; i++){



I can't reference a variable in the function that I defined inside an if/else statement outside the function.

Correct. That is a C/C++ language specification (look up "variable scope").

You can make the variable global by declaring it alone, outside of all functions.

In my case, the variables in the if/else statement are crucial to getting the scores. They have to be inside the if/else statement. Would it work if I redefined the variable outside the conditional statements? Basically how my project works is that when the ultrasonic sensor gets a reading, a time is recorded and when the vibration sensor is triggered, another time is recorded. The time between the two readings is taken and converted into speed. This is the full code.

#include <EEPROM.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);

const int vibrationSensor = 10;
boolean sensorValue = 0;
const int trigPin =  4;
const int echoPin = 6;   
long duration, cm, inches; 
unsigned long time;
long int timeStart = " ";
long int timeEnd = " ";

void leaderboard() {


void ultrasonic() {

pinMode(trigPin, OUTPUT); // initialize the trigPin
pinMode(echoPin, INPUT);// initialize the echoPin
digitalWrite(trigPin, LOW);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin, LOW); 

duration = pulseIn(echoPin, HIGH); 

cm = (duration/2) / 29.1; //converts the values into cm
inches = (duration/2) / 74;

Serial.print("in, "); 



void setup() {
  // put your setup code here, to run once:
pinMode(vibrationSensor, INPUT);


void loop() {
  // put your main code here, to run repeatedly

sensorValue = digitalRead(vibrationSensor);

 time = millis(); //starts the stopwatch when the code is uploaded


 if (cm < 30){

timeStart = time; // timeStart is equal to time when the ultrasonic is triggered.  
Serial.print("The start time is ");

}else if (sensorValue == HIGH){

timeEnd = time; //timeEnd is equal to time when the the vibration sensor is triggered
  Serial.print("The end time is: ");
int timeFinal = timeEnd - timeStart; //get the final time and print it. 
Serial.print("The final time is: ");

float presetMetres = 6; //The meters from the shooting pad and the net. Must be pre-determined.  
float PreVelocity;
PreVelocity = presetMetres/timeFinal;
Serial.print("The velocity before conversions is: ");
Serial.print(PreVelocity, 8); //The 8 is how many decimal places will be shown in the float


float FinalVelocity;
FinalVelocity = PreVelocity*3600;
Serial.print("The velocity after conversions is: ");
Serial.print(FinalVelocity, 2);


Can I declare the variable outside the if/else statement but initialize it inside the if/else statement, kind of like this?

/* global variable declaration */
int g;
int main () {

  /* local variable declaration */
  int a, b;
  /* actual initialization */
  a = 10;
  b = 20;
  g = a + b;

Of course you can.

They have to be inside the if/else statement.

The variables can be modified or initialized within if/else statements, but global in scope.

Ok great thanks!

While this leaderboard system should work, it becomes complicated when wanting to get the top 3 or multiple high score values. A simpler system would be to create a list and append each shot speed to the list, and then sort the list from highest to lowest and print the first 3 indexes. This would be pretty simple in python using .append and .sort, but I'm not sure how to do this in arduino. Any tips and suggestions?