Guest User

Untitled

a guest
Jan 19th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.80 KB | None | 0 0
  1. #include <LiquidCrystal.h>
  2.  
  3. #define TEMPERATURE_PIN   0
  4. #define LIGHT_PIN         1
  5. #define INDICATOR_PIN     13
  6. #define SWITCH_PIN        8
  7.  
  8. #define CLOCK_PIN 2
  9. #define RESET_PIN 3
  10.  
  11. const float Uin = 5.0;
  12.  
  13. #define MODE_CURRENT    1
  14. #define MODE_MINMAX     2
  15.  
  16. volatile short g_displayMode = MODE_CURRENT;
  17.  
  18. typedef struct {
  19.    float minimum;
  20.    float maximum;
  21.    float current;
  22. } MinMaxCurr;
  23.  
  24. MinMaxCurr temperature = { 100.0, 0.0, 0.0 };
  25. MinMaxCurr lighting = { 100.0, 0.0, 0.0 };
  26.  
  27. #define NUMTEMPS 25
  28. 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 };
  29. 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 };
  30.  
  31.  
  32. LiquidCrystal lcd(4, 5, 9, 10, 11, 12);
  33.  
  34. void setup() {
  35.   lcd.begin(16, 2);
  36.   Serial.begin(9600);  
  37.  
  38.   pinMode(RESET_PIN, OUTPUT);
  39.   pinMode(CLOCK_PIN, OUTPUT);  
  40.  
  41.   pinMode(INDICATOR_PIN, OUTPUT);
  42.   pinMode(SWITCH_PIN, INPUT);
  43. }
  44.  
  45. float interpolate(int *inputs, int *outputs, int count, int input)
  46. {
  47.    for ( int i=0; i<count-1; i++ )
  48.    {
  49.      int prev = inputs[i], next = inputs[i+1];
  50.      
  51.      if ( ( input <= next && input >= prev ) || ( input >= next && input <= prev ) )
  52.      {            
  53.            int value = map( input,
  54.                             prev, next,
  55.                             outputs[i]*100, outputs[i+1]*100);              
  56.  
  57.            return (float)value/100.0;
  58.      }      
  59.    }
  60.      
  61.    return 0;
  62. }
  63.  
  64. int getAverageReading(short pin, short count)
  65. {
  66.   //get an average of five sensor readings
  67.   int val = 0;
  68.   for ( int i=0; i<count; i++ ) {
  69.     val += analogRead(pin);
  70.     delay(5);
  71.   }
  72.   return val/count;  
  73. }
  74.  
  75. float getTemperature()
  76. {
  77.   //some constants
  78.   const int nominalR = 9810;
  79.   const int R25 = 10000;  
  80.  
  81.   //get average reading
  82.   int val = getAverageReading(TEMPERATURE_PIN, 5);
  83.  
  84.   //calculate result voltage and divided resitance
  85.   float Uout = Uin*val/1024;
  86.   int R = (nominalR * Uout) / (Uin - Uout);
  87.  
  88.   //calculate ratio between our resistance and nominal resistance at 25C
  89.   float RtR25 = (float)R/(float)R25;
  90.  
  91.   //interpolate resulting value using temperature table
  92.   return interpolate(ratios, temps, NUMTEMPS, RtR25*1000);
  93. }
  94.  
  95. float getLighting()
  96. {
  97.   //some constants
  98.   const int nominalR = 9790;
  99.   const long maxResistance = 500E3;
  100.   const float maxLightValue = 7.20;
  101.  
  102.   //get an average reading
  103.   int val = getAverageReading(LIGHT_PIN, 2);
  104.  
  105.   //calculate voltage and divided resistance
  106.   float Uout = Uin*val/1024;
  107.   float R = (nominalR * Uout) / (Uin - Uout);
  108.    
  109.   //cap values
  110.   R = min(max(R, 1e-6), maxResistance);
  111.    
  112.   //calculate logarithmic percentage
  113.   float light = -log(R/maxResistance)/maxLightValue;
  114.  
  115.   return min(max(light, 0.0), 1.0);
  116. }
  117.  
  118. void switchMode()
  119. {
  120.   if ( g_displayMode == MODE_CURRENT ) {
  121.      g_displayMode = MODE_MINMAX;
  122.   } else {
  123.      g_displayMode = MODE_CURRENT;    
  124.   }
  125.  
  126. //  static int cnt = 0;
  127.  
  128. //  Serial.print("switch = ");
  129. //  Serial.print(g_displayMode);
  130. //  Serial.print(", ");
  131. //  Serial.println(cnt);  
  132. }
  133.  
  134. void evaluateMinMax(void *mm, float value)
  135. {
  136.   MinMaxCurr *mmStruct = (MinMaxCurr *)mm;
  137.  
  138.   if ( value < mmStruct->minimum ) {
  139.      mmStruct->minimum = value;
  140.   }
  141.   if ( value > mmStruct->maximum ) {
  142.      mmStruct->maximum = value;
  143.   }
  144. }
  145.  
  146. void updateDisplay()
  147. {
  148.   if ( g_displayMode == MODE_CURRENT ) {
  149.       displayCurrentValues();
  150.   } else {
  151.       displayMinMaxValues();
  152.   }
  153. }
  154.  
  155. void updateValues()
  156. {
  157.    temperature.current = getTemperature();
  158.    evaluateMinMax(&temperature, temperature.current);
  159.    
  160.    lighting.current = getLighting()*100;
  161.    evaluateMinMax(&lighting, lighting.current);  
  162. }
  163.  
  164. void displayCurrentValues()
  165. {
  166.    //print label "Temp:"
  167.   lcd.setCursor(0, 0);
  168.   lcd.print("Te\xBC\xBE: ");
  169.  
  170.   //print temperature
  171.   lcd.print(temperature.current);
  172.   lcd.print("\x99" "C   ");
  173.  
  174.   //print label "Light:"
  175.   lcd.setCursor(0, 1);
  176.   lcd.print("C" "\xB3" "e" "\xBF" ": ");
  177.  
  178.   //print light percentage
  179.   lcd.print((int)lighting.current);
  180.   lcd.print("%   ");
  181. }
  182.  
  183. void displayMinMaxValues()
  184. {
  185.  //temperature
  186.  lcd.setCursor(1, 0);
  187.  lcd.print("\x86 ");  
  188.  lcd.print((int)temperature.minimum);  
  189.  lcd.print("\x99" "C  ");  
  190.  
  191.  lcd.setCursor(9, 0);
  192.  lcd.print("\x87 ");  
  193.  lcd.print((int)temperature.maximum);  
  194.  lcd.print("\x99" "C  ");  
  195.  
  196.  //lighting
  197.  lcd.setCursor(1, 1);
  198.  lcd.print("\x86 ");  
  199.  lcd.print((int)lighting.minimum);  
  200.  lcd.print("%  ");  
  201.  
  202.  lcd.setCursor(9, 1);
  203.  lcd.print("\x87 ");  
  204.  lcd.print((int)lighting.maximum);  
  205.  lcd.print("%  ");  
  206.  
  207. }
  208.  
  209. int lastButtonState = 0;
  210.  
  211. void loop()
  212. {
  213.   static boolean lastButtonState = 0;
  214.  
  215.   boolean reading = digitalRead(SWITCH_PIN);
  216.   delay(40);
  217.   boolean readingAgain = digitalRead(SWITCH_PIN);
  218.  
  219.   if ( reading == readingAgain ) //normal state
  220.   {
  221.     boolean needsUpdating = false;
  222.    
  223.      if ( reading != lastButtonState ) //&& lastButtonState == 0 )
  224.      {
  225.        if ( reading == false )
  226.        {
  227.          switchMode();
  228.          needsUpdating = true;
  229.          lcd.clear();
  230.        }
  231.        lastButtonState = reading;
  232.      }
  233.    
  234.      static int cnt = 0;
  235.  
  236.      if ( cnt == 20 || needsUpdating ) {
  237.         updateValues();
  238.         updateDisplay();
  239.        
  240.         showNumber((millis() / 1000) % 10);          
  241.        
  242.         cnt = 0;
  243.      }
  244.      else {
  245.         cnt++;
  246.      }    
  247.   }  
  248. }
  249.  
  250. void resetNumber()
  251. {
  252.     digitalWrite(RESET_PIN, HIGH);
  253.     digitalWrite(RESET_PIN, LOW);
  254. }
  255.  
  256. void showNumber(int n)
  257. {
  258.     resetNumber();
  259.  
  260.     while (n--) {
  261.         digitalWrite(CLOCK_PIN, HIGH);
  262.         digitalWrite(CLOCK_PIN, LOW);
  263.     }
  264. }
Add Comment
Please, Sign In to add comment