Created
May 5, 2011 14:10
-
-
Save jscrane/957100 to your computer and use it in GitHub Desktop.
Arduino sketch for Frankenstein's Thermometer
This file contains 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
#include <math.h> | |
#define THERMISTOR 7 | |
#define SWITCH 1 | |
#define A_PIN 4 | |
const double beta = 3977.0; | |
const double zeroC = 273.15; | |
const double tzero = 25 + zeroC; | |
const int cent = 0; | |
const int fahr = 1; | |
const int abso = 2; | |
int units = cent; | |
void setup(void) | |
{ | |
for (int i = 0; i < 7; i++) | |
pinMode(A_PIN + i, OUTPUT); | |
pinMode(SWITCH, INPUT); | |
} | |
boolean switchedUnits() | |
{ | |
if (!digitalRead(SWITCH)) { | |
if (++units > abso) | |
units = cent; | |
return true; | |
} | |
return false; | |
} | |
void loop(void) | |
{ | |
double rtk = 1.0 / tzero + log(1024.0 / analogRead(THERMISTOR) - 1) / beta; | |
double t = 1.0 / rtk; | |
boolean zero = true; | |
int temp = 0; | |
if (units != abso) { | |
t -= zeroC; | |
if (units == fahr) | |
t = t * 9.0 / 5.0 + 32; | |
} | |
if (t < 0) { | |
t = -t; | |
minus(); | |
if (switchedUnits()) | |
goto units; | |
} | |
temp = (int)(t + 0.5); | |
for (int pow = 100; pow > 0; pow /= 10) { | |
int dig = temp / pow; | |
temp -= dig * pow; | |
zero = zero && (dig == 0); | |
if (!zero || pow == 1) | |
digit(dig); | |
if (switchedUnits()) | |
goto units; | |
} | |
units: | |
if (units == fahr) | |
fahrenheit(); | |
else if (units == cent) | |
centigrade(); | |
else if (units == abso) | |
absolute(); | |
} | |
const int A = 1 << 0; | |
const int B = 1 << 1; | |
const int C = 1 << 2; | |
const int D = 1 << 3; | |
const int E = 1 << 4; | |
const int F = 1 << 5; | |
const int G = 1 << 6; | |
static int digits[] = { | |
A | B | C | D | E | F, | |
B | C, | |
A | B | G | E | D, | |
A | B | G | C | D, | |
F | G | B | C, | |
A | F | G | C | D, | |
A | F | E | G | C | D, | |
A | B | C, | |
A | B | C | D | E | F | G, | |
A | B | C | F | G, | |
}; | |
void digit(int d) | |
{ | |
bits(digits[d]); | |
} | |
void minus() | |
{ | |
bits(G); | |
} | |
void centigrade() | |
{ | |
bits(A | F | E | D); | |
} | |
void fahrenheit() | |
{ | |
bits(A | F | E | G); | |
} | |
void absolute() | |
{ | |
bits(A | B | C | E | F | G); | |
} | |
void bits(int bits) | |
{ | |
int b = 1; | |
for (int i = 0; i < 7; i++) { | |
int pin = A_PIN + i; | |
digitalWrite(pin, LOW); | |
if (bits & b) | |
digitalWrite(pin, HIGH); | |
b *= 2; | |
} | |
delay(750); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment