If not working

serial monitor is making an infinite loop without reading if the sensor is detecting an obstacle, can somebody help me, im just trying to measure the time between

sensor is blocked
sensor is not blocked

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

StopWatch vueltaKart;
infrarrojo estado(13); //ir port
int VALOR; //data value
int led =12; //optional pin for reference if the IR is blocked or not
int led_estado = estado.lectura(VALOR); //sensor state variable


void setup() { 

       pinMode(led, OUTPUT); //led
       Serial.begin(9600); //obvious
}

void loop() {
 
       Serial.print("\n leyendo estado sensor IR: "); //sending sensor state
       Serial.println(estado.lectura(VALOR)); //lprints sensor value
       led_estado = estado.lectura(VALOR); //led status

       if(led_estado == 0)                //SUPOSED TO COMPARE IF SENSOR IS OR NOT BLOCKED IN ORDER TO
      {
         digitalWrite(led,HIGH);        //TUR ON LED AND SERIAL PRINT THE TIMING.. HELLPP!!!
         Serial.println("Tiempo de vuelta:");
         Serial.println(vueltaKart.elapsed());        
         vueltaKart.stop();
      }
       else
      {
         digitalWrite(led,LOW);
         vueltaKart.start();
      }

}

infrarrojo.cpp (1.09 KB)

infrarrojo.h (965 Bytes)

What do you mean by "serial monitor is making an infinite loop without reading if the sensor is detecting an obstacle". It will print

Serial.println(estado.lectura(VALOR));

all the time. So how does it look on the monitor and [u]what do you expect[/u]?

The time between blocked and not blocked will be 0. That's because the sensor is ether blocked or not blocked, nothing in between.

Or do you want to know the time between the sensor becoming blocked and the sensor becoming unblocked again? That's something completely different ;)

what im supposed to see is how much time has passed when the sensor was blocked until is blocked again

and what i see in the serial monitor is this:

leyendo estado sensor IR: 1

leyendo estado sensor IR: 1

leyendo estado sensor IR: 1

leyendo estado sensor IR: 1

leyendo estado sensor IR: 1

leyendo estado sensor IR: 1

leyendo estado sensor IR: 1

leyendo estado sensor IR: 1

leyendo estado sensor IR: 1

.....

inifinite times

i just want to see how much time is elapsed betwwen passes

i want to see something like this:

tiempo de vuelta 832832

tiempo de vuelta 32324

tiempo de vuelta 2102

everytime IR is blocked.... sorry for my english

Why don't you simply use millis() or micros()? (Sorry, I've never used Stopwatch)

because with millis or micros i cant seem to reset it, thats why i use the stopwatch class :( if you can give me a hint it would be highly appreciated thank you for your responses

int led_estado = estado.lectura(VALOR); //sensor state variable Will only return 1 or 0 Why use return type long?

long infrarrojo::lectura(int lec)//LA FUNCION REGRESA UN VALOR TIPO LONG
{
 if(digitalRead(_pin)==HIGH)//DETECTA SI EL PIN ESTA EN ESTADO ALTO O BAJO
 {
      lec = 1;
  return lec;//RETORNA LEC CON EL VALOR DE UNO
 }
 else
 {
  lec = 0;
  return lec;
 }

im sorry for my super ignorance but how can i correct it? if i replace the Int with the long will it work the way im expecting?? it cant compile :(

can you give me a hand again thank you for your great support i have no words ive been trying to figure this out for days and like googling you get nothing but more confused, again thank you people you really rule (y)

Will you tell us what you want to accomplish? Use a step by step explanation

ericklopez: because with millis or micros i cant seem to reset it, thats why i use the stopwatch class :( if you can give me a hint it would be highly appreciated thank you for your responses

You don't need to reset. Just subtract the value at the beginning of a time period from the value at the end.

ok: IR sensor is beaming IR sensor is blocked reset the timer to 0 start to counting the time in mills, second, mins IR sensor is beaming again when the ir sensor is blocked again show the time that has elapsed since the last time reset the timer to 0 again start counting the time in mills, seconds, mins

the program is supposed to tell me 00:00:00 how much time has passed when the sensor was blocked then unblocked then blocked

im trying to count the Lap time of a car when interrupts the sensor signal

again sorry for my terrible english

hello??

Hello?

Reply #4.
How’s that going?

No luck i havent been able to fix it, i dont know how to stop the loop from showing me just the first line, i dont know how to make it wait until the IR is blocked again

i just want the time elapsed when the car passes the IR sensor each lap in mills in micros in whatever but it just keeps on looping that meesage

leyendo estado sensor 1

leyendo estado sensor 1

leyendo estado sensor 1

leyendo estado sensor 1

leyendo estado sensor 1 please help me with some code, i can pay if you have elance account, thanks in advance

What is VALOR ? Is it defined anywhere? ? ? ? ? ? ?

The entire sketch makes no sense.

Do you under stand how the attached sketch works?
It is not what you want, it shows how to handle timing things.

If so you can use sections of it along with some input coding to record times and then do the calculations needed.

//Blink without Delay skeleton 
//4 examples demonstrated
//

//LED wiring options
//=============================================
//Depending which way your LEDs are wired, uncomment the next line.
//#define PlusEqualsON 

#ifdef PlusEqualsON
//wired so +5V turns LED ON
#define ledON  HIGH
#define ledOFF LOW
//=========================
#else
//wired so +5V turns LED OFF
#define ledON  LOW
#define ledOFF HIGH
//=========================
#endif

//switch wiring options
//=============================================
//Depending which way your switches are wired, uncomment the next line.
#define PushEqualsLOW 

#ifdef PushEqualsLOW 
//pushing the switch makes pin LOW
#define Pushed   LOW
#define Released HIGH
//=========================
#else
//pushing the switch makes pin HIGH
#define Pushed   HIGH
#define Released LOW
//=========================
#endif

//=============================================
unsigned long currentMillis;
unsigned long pin13Millis;
unsigned long pin12Millis;
unsigned long pin11Millis;
unsigned long SwitchMillis;

//if these are not changed in the sketch, they can be const
unsigned long debounceMillis = 100UL;     //100ms
unsigned long ledOnTime      = 5*1000UL;  //5 seconds

byte laststartSwitchState    = HIGH;
byte buttonState             = HIGH;
byte counter                 = 0;

//the following are enable/disable flags
//some of these might not be used in this sketch
boolean flag13 = true;
boolean flag12 = true;
boolean flag11 = true;
boolean flag10 = true;

const byte startSwitch = 2; //pushed = LOW
const byte testSwitch  = 3; //pushed = LOW

//**********************************************************************

void setup()
{
  Serial.begin(9600);
  
  digitalWrite(13,ledOFF);
  pinMode(13, OUTPUT); 

  digitalWrite(12,ledOFF);
  pinMode(12, OUTPUT);

  digitalWrite(11,ledOFF);
  pinMode(11, OUTPUT);
  
  digitalWrite(10,ledOFF);
  pinMode(10, OUTPUT);

  pinMode(startSwitch, INPUT_PULLUP); //pushed = LOW
  pinMode(testSwitch,  INPUT_PULLUP); //pushed = LOW

} //  >>>>>>>>>>>>>> E N D  O F  s e t u p ( ) <<<<<<<<<<<<<<<<<

void loop()
{
  //save the current time
  currentMillis = millis();

  //************************************* E x a m p l e  1
  //toggle pin 13 every 200mS
  //has 200ms or more gone by?
  if (currentMillis - pin13Millis >= 200UL)
  {
    //code here runs every 200ms
    //get ready for next iteration
    pin13Millis = pin13Millis + 200UL;
    //toggle pin 13
    digitalWrite(13,!digitalRead(13));
  }

  //************************************* E x a m p l e  2
  //at power up, pin 12 LED goes ON, after 3 seconds goes OFF and stays OFF
  //could be used as a powerup reset signal
  if (flag12 == true && currentMillis - pin12Millis <= 3000UL)
  {
    //code here runs for 3 seconds after power up, then stops
    digitalWrite(12,ledON);
  }
  else
  {
    digitalWrite(12,ledOFF);
    //disable further pin 12 control
    flag12 = false;
  }

  //************************************* E x a m p l e  3
  //if testSwitch is pushed and released
  //pin 11 LED goes ON for 5 seconds, then goes OFF 
  buttonState = digitalRead(testSwitch);
  
  //are we are allowed to check the switch and is it pressed?
  if(flag11 == true && buttonState == Pushed)
  {    
    //enable timing of LED on pin 11
    flag11 = false; //false --> timing is enabled
    //turn LED ON
    digitalWrite(11,ledON);
    //record the time LED turned ON
    pin11Millis = currentMillis;
  }
    
  //are we allowed and is it time to control pin 11
  if (flag11 == false && currentMillis - pin11Millis >= ledOnTime)
  {
    //if enabled, code here runs after ledOnTime ms goes by
    digitalWrite(11,ledOFF);
    //allow switch press detection again
    flag11 = true; //true --> switch monitoring is enabled
  }

  //************************************* E x a m p l e  4
  //is it time to check the switches?
  //in particular, pushing startSwitch will turn ON/OFF (toggle) an output pin 10
  //is it time to check the switches
  if (currentMillis - SwitchMillis >= debounceMillis)
  {
    //code here runs every debounceMillis ms
    //get ready for the next iteration
    SwitchMillis += debounceMillis; 
    //go and check the switches
    checkSwitches();    
  } 

  //*********************************
  //put other non-blocking stuff here
  //*********************************

} //  >>>>>>>>>>>>>> E N D  O F  l o o p ( ) <<<<<<<<<<<<<<<<<


//======================================================================
//                      F U N C T I O N S
//======================================================================


//****************** c h e c k S w i t c h e s ( ) *********************
//switches are checked every debounceValue milli seconds 
//no minimum switch press time is validated with this code (i.e. No glitch filter)
void checkSwitches()  
{
  //re-usable for all the switches  
  boolean thisState;    

  //************************************* E x a m p l e  Push ON push OFF (toggle)   
  //check if this switch has changed state
  thisState = digitalRead(startSwitch);
  if (thisState != laststartSwitchState)
  {  
    //update the switch state
    laststartSwitchState = thisState;  

    //this switch position has changed so do some stuff

    //"HIGH condition code"
    //switch went from LOW to HIGH
    if(thisState == HIGH)        
    {
      //Do some HIGH switch stuff here
    }

    //"LOW condition code"
    //switch went from HIGH to LOW
    else                          
    {
      //Do some LOW switch stuff here  
      digitalWrite(10, !digitalRead(10));
      //print number of pushes
      counter++;
      Serial.println(counter);
    }

  } //END of startSwitch code

  //*****************************************  
  //similar code for other switches goes here 
  //*****************************************  

} //END of checkSwitches()

//**********************************************************************

//======================================================================
//                      E N D  O F  C O D E
//======================================================================
unsigned long currentMillis;
unsigned long pin13Millis;
unsigned long StartMillis;
unsigned long time;

boolean timingMode = false;

const byte startSwitch = 2; //pushed = LOW
const byte stopSwitch  = 3; //pushed = LOW

//**********************************************************************

void setup()
{
  Serial.begin(9600);

  pinMode(13,OUTPUT);
  pinMode(startSwitch, INPUT_PULLUP); //pushed = LOW
  pinMode(stopSwitch,  INPUT_PULLUP); //pushed = LOW

} //  >>>>>>>>>>>>>> E N D  O F  s e t u p ( ) <<<<<<<<<<<<<<<<<

void loop()
{
  if(timingMode == false && digitalRead(startSwitch) == LOW)
  {
    StartMillis = millis();
    timingMode = true;
  }
  if(timingMode == true && digitalRead(stopSwitch) == LOW)
  {
    time = millis() - StartMillis;
    Serial.print("Time from Start to Stop = ");
    Serial.println(time);
    timingMode = false;
  }

  currentMillis = millis();

  //toggle pin 13 every 200mS
  //has 200ms or more gone by?
  if (currentMillis - pin13Millis >= 200UL)
  {
    //code here runs every 200ms
    //get ready for next iteration
    pin13Millis = pin13Millis + 200UL;
    //toggle pin 13
    digitalWrite(13,!digitalRead(13));
  }

  //*********************************
  //put other non-blocking stuff here
  //*********************************

} //  >>>>>>>>>>>>>> E N D  O F  l o o p ( ) <<<<<<<<<<<<<<<<<

OMG Larry thank youuu thank youuu thank you G-d bless you wow! thanks!!

The sketch in post 16 will have to be modified to meet your needs as it is designed for a Start and Stop switch situation. The flashing of the pin 13 led is there just to check to see if your code is blocking. The led should continually flash if things are written correctly.

You should review Robin2 's and Nick's discussions.

http://forum.arduino.cc/index.php?topic=223286.0

http://www.gammon.com.au/millis

Also take a look at these:

https://forum.arduino.cc/index.php?topic=384198.0 .

thank youuu im so so happy and excited, its 2:00am and i finally able to move forward, thank you, i mean you cant imagine what you have done for me, thank you!