Programing error

So I am trying to write a program for controlling a fan whit gas sensor ZP07 MP901. Specification from the sensor is in the attachment. I do know that this sensor is working like this: low concentraton of the gas 1 is pressent for 10ms and 0 is pressent for 90ms, at high concetration 1 is pressent for 90ms and 0 for 10ms. I know I need to mesure time how long 1 is pressent but i just dont know quit how. For controlling I will use analogWrite and PWM.

zp07 mp901.pdf (451 KB)

1 is pressent for 10ms and 0 is pressent for 90ms

Present where? I'm not following what that means.

How often does that cycle repeat?

If you mean that the voltage on a pin is HIGH for some period of time, and LOW for the rest of the time, and the cycle repeats often enough, you could feed that into an analog pin, and get a reading that matches what you'd get reading a PWM pin.

I have connecte a sensor to analog pin A0. The cycle repeats until the sensor its turnned off.

At low contreation the cycle looks like this. 1 present 10ms,0 present 90ms, 1 present 10ms, 0 present
90ms,... goes on

Medium concentration cycle: 1 present 50ms, 0 present 50ms, 1 present 50ms, 0 present 50ms,... goes on

At high concentrain the cycle changes to this: 1 present 90ms, 0 present 10ms, 1 present 90ms, 0 present 10ms,... goes on

So whit program i need to messure this time how long the 1 i pressent so arduino can turn on the fan. Fan speed will be determend whit PWM

So whit program i need to messure this time how long the 1 i pressent

The length of time the pin is HIGH is the duty cycle. That influences how long it takes to charge the capacitor that the analogRead() function is using.

Different duty cycles are reflected in the reading that you get from analogRead() already. You do NOT need to add something to time how long the pin is HIGH, and, even if you did, you'd need to know when the pin went HIGH, when it went LOW, AND the length of the cycle.

You'd need an interrupt for that, with CHANGE as the type. In the handler, you'd have to read the state of the pin, and record the time in the appropriate variable (wentHIGH or wentLOW) so that you could calculate the duty cycle.

Or, you could just let the analogRead() function handle it.

With a period of 100ms will analogRead work like that? analogRead takes something like 100us to run. So it seems like you'd get a whole bunch of 1023 or 0 depending on when you called it.

OP, have a look at the pulseIn function. Would that fit the bill?

}
void loop(){
 gas=(analogRead, A0);
 if (gas>=1);
 for                       // time at low concetration cycle 10ms
 analogWrite(FAN, 255);
}

I was thinking of something like that. Now this writen as it is dosent work. It is just how i thgout it could be written

if (gas>=1);
 for                       // time at low concetration cycle 10ms
 analogWrite(FAN, 255);

Doesn't work or doesn't compile?

(Lose the semicolon on the if, BTW)

Its not finnished I dont know how to write this 10ms into the program nex to FOR.

If you look at the reference page for analogRead(), you'd see that the return value would be from 0-1023.
(And be sure to check the reference for if, also.)

So, maybe:

void loop()
{
     gas = analogRead(A0);
     if (gas > 500) digitalWrite(FAN, HIGH);
}

Blocked1025:
Its not finnished I dont know how to write this 10ms into the program nex to FOR.

You only want the fan to run for 10ms?

No i want the program to do: when he reads 1 or more from the sensor for 10ms he turns on the fan on 100% (PWM is 255). I get the second part setting fan on 100% i dont understend how to write the program that will messure time when 1 or more is present at A0(analoginput)

Stop with the 1 and 0 stuff. Do you mean 5V or 0V or do you mean analogRead gets 1 which is at a little less than 5mV. If it is really 5mV then you're going to probably need extra circuitry because you might pick that up just from noise.

I note that this thread has 11 replies but the datasheet provided was only downloaded twice.

I think this is a perfect application for the pulseIn() function. Read that reference and try to make that work.

No, I won't give private help. Ask your question in public.

MorganS:
No, I won't give private help. Ask your question in public.

I hate when they do that.