Issues with the sensor counter using PulseIn function.

Hey Guys,

I am a newbie to Arduino. I am currently doing a project that includes Arduino UNO controller hooked to a retroreflective sensor (transmitter & receiver on the same side of the sensor). It is a simple application that involves interrupting the sensor. The sensor generates an active LOW pulse upon interruption and based on that a counter increments by 1. My issue is with the counter itself. So here it goes: I used the PulseIn function and it did work well. I used the LOW mode(the sensor goes active LOW upon interruption) and it did the trick. Now I want the controller to increment by 1 only when the duration of the interruption is above a certain number of milliseconds. So that it filters out the fast instantaneous interruption's. I started with figuring out the average timing of each interruption. This is where the problem starts, I am aware that the readings of the pulseIn fn. is in (usec). The controller only outputs the duration of fast interrupts, so when the interrupt is long (i.e. you interrupt and the pulse goes LOW and is kept at that state for a second or 2) the controller doesn't give any output. This was quite confusing since the pulseIn fn. could return length of the pulse for upto 3 min. in length. I did assign the longest datatype (Double). I guess it has to do something with the watchdog timer itself. It is resetting the internal clock when it sees that the pulse is taking too long to go to HIGH from LOW. Could this be a possible reason, I want to know as to how can you alter the default settings of the watchdog timer (Interrupt timer).

So guys, I would really appreciate your help in this regard.

Desperate Programmer! :(

This is the code that I've composed:

/Counter using PulseIN/

double duration; int counter = 0; void setup() { pinMode (5, INPUT); Serial.begin(9600); } void loop() { duration = pulseIn(5, LOW); if (duration >100000) {

counter ++; Serial.println (counter); Serial.println (duration); } }

First, the pulseIn function returns the number of microseconds that elapse after the signal next goes HIGH (in you case) until it goes LOW again. It returns a whole number of microseconds. Why are you storing this as a double? It should be stored in an unsigned long variable.

Second, literals are interpreted as ints, in the absence of directives to the contrary. 100000 is NOT an int sized value.

100000UL would be interpreted as an unsigned long value, which is what duration should be, so the comparison can be made correctly.

I did assign the longest datatype (Double). I guess it has to do something with the watchdog timer itself.

That is not the longest data type, nor is it the correct data type. The watchdog timer is not a factor. It is not involved at all.

Second, literals are interpreted as ints, in the absence of directives to the contrary. 100000 is NOT an int sized value. 100000UL would be interpreted as an unsigned long value, which is what duration should be, so the comparison can be made correctly.

First, the pulseIn function returns the number of microseconds that elapse after the signal next goes HIGH (in you case) until it goes LOW again. It returns a whole number of microseconds

Correct me if I am wrong: So, initially the pulse from the sensor is HIGH. Upon interruption, the pulse goes LOW and the pulseIn fn. initiates the duration. After taking off the interruption, the pulse goes HIGH and the duration stops and returns the length. ri8?

Second, literals are interpreted as ints, in the absence of directives to the contrary.

By literals you mean variables, ri8? I didn't quite understand the rest.

so the comparison can be made correctly.

?! I didn't quite get this one either

"ri8" ??

Second, literals are interpreted as ints, in the absence of directives to the contrary

By literals you mean variables

No, PaulS meant literals. See:

int I1 = 10000 ;     // this is a int variable(I1) and an int literal(10000)
long L1 = 100000L ; // this is a long variable(L1) and a long literal(100000)
long L2 = 100000 ;  // this is a int(?!) constant (cant be larger than 32K, so what does the 100000 turn into?)
                    //  to be stored in a long (which is fine)

comparison can be made correctly

The same as above goes for unisgned long variables and literals (postfixed with "UL")

Oh...okay now i get it Thanks! ri8 = right :)

So, this has nothing to do with the WDT & the problem is due to the data types?

So, this has nothing to do with the WDT & the problem is due to the data types?

It has nothing to do with the watchdog timer, and you need to correct the data types and invalid literals, and see if the problem persists.