Advertisement
MicahB9

Arduino Thermostat v_2

Mar 1st, 2016
6,169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. // written by Dylon Jamna, modified by Micah Beeler
  2. // include the library code
  3. #include <LiquidCrystal.h>// include the library code
  4. int tempPin = A0; // make variables// thermistor is at A0
  5. int tup = 7; //Temp up button
  6. int tdown = 9; //Temp down button
  7. int tcf = 8; //F to C toggle
  8. boolean far = true; //Default F to true
  9. boolean cel = false; //Default C to false
  10. boolean lightMe; //Light up LED
  11. char abbrv; //Shortcut for C or F abbreviation
  12. float temp; //Raw temp value
  13. float settemp; //Desired temp
  14. int defF = 65; //Default Far.
  15. int defC = 19; //Default Cel.
  16. int upstate = 0; //Temp up button state
  17. int downstate = 0; //Temp down button state
  18. int tcfstate = 0; //C to F button state
  19. int ledPin = 13; //LED's pin
  20. LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // lcd is at 12,11,5,4,3,2 on the breadboard
  21.  
  22. void setup() {
  23. Serial.begin (9600); // set the serial monitor tx and rx speed
  24. lcd.begin(16, 2); // set up all the "blocks" on the display
  25. lcd.setCursor(0, 0); // set the cursor to colum 0 row 0
  26. lcd.print("hello, world!"); // display hello world for 1 second
  27. lcd.clear(); // clear the lcd
  28. pinMode(tup, INPUT); //Set temp up button to input
  29. pinMode(tdown, INPUT); //Set temp down button to input
  30. pinMode(tcf, INPUT); //Set C to F button to input
  31. pinMode(ledPin, OUTPUT); //Set LED to output
  32. }
  33.  
  34. void loop() {
  35. upstate = digitalRead(tup); //Read state of temp up
  36. downstate = digitalRead(tdown); //Read state of temp down
  37. tcfstate = digitalRead(tcf); //Read state of C to F button
  38. int tvalue = analogRead(tempPin); // make tvalue what ever we read on the tempPin
  39.  
  40. if (tcfstate == LOW) { //If the C to F button is pressed
  41. if (far == true) { //And Farenheit is true already
  42. far = false;
  43. cel = true; //Enable Celsius
  44. }
  45. else if (far == false) { //Otherwise if Farenheit isn't true
  46. far = true; //Enable Farenheit
  47. cel = false;
  48. }
  49. }
  50.  
  51. if (far == true) { //If Farenheit is true
  52. temp = farenheit(tvalue); //Caclulate temp in F
  53. abbrv = 'F'; //Set label to F
  54. }
  55. if (cel == true) { //If Celsius is true
  56. temp = celsius(tvalue); //Calculate temp in C
  57. abbrv = 'C'; //Set label to C
  58. }
  59.  
  60. if (upstate == LOW) { //if up button is pressed,
  61. defF = defF + 1; //raise desired F by 1
  62. defC = defC + 1; //Raise desired C by 1
  63. }
  64. if (downstate == LOW) { //if down button is pressed
  65. defF = defF - 1; //lower desired F by 1
  66. defC = defC - 1; //lower desired C by 1
  67. }
  68.  
  69. lcd.setCursor(0, 0);
  70. lcd.print("Current ");
  71. lcd.print (temp); // Print the current temp in f
  72. lcd.print (abbrv);
  73. delay(255);
  74. if (cel == true) {
  75. lcd.setCursor (0, 1); // set the cursor to 0,1
  76. lcd.print ("Desired "); // Print set to and your ideal temperature in f
  77. lcd.print (defC);
  78. lcd.print (abbrv);
  79. }
  80. if (far == true) {
  81. lcd.setCursor (0, 1); // set the cursor to 0,1
  82. lcd.print ("Desired "); // Print set to and your ideal temperature in f
  83. lcd.print (defF);
  84. lcd.print (abbrv);
  85. }
  86.  
  87. lightUp(temp, defC, defF); //run custom light up command
  88.  
  89. if (lightMe == true) { //if light up is true
  90. digitalWrite(ledPin, HIGH); //turn on LED
  91. } else if (lightMe == false) { //otherwise
  92. digitalWrite(ledPin, LOW); //turn off LED
  93. }
  94. } // we're done
  95.  
  96. float farenheit(float raw) { //calculate far value
  97. float f; //create temporary variable F
  98. f = raw / 2.07; //calculate temperature from raw value
  99. return f; //return temperature
  100. }
  101.  
  102. float celsius(float raw) { //calculate cel value
  103. float c; //create temp variable C
  104. c = raw / 7.1; //calculate temperature from raw value
  105. return c; //return temperature
  106. }
  107.  
  108. boolean lightUp(float act, int desC, int desF) { //Decides if LED should light up, imports current temp, desired C, and desired F
  109. if (act < desC || act < desF) { //if the actual is less than the desired F or C value
  110. lightMe = true; //Turn on the LED
  111. } else { //otherwise
  112. lightMe = false; //Turn the LED off
  113. }
  114. return lightMe; //return that value
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement