# Sum two values given by pulseIn and the result is 0????

Good evening I hope someone can help me please…
I am using the function pulseIn in my project and when I want to make arithmetic operations with the obtained values the result is 0, can anybody help me?

This is just one part of my code:

``````starttime = millis();

// Calculation of LOW RATIO:
do {
endtime = millis();
duration_low_PM25 = pulseIn(DUST_SENSOR_DIGITAL_PIN_PM25, LOW);
duration_low_PM10 = pulseIn(DUST_SENSOR_DIGITAL_PIN_PM10, LOW);
duration_high_PM25 = pulseIn(DUST_SENSOR_DIGITAL_PIN_PM25, HIGH);
duration_high_PM10 = pulseIn(DUST_SENSOR_DIGITAL_PIN_PM10, HIGH);
lowpulseoccupancy_PM25 += duration_low_PM25;
lowpulseoccupancy_PM10 += duration_low_PM10;
highpulseoccupancy_PM25 += duration_high_PM25;
highpulseoccupancy_PM10 += duration_high_PM10;
}
while ((endtime - starttime) < sampletime_ms);

Vout1LowRatio = float(((lowpulseoccupancy_PM25) / (lowpulseoccupancy_PM25 + highpulseoccupancy_PM25))*100); // Integer percentage 0=>100
Vout2LowRatio = float(((lowpulseoccupancy_PM10) / (lowpulseoccupancy_PM25 + highpulseoccupancy_PM25))*100); // Integer percentage 0=>100
endtime = 0;
lowpulseoccupancy_PM25 = float(lowpulseoccupancy_PM25);
lowpulseoccupancy_PM10 = float(lowpulseoccupancy_PM10);
highpulseoccupancy_PM25 = float(highpulseoccupancy_PM25);
highpulseoccupancy_PM10 = float(highpulseoccupancy_PM10);
Serial.println(lowpulseoccupancy_PM25);
Serial.println(highpulseoccupancy_PM25);
Serial.println(lowpulseoccupancy_PM10);
Serial.println(highpulseoccupancy_PM10);
Serial.println(lowpulseoccupancy_PM25 + highpulseoccupancy_PM25);
Serial.println(lowpulseoccupancy_PM25/(lowpulseoccupancy_PM25 + highpulseoccupancy_PM25));
Serial.println(Vout1LowRatio);
Serial.println(Vout2LowRatio);
duration_low_PM25 = 0;
duration_low_PM10 = 0;
duration_high_PM25 = 0;
duration_high_PM10 = 0;
lowpulseoccupancy_PM25 = 0;
lowpulseoccupancy_PM10 = 0;
highpulseoccupancy_PM25 = 0;
highpulseoccupancy_PM10 = 0;
``````
``````lowpulseoccupancy_PM25 = float(lowpulseoccupancy_PM25);
``````

What is that line going to accomplish?

I think you need to post some complete code. The context of those lines and the definitions of those variables might be pretty important.

You should care more for the conversion from int to float. Most probably this code

``````   Vout1LowRatio = float(((lowpulseoccupancy_PM25) / (lowpulseoccupancy_PM25 + highpulseoccupancy_PM25))*100); // Integer percentage 0=>100
``````

will yield zero. Try change it to

``````   Vout1LowRatio = lowpulseoccupancy_PM25 * 100.0 / (lowpulseoccupancy_PM25 + highpulseoccupancy_PM25); // Integer percentage 0=>100
``````

You also should make sure that the same number of low and high samples are used in the calculations. If the duty cycles do not vary frequently, it will be sufficient to take only one sample, then calculate the duty cycle.