Speed detection with nrf24L01 modules

I created a speed detection device to calculate the speed of a hockey shot. It involved the use of a vibration sensor attached to the net and an ultrasonic sensor attached to a shooting pad. The time between the sensors was calculated and used to get speed.

I am now trying to introduce nrf24L01 radio modules to make the system wireless. This is the receiver portion of the code. The logic I am trying to implement is that if the ultrasonic sensor gets a reading, check for vibration sensor data from the transmitter, and if the vibration Sensor data is == HIGH or 1, execute the remaining code. I am currently having difficulties trying to get the receiver to execute the remaining code after detecting the sensor data.


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

 ultrasonic();

 if (cm < 20){

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

 } if (radio.available()) {
  radio.read(&SensorData, sizeof(SensorData));
    Serial.println(SensorData[0]);
    
     if (SensorData[0] == 1){

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

float presetMetres = 7.62; //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
Serial.println("m/msec");

delay(100);

FinalVelocity = PreVelocity*3600;
Serial.print("The velocity after conversions is: ");
Serial.print(FinalVelocity, 2);
Serial.print("km/h");
Serial.print("\n");

delay(100);


for (int x = 0; x < 4; x++){
lcd.setCursor(0, x);  
lcd.backlight();
lcd.print("Speed: ");
lcd.print(FinalVelocity);
lcd.print("km/h");

}

//LEDshotsequences();

    }
//leaderboard();
 }
 
}

This is the transmitter code:


#include <SPI.h>
#include <printf.h>
#include <nRF24L01.h>
#include <RF24_config.h>
#include <RF24.h>


RF24 radio(9,10); // CE, CSN
const byte address[6] = "00001";

const int vibrationSensor = 8;
//boolean vibrationValue = "0";
int SensorData[0]; 

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

  
radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
}

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

SensorData[0] = digitalRead(vibrationSensor);
Serial.println(SensorData[0]);
radio.write(&SensorData, sizeof(SensorData));
}

Count your braces { } I didn't print it out and mark each but it looks like your

if (radio.available()) {

close brace is at the end of the code.

How do you know that it actually is receiving a detection? Does the debug print out confirm this? What does it print?

Good point.

The OP should delete all the non communication code and get a transmit and receive pair to communicate at some vary base level. Perhaps transmit an incrementing value every second or so.

The little I've done with radios was initially frustrating not knowing it the transmitter or receiver or both were not working.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.