Instantly share code, notes, and snippets.
Last activeAugust 29, 2015 14:14
Save youpy/ea61f502d4ff772dabc8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
constdouble THRESHOLD =64.0;// +2 | |
// http://makers-with-myson.blog.so-net.ne.jp/2014-04-05 | |
unsignedint on[] = {2552,2680,824,860,820,860,824,1892,852,1868,824,1896,824,1892,828,856,828,856,848,53728,2552,2672,828,860,824,856,824,1896,852,1864,824,1896,824,1896,852,828,828,856,828,53840,2560,2672,828,856,848,836,824,1892,824,1892,828,1892,852,1868,852,832,848,832,828}; | |
unsignedint off[] = {2552,2676,852,832,824,1896,848,832,828,856,828,1892,824,856,852,1868,824,1892,852,53944,2556,2676,824,860,820,1896,828,856,828,852,848,1872,824,856,828,1896,824,1892,824,53844,2552,2676,852,832,824,1896,848,836,824,856,828,1892,824,860,820,1896,828,1888,848}; | |
voidsetup() { | |
Serial.begin(115200); | |
pinMode(12, OUTPUT); | |
analogReference(INTERNAL); | |
} | |
voidsendSignal(unsignedint *values,int dataSize) { | |
for(int i =0; i < dataSize; ++i) { | |
unsignedlong len = values[i]; | |
unsignedlong now =micros(); | |
do { | |
digitalWrite(12,1 - i &1); | |
delayMicroseconds(8); | |
digitalWrite(12,0); | |
delayMicroseconds(7); | |
}while(long(now + len -micros()) >0); | |
} | |
} | |
voidloop() { | |
int sensorValue =analogRead(A0); | |
double c =temperature(sensorValue) -1; | |
Serial.println(c); | |
Serial.println(prob(THRESHOLD, c)); | |
if(random(100) >prob(THRESHOLD, c) *100) { | |
Serial.println("on"); | |
sendSignal(on,sizeof(on)); | |
}else { | |
Serial.println("off"); | |
sendSignal(off,sizeof(off)); | |
} | |
delay(1000); | |
} | |
doubletemperature(double value) { | |
double ref =1.1 /1024 *1000; | |
return ((value * ref) /10.0) -2.0; | |
} | |
doubleprob(double mx,double cur) { | |
returnpow(2.0,10 * (cur / mx -1.0)); | |
} |
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment