Overflow problem

Hi all!

I am working with the dust PPD42NS sensor and it works with a PWM signal which I read from an Arduino Uno. The issue is that at high concentration of particles the variable overflows. I have thought about removing the overflowed measurement and just displayed by the serial monitor the previous one which is stored in an array. To do that I have tried to detect when it overflows, with the next lines:

               if (part_1m >= FLT_MAX){
               Serial.println("The current measurement has been not taken into account due to overflow");
                part_1m= (PM_backup_1+ PM_backup_2)/2;

As you can imagine, it didn't work. What would you suggest to detect it?

Thanks in advance ;)

Let's see your whole program so that the problem can be seen in context.

boolean P2 = HIGH;
boolean trigP2 = false;

Boolean variables should be assigned true or false, not HIGH.

    //Wait one minute to have the sensor stabilized
    if (z==0){

Your watch is broken.

            part_P1 = 1.1*pow(ratioP1,3)-3.8*pow(ratioP1,2)+520*ratioP1+0.62;

pow() takes float arguments. It is a very expensive way to square or cube a number.

               goto avoidmeasurement;

Rubbish. And this is where I quit reading.

About the watch, I am not programmer, I am far away to be one, but I don’t know exactly why is broken.

65000 milliseconds in not one minute. Stop looking stupid. Delete the useless comments.

I fail to see how pow(ratioP1,2) is any better then ratioP1 * ratioP1. Or, how pow(ratioP1,3) is any better then ratioP1 * ratioP1 * ratioP1. In fact, multiplication is far faster than pow(), so using pow() is CLEARLY worse, when the second argument is a small-value int.

You need to add Serial.print() statements to your code, to see where this overflow happens. I got lost trying to follow your code. Using some functions would certainly make the code easier to follow.