Skip to content

Instantly share code, notes, and snippets.

@youpy
Last active August 29, 2015 14:14
Show Gist options
  • Save youpy/ea61f502d4ff772dabc8 to your computer and use it in GitHub Desktop.
Save youpy/ea61f502d4ff772dabc8 to your computer and use it in GitHub Desktop.
const double THRESHOLD = 64.0; // +2
// http://makers-with-myson.blog.so-net.ne.jp/2014-04-05
unsigned int 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};
unsigned int 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};
void setup() {
Serial.begin(115200);
pinMode(12, OUTPUT);
analogReference(INTERNAL);
}
void sendSignal(unsigned int *values, int dataSize) {
for(int i = 0; i < dataSize; ++i) {
unsigned long len = values[i];
unsigned long now = micros();
do {
digitalWrite(12, 1 - i & 1);
delayMicroseconds(8);
digitalWrite(12, 0);
delayMicroseconds(7);
} while(long(now + len - micros()) > 0);
}
}
void loop() {
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);
}
double temperature(double value) {
double ref = 1.1 / 1024 * 1000;
return ((value * ref) / 10.0) - 2.0;
}
double prob(double mx, double cur) {
return pow(2.0, 10 * (cur / mx - 1.0));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment