• This looks much better:

    var dht={};
    var watch;
    var i=0;
    var out=0;
    var pstart=0;
    var badbits=0;
    dht.read = function (a,pin) {
    	dht.onreadf=a;
    	if (pin===undefined) {pin=C6;}
    	i=0;
    	out=0;
    	badbits=0;
    	digitalPulse(pin,1,0.08);
    	setTimeout(function() {pinMode(pin,'input_pullup');watch=setWa­tch(function(t) {dht.onwatch(t);},pin,{repeat:true});},0­.07);
    	setTimeout(function() {dht.onread(dht.endRead());},50);
    }
    dht.onread= function(d) {
    	if (d.temp==-1) {
    		dht.read(dht.onreadf);
    	} else {
    		dht.onreadf(d);
    	}
    }
    dht.onwatch = function(t) {
    	if (t.state) {
    		pstart=t.time
    	} else {
    		var tt=t.time-pstart;
    		if (tt < 0.000044) {
    			badbits = 1;
    		}
    		if (badbits) {
    			out=(out<<1) | ((tt > 0.000044) && (tt < 0.0001));
    		}
    		i++;
    	}
    }
    dht.endRead = function() {
    	clearWatch(watch);
    	if (badbits && i > 32) {
    		rh=(out>>(i-10))&0xFF;
    		temp=(out>>(i-26))&0xFF;
    		return {"temp":temp,"rh":rh};
    	} else {
    		return {temp:-1,rh:-1};
    	}
    }
    

    Any other cleanup suggestions?

About

Avatar for DrAzzy @DrAzzy started