Speed Camera project

Hi! I was doing a project for my school with Arduino (it’s not the first time I use Arduino, I’d say I’m quite capable), but it doesn’t work well.
It’s code for a speed camera project.

#define TR1 10
#define TR2 12
int detection1 = HIGH;     // no obstacle1
int detection2 = HIGH;     // no obstacle2
unsigned int inizioMisura = 0;
unsigned long Ti = 0;
unsigned long Tf = 0;
float deltaT = 0;
const float distanza = 0.115;
const float conv = 3.6;    //conv=1 per m/S - conv=3.6 per Km/h

void setup() {
  Serial.begin(9600);
  pinMode(TR1, INPUT);
  pinMode(TR2, INPUT);
}

void loop() {
  detection1 = digitalRead(TR1);
  detection2 = digitalRead(TR2);
  if(detection1 == LOW && inizioMisura == 0){
    Ti = millis();
    //Serial.println("Ti= "+String(Ti));
    inizioMisura = 1;
  }
  if(detection2 == LOW && inizioMisura == 1){
    Tf = millis();
    deltaT = (float)(Tf-Ti)/1000;
    //Serial.println("Tf= "+String(Tf));
    //Serial.println(deltaT,3);
    Serial.println(conv*distanza/deltaT,1);
    inizioMisura = 0;
  }
}

I think the code is correct, but when I use it the values are "off": if I put my finger on the sensors slowly it gives me a lower number than when I pass it quickly.
This is the circuit design.


(the infrared sensors are different from the image, the sensors are these: https://it.aliexpress.com/item/1005006095222017.html)

And what is it supposed to do? Measure the time it takes you to pass your finger from sensors 1 to sensor 2?

Your code resembles an fsm.

Try this:

void loop () {
  switch (initioMizura) {

    case 0: // state 0: stay put until sensor 1 is active 
       if (digitalRead(detection1) == LOW) {
          Ti = millis();
           initioMizura = 1; // transition to state 1
       }

    case 1: // state 1: stay put until sensor 2 is active
       if(digitalRead(detection2) == LOW) {
           Tf = millis();
           deltaT = (float)(Tf-Ti)/1000.0;
           Serial.println(conv*distanza/deltaT,1);
           initioMizura = 0; // go back to state 0
       }
   }
}

Please note that I have not compiled or tested this code

Have you adjusted the pots on the sensors so that the sensors actually work?

Hint: this is all you have to post for the sensor link:
https://it.aliexpress.com/item/1005006095222017.html

In the link you posted above, the question mark and everything after it is tracking your personal web activity.

yes, I did, and thanks for the hint, i didn't know that :grimacing:

Yes, it is supposed to to this. Sorry I didn't specifed that 'InizioMisura' means startMeasure.
Anyway, today I'll try your code, thanks

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