Advertisement
bld

Untitled

bld
Mar 3rd, 2014
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | None | 0 0
  1. #define relayPin    D0              // Set what pin the relay is connected to
  2. #define relayInvert 0               // Set to 1 to invert the function of the relay
  3.  
  4. float targetTemp = 24.0;            // Target temperature
  5. float targetHyst = 0.05;            // Hysteresis determinig how much over and under target temp it should trigger
  6.  
  7.  
  8. #define arraySize   20              // Size of the arry holding values for averaging
  9. double temps[arraySize];            // Arry holding temperature readings
  10. double temperature = 0;             // Current average temperature
  11. int idx = 0;                        // Index of where we are in the array
  12.  
  13. unsigned long readTMP36last = 0;    // Holding time of when temperature was last checked
  14. #define readTMP36trot 500           // Time in milliseconds between temperature readings
  15.  
  16. char tempStr[16];                   // Holding the result in the json api reply
  17.  
  18.  
  19. void setup()
  20. {
  21.     pinMode(relayPin, OUTPUT);  // Set pin for the relay to output
  22.     Spark.variable("temperature", &tempStr, STRING);    // Register variable so it can be accessed with the api later
  23. }
  24.  
  25. void loop()
  26. {
  27.     readTMP36();    // Read from the temperature sensor and set the temperature variable
  28.  
  29.     if (temperature < (targetTemp-targetHyst)) // If the temperature is below our target temperature-hysteresis then...
  30.     {
  31.         digitalWrite(relayPin, relayInvert?LOW:HIGH); // Set pin high, or low if function is inverted
  32.     }
  33.     else if (temperature > targetTemp+targetHyst) // If the temperature is ablove target temperature+hysteresis then..
  34.     {
  35.         digitalWrite(relayPin, relayInvert?HIGH:LOW); // Set pin to low, or high if function is inverted
  36.     }
  37.    
  38.     sprintf(tempStr, "%4.2f", temperature); // Set the result we are going to reply with when asked by the api (returning temperature with two decimals)
  39. }
  40.  
  41. void readTMP36() // Function for reading from the sensor
  42. {
  43.     unsigned int now = millis(); // Set the current time (from when the controller was powered on)
  44.    
  45.     if (now - readTMP36last < readTMP36trot) return; // If less than 500ms (default) has passed, just stop here and return to loop()
  46.  
  47.     readTMP36last = now; // More than 500ms (default) has passed, store the time we are reading at so we know when to do it again
  48.    
  49.     temps[idx] = ((((analogRead(A0)*3.3)/4095.0)-0.5)*100.0); // Check analog input and calculate the temperature
  50.    
  51.     if (++idx >= arraySize) idx = 0; // Add one to the array index, and check that we dont go above its size, if we do, set index to 0
  52.    
  53.     double total = 0; // Used to hold total value of the array
  54.     for(int i=0; i < arraySize; i++) // Run through the array holding the temperatures
  55.     {
  56.         total += temps[i]; // Add each temperature in the array to the total
  57.     }
  58.     temperature = total / arraySize; // Set temperature variable to total temperature, divided by the array size, so we get an average temperature
  59. }
  60.  
  61. // The temperature can now be accessed by opening https://api.spark.io/v1/devices/{device id}/temperature?access_token={access token}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement