Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- #define TEMPERATURE_PIN 0
- #define LIGHT_PIN 1
- #define INDICATOR_PIN 13
- #define SWITCH_PIN 8
- #define CLOCK_PIN 2
- #define RESET_PIN 3
- const float Uin = 5.0;
- #define MODE_CURRENT 1
- #define MODE_MINMAX 2
- volatile short g_displayMode = MODE_CURRENT;
- typedef struct {
- float minimum;
- float maximum;
- float current;
- } MinMaxCurr;
- MinMaxCurr temperature = { 100.0, 0.0, 0.0 };
- MinMaxCurr lighting = { 100.0, 0.0, 0.0 };
- #define NUMTEMPS 25
- int temps[NUMTEMPS] = { -35, -30, -25, -20, -15, -10, -5, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85 };
- int ratios[NUMTEMPS] = { 29947, 21560, 15640, 11460, 8451, 6292, 4707, 3556, 2711, 2086, 1620, 1268, 1000, 794, 632, 507, 410, 333, 272, 223, 184, 153, 127, 106, 89 };
- LiquidCrystal lcd(4, 5, 9, 10, 11, 12);
- void setup() {
- lcd.begin(16, 2);
- Serial.begin(9600);
- pinMode(RESET_PIN, OUTPUT);
- pinMode(CLOCK_PIN, OUTPUT);
- pinMode(INDICATOR_PIN, OUTPUT);
- pinMode(SWITCH_PIN, INPUT);
- }
- float interpolate(int *inputs, int *outputs, int count, int input)
- {
- for ( int i=0; i<count-1; i++ )
- {
- int prev = inputs[i], next = inputs[i+1];
- if ( ( input <= next && input >= prev ) || ( input >= next && input <= prev ) )
- {
- int value = map( input,
- prev, next,
- outputs[i]*100, outputs[i+1]*100);
- return (float)value/100.0;
- }
- }
- return 0;
- }
- int getAverageReading(short pin, short count)
- {
- //get an average of five sensor readings
- int val = 0;
- for ( int i=0; i<count; i++ ) {
- val += analogRead(pin);
- delay(5);
- }
- return val/count;
- }
- float getTemperature()
- {
- //some constants
- const int nominalR = 9810;
- const int R25 = 10000;
- //get average reading
- int val = getAverageReading(TEMPERATURE_PIN, 5);
- //calculate result voltage and divided resitance
- float Uout = Uin*val/1024;
- int R = (nominalR * Uout) / (Uin - Uout);
- //calculate ratio between our resistance and nominal resistance at 25C
- float RtR25 = (float)R/(float)R25;
- //interpolate resulting value using temperature table
- return interpolate(ratios, temps, NUMTEMPS, RtR25*1000);
- }
- float getLighting()
- {
- //some constants
- const int nominalR = 9790;
- const long maxResistance = 500E3;
- const float maxLightValue = 7.20;
- //get an average reading
- int val = getAverageReading(LIGHT_PIN, 2);
- //calculate voltage and divided resistance
- float Uout = Uin*val/1024;
- float R = (nominalR * Uout) / (Uin - Uout);
- //cap values
- R = min(max(R, 1e-6), maxResistance);
- //calculate logarithmic percentage
- float light = -log(R/maxResistance)/maxLightValue;
- return min(max(light, 0.0), 1.0);
- }
- void switchMode()
- {
- if ( g_displayMode == MODE_CURRENT ) {
- g_displayMode = MODE_MINMAX;
- } else {
- g_displayMode = MODE_CURRENT;
- }
- // static int cnt = 0;
- // Serial.print("switch = ");
- // Serial.print(g_displayMode);
- // Serial.print(", ");
- // Serial.println(cnt);
- }
- void evaluateMinMax(void *mm, float value)
- {
- MinMaxCurr *mmStruct = (MinMaxCurr *)mm;
- if ( value < mmStruct->minimum ) {
- mmStruct->minimum = value;
- }
- if ( value > mmStruct->maximum ) {
- mmStruct->maximum = value;
- }
- }
- void updateDisplay()
- {
- if ( g_displayMode == MODE_CURRENT ) {
- displayCurrentValues();
- } else {
- displayMinMaxValues();
- }
- }
- void updateValues()
- {
- temperature.current = getTemperature();
- evaluateMinMax(&temperature, temperature.current);
- lighting.current = getLighting()*100;
- evaluateMinMax(&lighting, lighting.current);
- }
- void displayCurrentValues()
- {
- //print label "Temp:"
- lcd.setCursor(0, 0);
- lcd.print("Te\xBC\xBE: ");
- //print temperature
- lcd.print(temperature.current);
- lcd.print("\x99" "C ");
- //print label "Light:"
- lcd.setCursor(0, 1);
- lcd.print("C" "\xB3" "e" "\xBF" ": ");
- //print light percentage
- lcd.print((int)lighting.current);
- lcd.print("% ");
- }
- void displayMinMaxValues()
- {
- //temperature
- lcd.setCursor(1, 0);
- lcd.print("\x86 ");
- lcd.print((int)temperature.minimum);
- lcd.print("\x99" "C ");
- lcd.setCursor(9, 0);
- lcd.print("\x87 ");
- lcd.print((int)temperature.maximum);
- lcd.print("\x99" "C ");
- //lighting
- lcd.setCursor(1, 1);
- lcd.print("\x86 ");
- lcd.print((int)lighting.minimum);
- lcd.print("% ");
- lcd.setCursor(9, 1);
- lcd.print("\x87 ");
- lcd.print((int)lighting.maximum);
- lcd.print("% ");
- }
- int lastButtonState = 0;
- void loop()
- {
- static boolean lastButtonState = 0;
- boolean reading = digitalRead(SWITCH_PIN);
- delay(40);
- boolean readingAgain = digitalRead(SWITCH_PIN);
- if ( reading == readingAgain ) //normal state
- {
- boolean needsUpdating = false;
- if ( reading != lastButtonState ) //&& lastButtonState == 0 )
- {
- if ( reading == false )
- {
- switchMode();
- needsUpdating = true;
- lcd.clear();
- }
- lastButtonState = reading;
- }
- static int cnt = 0;
- if ( cnt == 20 || needsUpdating ) {
- updateValues();
- updateDisplay();
- showNumber((millis() / 1000) % 10);
- cnt = 0;
- }
- else {
- cnt++;
- }
- }
- }
- void resetNumber()
- {
- digitalWrite(RESET_PIN, HIGH);
- digitalWrite(RESET_PIN, LOW);
- }
- void showNumber(int n)
- {
- resetNumber();
- while (n--) {
- digitalWrite(CLOCK_PIN, HIGH);
- digitalWrite(CLOCK_PIN, LOW);
- }
- }
Add Comment
Please, Sign In to add comment