Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. #include <TH02_dev.h>
  2. #include "Arduino.h"
  3. #include "Wire.h"
  4.  
  5. const int TIP120pin = 5; //base pin of TIP120 transistor
  6. const int ButtonPWR = 6; //provide 5v to button circuit
  7. const int inPin = 7; //read button circuit status
  8. int BUTTONVAL = 0; //store button circuit status as value
  9. int DRYMODE=0; //determine monitor or dry mode (fan on or off)
  10. int ONCE=0; //ensure FANCONTINUOUS() only runs once per drying cycle (per high humidity)
  11. float LOWREADING=200; //Track historical low RH
  12. float HIGHREADING=0; //Track historical high RH
  13. long startTime ; //Track fan runtime
  14. long elapsedTime ; //Track fan runtime
  15. int FANPREV = 0; //Store if fan has ever run
  16.  
  17. //LCD requirements:
  18.  
  19. // You can use any (4 or) 5 pins
  20. #define sclk 13
  21. #define mosi 11
  22. #define cs 10
  23. #define rst 9
  24. #define dc 8
  25.  
  26. // Color definitions for LCD display
  27. #define BLACK 0x0000
  28. #define BLUE 0x001F
  29. #define RED 0xF800
  30. #define CYAN 0x07FF
  31. #define MAGENTA 0xF81F
  32. #define YELLOW 0xFFE0
  33. #define WHITE 0xFFFF
  34.  
  35. #include <Adafruit_GFX.h>
  36. #include <Adafruit_SSD1331.h>
  37. #include <SPI.h>
  38.  
  39. Adafruit_SSD1331 display = Adafruit_SSD1331(cs, dc, mosi, sclk, rst);
  40.  
  41. void setup()
  42. {
  43. //Button setup
  44. pinMode(inPin, INPUT); // declare pushbutton as input
  45. pinMode(ButtonPWR,OUTPUT); //provide power to button
  46.  
  47. Serial.begin(9600); // start serial for output
  48.  
  49. //Temp+humid requirements
  50. Serial.println("****THO2 power up delay, letting voltage settle****\n");
  51. /* Power up,delay 150ms,until voltage is stable */
  52. delay(150);
  53. /* Reset HP20x_dev */
  54. TH02.begin();
  55. delay(100);
  56.  
  57. /* Determine TH02_dev is available or not */
  58. Serial.println("TH02_dev is available.\n");
  59.  
  60. //TIP120 Transistor base pin as OUTPUT
  61. pinMode(TIP120pin, OUTPUT);
  62.  
  63. //LCD Display requirements
  64. display.begin();
  65. display.fillScreen(BLACK);
  66. display.setCursor(0,0);
  67. display.print("Glove Dryer V.1");
  68. delay(1000);
  69. display.fillScreen(BLACK);
  70. }
  71.  
  72. void loop()
  73. {
  74. //Button to activate LCD display
  75. digitalWrite(ButtonPWR, HIGH);
  76. BUTTONVAL = digitalRead(inPin); // read button input value
  77. if (BUTTONVAL == LOW)
  78. {
  79. LCD_DISPLAY();
  80. }
  81.  
  82. float humidity = TH02.ReadHumidity();
  83.  
  84. //Track historical high and low readings
  85.  
  86. if (humidity > HIGHREADING)
  87. {
  88. HIGHREADING=humidity;
  89. }
  90. if (humidity < LOWREADING)
  91. {
  92. LOWREADING=humidity;
  93. }
  94.  
  95. //Trigger events based on humidity level
  96. if (humidity >= 85)
  97. {
  98. Serial.print("Humidity: ");
  99. Serial.print(humidity);
  100. Serial.println("%\r\n");
  101. Serial.println("Fan on");
  102. analogWrite(TIP120pin, 255); // By changing values from 0 to 255 you can control motor speed
  103. if ( FANPREV != 1 && DRYMODE != 1)
  104. {
  105. startTime = millis(); //Store the fan start time once per drying cycle (per high RH)
  106. }
  107. FANPREV = 1; //Record that fan has run (used in LCD_DISPLAY()
  108. DRYMODE=1; //Signify drymode; fan should be running
  109. }
  110. else if (humidity <= 80)
  111. {
  112. Serial.print("Humidity: ");
  113. Serial.print(humidity);
  114. Serial.println("%\r\n");
  115. Serial.println("Fan off");
  116. while(ONCE < 1 && DRYMODE == 1 )
  117. {
  118. ONCE++;
  119. FANCONTINUOUS();
  120. }
  121. analogWrite(TIP120pin, 0); // Fan off
  122. DRYMODE=0;
  123. }
  124. delay(1000);
  125. }
  126.  
  127. //FUNCTIONS
  128.  
  129. void FANCONTINUOUS()
  130. {
  131. int count=0;
  132. float humidity = TH02.ReadHumidity();
  133.  
  134. while(count < 300){
  135. if (BUTTONVAL == LOW)
  136. {
  137. LCD_DISPLAY();
  138. }
  139. analogWrite(TIP120pin, 255);
  140. Serial.print("Humidity: ");
  141. Serial.print(humidity);
  142. Serial.println("%\r\n");
  143. Serial.println("Fan on CONTINUOUS");
  144. delay(1000);
  145. count++;
  146. }
  147. }
  148.  
  149. void LCD_DISPLAY()
  150. {
  151. float humidity = TH02.ReadHumidity();
  152. display.setCursor(0,0);
  153. display.print("Humid:");
  154. display.setCursor(50,0);
  155. if (humidity >= 85)
  156. {
  157. display.setTextColor(RED);
  158. }
  159. display.print(humidity);
  160. display.setTextColor(WHITE);
  161. display.setCursor(80,0);
  162. display.print("%");
  163. display.setCursor(0,20);
  164. display.print("Mode:");
  165. if (DRYMODE == 1 && ONCE == 0)
  166. {
  167. display.setCursor(50,20);
  168. display.print("DRY");
  169. }
  170. else if (ONCE > 0)
  171. {
  172. display.setCursor(50,20);
  173. display.print("CONT");
  174. }
  175. else if (ONCE == 0 && DRYMODE != 1)
  176. {
  177. display.setCursor(50,20);
  178. display.print("MONITOR");
  179. }
  180. delay(1500);
  181. display.fillScreen(BLACK);
  182. display.setCursor(0,0);
  183. display.print("High:");
  184. display.setCursor(50,0);
  185. display.print(HIGHREADING);
  186. display.print("%");
  187. display.setCursor(0,20);
  188. display.print("Low:");
  189. display.setCursor(50,20);
  190. display.print(LOWREADING);
  191. display.print("%");
  192. if (FANPREV == 1)
  193. {
  194. display.setCursor(0,40);
  195. display.print("Time:");
  196. display.setCursor(50,40);
  197. elapsedTime = millis() - startTime;
  198. if ( (int)(elapsedTime / 1000L) > 100 )
  199. {
  200. elapsedTime = millis() - startTime;
  201. display.print( (int)(elapsedTime / 60000L)); //convert to minutes
  202. display.print(" mins");
  203. }
  204. else
  205. {
  206. elapsedTime = millis() - startTime;
  207. display.print( (int)(elapsedTime / 1000L));
  208. display.print(" secs");
  209. }
  210. }
  211. delay(1500);
  212. display.fillScreen(BLACK);
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement