Operational.ino (2208B)
1 #include <hd44780.h> 2 #include <Wire.h> 3 #include <hd44780ioClass/hd44780_I2Cexp.h> 4 hd44780_I2Cexp lcd; 5 #define THERMISTORNOMINAL 100000 6 #define TEMPERATURENOMINAL 25 7 #define NUMSAMPLES 2 8 #define BCOEFFICIENT 4000 9 #define SERIESRESISTOR 100000 10 11 int red_light_pin= 12; 12 int green_light_pin = 10; 13 int blue_light_pin = 11; 14 int samples[NUMSAMPLES]; 15 16 void setup() { 17 lcd.begin(16, 2); 18 lcd.backlight(); 19 Serial.begin(9600); 20 analogReference(EXTERNAL); 21 pinMode(red_light_pin, OUTPUT); 22 pinMode(green_light_pin, OUTPUT); 23 pinMode(blue_light_pin, OUTPUT); 24 } 25 26 void loop() { 27 float first = voltage(0), second = voltage(1); 28 float bridge = (first + second) / 2; 29 float volts = bridge * (3.3 / 1023.0); 30 float res = resistance(bridge); 31 float temp = celsius(res); 32 lcd.setCursor(5, 0); 33 lcd.print(String(temp) + "C"); 34 lcd.setCursor(5, 1); 35 lcd.print(String(volts) + "V"); 36 if (temp < 0.0) { 37 digitalWrite(blue_light_pin, LOW); 38 digitalWrite(green_light_pin, LOW); 39 digitalWrite(red_light_pin, LOW); 40 } else if (temp < 21.0) { 41 digitalWrite(blue_light_pin, HIGH); 42 digitalWrite(green_light_pin, LOW); 43 digitalWrite(red_light_pin, LOW); 44 } else if (temp < 52.0) { 45 digitalWrite(blue_light_pin, LOW); 46 digitalWrite(green_light_pin, HIGH); 47 digitalWrite(red_light_pin, LOW); 48 } else if (temp <= 75.0) { 49 digitalWrite(blue_light_pin, LOW); 50 digitalWrite(green_light_pin, LOW); 51 digitalWrite(red_light_pin, HIGH); 52 } 53 delay(1000); 54 lcd.clear(); 55 } 56 57 float voltage(int pin) { 58 uint8_t i; 59 float volt; 60 for (i=0; i< NUMSAMPLES; i++) { 61 samples[i] = analogRead(pin); 62 delay(10); 63 } 64 volt = 0; 65 for (i=0; i< NUMSAMPLES; i++) { 66 volt += samples[i]; 67 } 68 volt /= NUMSAMPLES; 69 return volt; 70 } 71 72 float resistance(float volts) { 73 volts = 1023 / volts - 1; 74 volts = SERIESRESISTOR / volts; 75 return volts; 76 } 77 78 float celsius(float res) { 79 float celsius = res / THERMISTORNOMINAL; 80 celsius = log(celsius); 81 celsius /= BCOEFFICIENT; 82 celsius += 1.0 / (TEMPERATURENOMINAL + 273.15); 83 celsius = 1.0 / celsius; 84 celsius -= 273.15; 85 return celsius; 86 }