My girlfreind is right, I am a bonehead sometimes. I was assuming it was an A/D output. Would a hardware solution work? Use the input to pulse a RC circuit (2 resistors and 1 cap). Feed the cap voltage back to another input pin. More pulses (1's) = more voltage across the cap. At some point you would have your digital high on the 2nd input pin. Would make it easy to adjust your trip-point (if you need to do that) on the fly but of course it uses more pins and external hardware. Patrick J wrote: > A/D ?!? I must be missing something here.. Not using any A/Ds... > > ----- Original Message ----- > Subject: Re: [PIC]:pic16F877... howto keep in range ? > > > Just use the upper byte of your A/D and throw the lower one away > > > > Patrick J wrote: > > > > > I have a sensor that gives output simular to this: > > > empty: 000000100000010 > > > midfull: 011100011011101 > > > full: 11101111001111101 > > > As you can see I mostly get 1:s when the level is high. > > > When empty, I get mostly 0:s when the sensor is polled. > > > I want this sensor input to produce a thermostat-like output. > > > ON until 'HOT' (or rather 'full') > > > OFF until 'COLD' (or rather 'empty') > > > > > > My basic idea for this, is to poll the sensor and: > > > if Sensor=1 then X=X+1 > > > if Sensor=0 then X=X-1 > > > Timeconstant: (exact timing, nor speed, is an issue) > > > If Sensor=0, X will count down to min (LOW) in 3 secs > > > If Sensor=1, X will count up to max (HIGH) in 3 secs > > > > > > A pic running at 4MHz polls the sensor at approx 80kHz, if the > > > loop takes 50 cycles. 80000* 3sec gives X will be in the > > > 0-240 000 range. 18 bits math. > > > > > > It would be more convenient to have a 0-255 range instead. > > > 32 bits math is not fun. And takes up much ram for changable limits etc. > > > > > > I have a few ideas on howto do it: > > > - divide X (32 bits) by 1024 and use that as new X (8 bits) > > > - only add 1 when counted 1024 1:s. only sub 1 when counted 1024 0:s > > > - some kind of (moving) average could be used ? > > > > > > My question is, is there a better/simpler way to keep X in a decent range ? > > -- > http://www.piclist.com hint: The list server can filter out subtopics > (like ads or off topics) for you. See http://www.piclist.com/#topics -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details.