daily pastebin goal
8%
SHARE
TWEET

ES Project

a guest May 25th, 2018 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int button = 2;
  2. int buttonState;
  3.  
  4. //=========================
  5.  
  6. int redLed = 7;
  7. int greenLed = 8;
  8.  
  9. int rgbInR = 5;
  10. int rgbInG = 4;
  11. int rgbInB = 3;
  12.  
  13. int rgbOutR = 10;
  14. int rgbOutG = 9;
  15. int rgbOutB = 11;
  16.  
  17. int buzzer = 13;
  18. //=====================
  19.  
  20. int inTemp = A0;
  21. int outTemp = A1;
  22. int inHumid = A2;
  23. int outHumid = A3;
  24.  
  25. //=========================
  26.  
  27. int incomingLow=0, incomingHigh=0;
  28.  
  29. #define OFF_STATE 0
  30. #define ON_STATE 1
  31. int systemState;
  32.  
  33. void setup()
  34. {
  35.   Serial.begin(9600);
  36.  
  37.   pinMode(13, OUTPUT);
  38.   pinMode(button, INPUT);
  39.  
  40.   pinMode(redLed, OUTPUT);
  41.   pinMode(greenLed, OUTPUT);
  42.  
  43.   systemState = 0;
  44.   buttonState = 0;
  45. }
  46.  
  47. void loop()
  48. {
  49.   if (buttonPressed()){
  50.     buttonState = 1;
  51.     systemState = nextState();
  52.   }
  53.   delay(500);
  54.  
  55.   if(Serial.peek()!=-1){
  56.   incomingLow = Serial.read();
  57.   incomingHigh = Serial.read();
  58.   }
  59.  
  60.   handleSystemState();
  61.     //Serial.println(readTempInCelsius(10, outTemp));
  62.  
  63. }
  64. //=========================
  65.  
  66. void handleSystemState(){
  67.   switch(systemState){
  68.     case OFF_STATE:
  69.       digitalWrite(redLed, HIGH);
  70.       digitalWrite(greenLed, LOW);
  71.       rgbWrite(5,0,4,0,3,0);
  72.       rgbWrite(10,0,9,0,6,0);
  73.       break;
  74.     case ON_STATE:
  75.       digitalWrite(redLed, LOW);
  76.       digitalWrite(greenLed, HIGH);
  77.     float outTemperature = getTemp(outTemp);
  78.     if ( outTemperature >=0 && outTemperature<=50){
  79.      
  80.       rgbWrite(rgbOutR,0,rgbOutG,255,rgbOutB,0);
  81.     } else
  82.       rgbWrite(rgbOutR,255, rgbOutG,0,rgbOutB,0);
  83.  
  84.     float inTemperature = getTemp(inTemp);
  85.    
  86.     if (inTemperature >=0 && inTemperature <=50){
  87.       rgbWrite(rgbInR,0,rgbInG,255, rgbInB, 255);
  88.     }else {
  89.       rgbWrite(rgbInR,255, rgbInG,0,rgbInB,0);
  90.     }
  91.     Serial.println(getTemp(outTemp));
  92.     Serial.println(getTemp(inTemp));
  93.     Serial.println(getHumidity(outHumid));
  94.     Serial.println(getHumidity(inHumid));
  95.     break;
  96.   }
  97. }
  98.  
  99. //=========================
  100.  
  101. void rgbWrite(int rPin, int rInt,
  102.               int gPin, int gInt,
  103.               int bPin, int bInt
  104. ){
  105.   analogWrite(rPin, rInt);
  106.   analogWrite(gPin, gInt);
  107.   analogWrite(bPin, bInt);
  108. }
  109.  
  110. int nextState(){
  111.   switch(systemState){
  112.     case OFF_STATE: return ON_STATE;
  113.     case ON_STATE: return OFF_STATE;
  114.     default: return -1;
  115.   }
  116. }
  117.  
  118. int buttonPressed(){
  119.   int btn_state = digitalRead(button);
  120.   if (btn_state == LOW){
  121.     buttonState = 0;
  122.     return 0;
  123.   }
  124.  
  125.   return ((btn_state == HIGH) != buttonState);
  126. }
  127.  
  128. float getTemp(int pin){
  129.     return readTempInCelsius(10,pin);
  130. }
  131.  
  132.  
  133.  
  134.  
  135. double Thermistor(int RawADC) {
  136. double Temp;  // Working variable
  137.   Temp = log(((10240000 / RawADC) - 10000));
  138.   Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
  139.   Temp = Temp - 273.15;            // Convert Kelvin to Celcius
  140.   //Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
  141.   return Temp;
  142. }
  143.  
  144.  
  145. int oldV;
  146. int getHumidity(int pin){
  147.   int v = analogRead(pin);
  148.  
  149.   v = smooth(v, 0.99, oldV);
  150.  
  151.   oldV = v;
  152.   delay(2);
  153.   return v;
  154. }
  155.  
  156.  
  157.  
  158.  
  159. int smooth(int data, float filterVal, float smoothedVal) {
  160.   if (filterVal > 1){      // check to make sure param's are within range
  161.     filterVal = .99;
  162.   }
  163.  
  164.   else if (filterVal <= 0){
  165.     filterVal = 0;
  166.   }
  167.  
  168.   smoothedVal = (data * (1 - filterVal)) + (smoothedVal  *  filterVal);
  169.   return (int)smoothedVal;
  170.  
  171. }
  172.  
  173. float readTempInCelsius(int count, int pin) {
  174.   float temperaturaMediata = 0;
  175.   float sumaTemperatura = 0;
  176.   for (int i =0; i < count; i++) {
  177.     int reading = analogRead(pin);
  178.     float voltage = reading * 5.0;
  179.     voltage /= 1024.0;
  180.     float temperatureCelsius = (voltage - 0.5) * 100 ;
  181.     sumaTemperatura = sumaTemperatura + temperatureCelsius;
  182.   }
  183.   return sumaTemperatura / (float)count;
  184. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top