Record time between two Events (HC-SR04)

Hello!

I am doing a rc car project. As one of the features on the rear of the car there is an HC-SR04 sensor. I would like to find the time between the car driving away from a wall (HC-SR04 sensor will detect distance larger than 2cm) to the time of it reversing back against the wall (HC-SR04 sensor will detect distance less than 2cm). And I need to somehow record the time between these two actions (which would be probably 30 seconds long).

As you can see I already have a lot of code, and don’t want the new code to affect the performance of my servo and DC motor (responsiveness of them) so any help would be really appreciated!

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
#include <RFID.h>
#include "SR04.h"

/* Define the DIO used for the SDA (SS) and RST (reset) pins. */
#define SDA_DIO 13
#define RESET_DIO 10

/* Create an instance of the RFID library */
RFID RC522(SDA_DIO, RESET_DIO); 

/*ultrasound*/
#define TRIG_PIN 7
#define ECHO_PIN 6 

SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long a, b;

int cardID;
boolean didMyOneTimeAction = false;
int buzzer = 8;
int LED = 5;




#define ENABLE 12
#define DIRA 11   //DC MOTOR
#define DIRB 9
int i;

Servo servo;
RF24 radio(4, 53); // CSN, CE
const byte address[6] = "00001";
int servo_pin = 3;
int motorPin = 2;
int x_pos;
int GotArray[2];

void setup() {
  pinMode(motorPin, OUTPUT);

  //---set pin direction
  pinMode(ENABLE,OUTPUT);
  pinMode(DIRA,OUTPUT);
  pinMode(DIRB,OUTPUT);
  
  Serial.begin(9600);
  radio.begin();
  servo.attach(servo_pin ) ; 
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
  servo.write(0);// move servos to center position -> 90°

  // Enable the SPI interface 
  SPI.begin(); 
  // Initialise the RFID reader
  RC522.init();
  pinMode(LED, OUTPUT);
  pinMode(buzzer,OUTPUT);//initialize the buzzer pin as an output

}

void loop() {

//_______________________RFID SECTION_________________________________
  /* Has a card been detected? */
  if ((RC522.isCard()) && (didMyOneTimeAction == false)) {
    /* If so then get its serial number */
    RC522.readCardSerial();
    Serial.println("Card detected:");
    
    for(int i=0;i<5;i++)  {
      cardID = (RC522.serNum[i]);
      Serial.print(cardID);
    }
    
    if ((cardID==114) && (didMyOneTimeAction == false)) {
      Serial.println();
      Serial.println("Correct Card:");
      digitalWrite(LED, HIGH);   
      delay(70);              
      digitalWrite(LED, LOW);   
      delay(70);  
      digitalWrite(LED, HIGH);   
      delay(70);              
      digitalWrite(LED, LOW);   
      delay(70); 
      digitalWrite(LED, HIGH);   
      delay(70);              
      digitalWrite(LED, LOW);   
      delay(300); 
      for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
      // sets the value (range from 0 to 255):
      analogWrite(LED, fadeValue);
      delay(100);
      }
    
    didMyOneTimeAction = true;
    }

    else {
      Serial.println();
      Serial.println("Wrong Card:");
    }
  }  

  //_______________________SERVO SECTION_________________________________
  if (radio.available() && (didMyOneTimeAction == true)) {
    radio.read(&GotArray, sizeof(GotArray));
    Serial.println(GotArray[0]);
    Serial.println(GotArray[1]);
    x_pos = map(GotArray[0], 0, 1023, 0, 180);
    if (x_pos>400 && x_pos<600){ 
    }
    else{
      servo.write(x_pos/2);
    }
      //_______________________DC MOTOR SECTION_________________________________
    int speed = (GotArray[1]-508)/2.2;
    Serial.println(speed);
    
    analogWrite(ENABLE,255); //enable on
    if (speed > 0) {
        digitalWrite(DIRA,HIGH); //one way
        digitalWrite(DIRB,LOW);
        analogWrite(ENABLE,speed); //input speed
        //_______________________ULTRASOUND SENSOR DISPLAY SECTION_________________________________
        if (didMyOneTimeAction == true) {
          a=sr04.Distance();     //Return distance in cm
          b=timepulse();         //Return distance in microseconds
          Serial.print(a);
          Serial.print("cm   ");
          Serial.print(b);
          Serial.println("us");
          delay(10);
        }
        //_______________________BUZZER SECTION_________________________________
        if (a > 20) {
          digitalWrite(buzzer, LOW);
        } 
        else if ((10 < a) && (a < 20)) {
          digitalWrite(buzzer, HIGH); 
        }
        else if (10 > a) {
          digitalWrite(buzzer, LOW);
        }
        //______________________________________________________________________________________________
        delay(50);
    }
    else if (speed < 0) {
        digitalWrite(DIRA,LOW); //other way
        digitalWrite(DIRB,HIGH);
        analogWrite(ENABLE,abs(speed)); //input speed
        //_______________________ULTRASOUND SENSOR DISPLAY SECTION_________________________________
        if (didMyOneTimeAction == true) {
          a=sr04.Distance();     //Return distance in cm
          b=timepulse();         //Return distance in microseconds
          Serial.print(a);
          Serial.print("cm   ");
          Serial.print(b);
          Serial.println("us");
          delay(10);
        }
        //_______________________BUZZER SECTION_________________________________
        if (a > 20) {
          digitalWrite(buzzer, LOW);
        } 
        else if ((10 < a) && (a < 20)) {
          digitalWrite(buzzer, HIGH); 
        }
        else if (10 > a) {
          digitalWrite(buzzer, LOW);
        }
        //______________________________________________________________________________________________
        delay(50);
    }
  }

  digitalWrite(DIRA,LOW); //one way
  digitalWrite(DIRB,LOW);
  digitalWrite(ENABLE,LOW); //all done
}

  //_______________________ULTRASOUND SENSOR DETECTION SECTION_________________________________
long timepulse() {
    long duration = 0;
    digitalWrite(TRIG_PIN, LOW);
    delayMicroseconds(2);
    digitalWrite(TRIG_PIN, HIGH);
    delayMicroseconds(10);
    digitalWrite(TRIG_PIN, LOW);
    delayMicroseconds(2);
    duration = pulseIn(ECHO_PIN, HIGH, PULSE_TIMEOUT);
//    delay(25);
    return duration;
}

Save off the millis() counter when the distance BECOMES larger than 2cm. When the distance BECOMES than 2cm subtract the saved value from millis() and you will have the number of milliseconds between the events.

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