Crazy results trying to measure the time. Noob Help please!

Hello everybody, im a noob, we are trying to measure lap times in a kart, ive tried with a 433mhz rx tx with no luck, when it comes to send INT messages, gives me error, i just can send text when detected, and now we've decided to use an IR sensor that its supposed to work like this:

sensor is on
when beam is blocked
if counter == 0
its a warm up lap and timer starts to count
if counter its different from 0
its supposed to give lap time and restart
counting

its giving an infinite loop with crazy results and just displaying
1
1
1
1

can somebody help me? below its the code, is it better to re use 433mhz sensor again??

#include <infrarrojo.h>
#include <StopWatch.h>

StopWatch vueltaKart;
infrarrojo estado(13); //DEFINICION DEL PIN DEL ARDUINO A USAR

int VALOR;             //VARIBLE QUE RECIBE EL DATO
int led =12;           //REDEFINICION DE PIN DE ARDUINO PARA LED INDICADOR DE PULSO(ESTO ES OPCIONAL)
int led_estado;        //VARIABLE

void setup() {
pinMode(led,INPUT);    //LED QUE INDICA EL ESTADO DEL LED
Serial.begin(9600);     //VELOCIDAD COMUNICACION SERIAL
}

void loop() {
  deteccionSensor();
Serial.print("\n leyendo estado sensor: \n");//IMPRIME MENSAJE EN PC
Serial.print(estado.lectura(VALOR));         //IMPRIME EL ESTADO DEL Vo DEL SENSOR MEDIANTE LA VARIABLE VALOR
vueltaKart.start();
delay(10000);
Serial.println(vueltaKart.elapsed());
vueltaKart.stop();
delay(50000);
}


void deteccionSensor(){
  int contador;
  while (led_estado == 1) {
    Serial.println("Tiempos de Vuelta:");
    Serial.println(vueltaKart.elapsed()); 
    vueltaKart.stop();
    vueltaKart.start();
    contador++; 
    }
    if (led_estado == 1 && contador == 0){
      vueltaKart.stop();
      vueltaKart.start();
      Serial.println("Vuelta de calentamiento");
      contador++;
      }
        
  }

ericklopez:
Hello everybody, im a noob, we are trying to measure lap times in a kart, ive tried with a 433mhz rx tx with no luck, when it comes to send INT messages, gives me error, i just can send text when detected, and now we've decided to use an IR sensor that its supposed to work like this:

sensor is on
when beam is blocked
if counter == 0
its a warm up lap and timer starts to count
if counter its different from 0
its supposed to give lap time and restart
counting

its giving an infinite loop with crazy results and just displaying
1
1
1
1

can somebody help me? below its the code, is it better to re use 433mhz sensor again??

#include <infrarrojo.h>

#include <StopWatch.h>

StopWatch vueltaKart;
infrarrojo estado(13); //DEFINICION DEL PIN DEL ARDUINO A USAR

int VALOR;            //VARIBLE QUE RECIBE EL DATO
int led =12;          //REDEFINICION DE PIN DE ARDUINO PARA LED INDICADOR DE PULSO(ESTO ES OPCIONAL)
int led_estado;        //VARIABLE

void setup() {
pinMode(led,INPUT);    //LED QUE INDICA EL ESTADO DEL LED
Serial.begin(9600);    //VELOCIDAD COMUNICACION SERIAL
}

void loop() {
  deteccionSensor();
Serial.print("\n leyendo estado sensor: \n");//IMPRIME MENSAJE EN PC
Serial.print(estado.lectura(VALOR));        //IMPRIME EL ESTADO DEL Vo DEL SENSOR MEDIANTE LA VARIABLE VALOR
vueltaKart.start();
delay(10000);
Serial.println(vueltaKart.elapsed());
vueltaKart.stop();
delay(50000);
}

void deteccionSensor(){
  int contador;
  while (led_estado == 1) {
    Serial.println("Tiempos de Vuelta:");
    Serial.println(vueltaKart.elapsed());
    vueltaKart.stop();
    vueltaKart.start();
    contador++;
    }
    if (led_estado == 1 && contador == 0){
      vueltaKart.stop();
      vueltaKart.start();
      Serial.println("Vuelta de calentamiento");
      contador++;
      }
       
  }

Hi,

try to use a global variable for contador instead of a local variable in the function. You are updating this variable each time you run the function so each time step.

Thank you sooo much for your super fast reply!
you mean to #define contador ?? still prints out

1
1
1
1
1

thank you, im sorry i know i sound too stupid but i just cant figure it out

hi,
no define before the setup function: int contador.
It should be an variable. When you do #define contador ("put a value")
you make a macro. A value that is stored and can not be changed by the program when running. While when you do int contador;
your program can change the value when running

can you please show me how would that be in code please

ericklopez:
can you please show me how would that be in code please

#include <infrarrojo.h>
#include <StopWatch.h>

StopWatch vueltaKart;
infrarrojo estado(13); //DEFINICION DEL PIN DEL ARDUINO A USAR

int VALOR; //VARIBLE QUE RECIBE EL DATO
int led =12; //REDEFINICION DE PIN DE ARDUINO PARA LED INDICADOR DE PULSO(ESTO ES OPCIONAL)
int led_estado; //VARIABLE
int contador;
void setup() {
pinMode(led,INPUT); //LED QUE INDICA EL ESTADO DEL LED
Serial.begin(9600); //VELOCIDAD COMUNICACION SERIAL
}

void loop() {
deteccionSensor();
Serial.print("\n leyendo estado sensor: \n");//IMPRIME MENSAJE EN PC
Serial.print(estado.lectura(VALOR)); //IMPRIME EL ESTADO DEL Vo DEL SENSOR MEDIANTE LA VARIABLE VALOR
vueltaKart.start();
delay(10000);
Serial.println(vueltaKart.elapsed());
vueltaKart.stop();
delay(50000);
}

void deteccionSensor(){

while (led_estado == 1) {
Serial.println("Tiempos de Vuelta:");
Serial.println(vueltaKart.elapsed());
vueltaKart.stop();
vueltaKart.start();
contador++;
}
if (led_estado == 1 && contador == 0){
vueltaKart.stop();
vueltaKart.start();
Serial.println("Vuelta de calentamiento");
contador++;
}

}

same thing my brother... In Serail Monitor it
shows

1
just one time...

can somebody help me with a code that
only prints the time when a whole lap has passed between sensor is blocked one time and another and so forth and so on...

please please anybody