Guest User

Untitled

a guest
Nov 18th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.15 KB | None | 0 0
  1.  
  2. #include <LiquidCrystal.h>
  3.  
  4. #define DEBUG 1
  5.  
  6. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  7. int Temp;
  8. int Light, Humidity, hCheck;
  9. boolean Tilt;
  10.  
  11. boolean doTemp = true;
  12. boolean doLight = true;
  13. boolean doHumi = true;
  14. boolean doTilt = true;
  15.  
  16. boolean colonOn = false;
  17. int dtime = 1;
  18.  
  19.  
  20. #define tempPin 0
  21. #define lightPin 1
  22. #define tiltPin 6
  23. #define humidityPin 7
  24.  
  25. #include <dht11.h>
  26.  
  27. dht11 DHT11;
  28.  
  29. // initialise LCD library and pins
  30. void setup() {
  31. lcd.begin(16, 2);
  32. Serial.begin(115200);
  33. pinMode(tempPin, INPUT);
  34. pinMode(lightPin, INPUT);
  35. pinMode(tiltPin, INPUT);
  36. pinMode(humidityPin, INPUT);
  37. pinMode(13, OUTPUT);
  38.  
  39. Temp = 0;
  40. Light = 0;
  41. Tilt = 0;
  42. Humidity = 0;
  43. hCheck = -2;
  44.  
  45. digitalWrite(13, HIGH);
  46. delay(200);
  47. digitalWrite(13, LOW);
  48. delay(200);
  49.  
  50. digitalWrite(13, HIGH);
  51. delay(200);
  52. digitalWrite(13, LOW);
  53. delay(200);
  54.  
  55. digitalWrite(13, HIGH);
  56. delay(200);
  57. digitalWrite(13, LOW);
  58. delay(200);
  59.  
  60.  
  61.  
  62. }
  63.  
  64. void loop()
  65. {
  66.  
  67.  
  68. if (doTemp)
  69. {
  70. //pds("reading temp pin");
  71. analogRead(tempPin);
  72. delay(10);
  73. Temp = convertTemp(analogRead(tempPin)); // ;
  74. delay(10);
  75. }
  76.  
  77. if(doLight)
  78. {
  79. //pds("reading light pin");
  80. analogRead(lightPin);
  81. delay(10);
  82. Light = toLux(analogRead(lightPin));
  83. delay(10);
  84. }
  85.  
  86. if(doHumi)
  87. {
  88. //pds("reading humidity");
  89. hCheck = DHT11.read(humidityPin);
  90. //Serial.println(String(hCheck));
  91. //Serial.println(String(DHT11.humidity));
  92. if(hCheck != 0)
  93. Humidity = 255; //Must be an error
  94. else
  95. Humidity = DHT11.humidity;
  96.  
  97. }
  98.  
  99. if(doTilt)
  100. {
  101. //pds("reading tilt pin");
  102. Tilt = (digitalRead(tiltPin) == LOW);
  103. }
  104. /*
  105. char charBuf[5];
  106. dtostrf(Temp, 1, 2, charBuf);
  107. */
  108. //Serial.println("TEMP:" + String(Temp));
  109. //Serial.println("LIGH:" + String(Light));
  110. //Serial.println("HUMI:" + String(Humidity));
  111. //Sermial.println("TILT:" + String(Tilt));
  112.  
  113. /*
  114.  
  115. */
  116. Temp = GetAvgTemp(Temp);
  117. showData(Temp, Light, Humidity, Tilt);
  118. Serial.println(String(Temp) + ":" + String(Light) + ":" + String(Humidity));
  119. //pds("delay started for " + String(dtime) + " seconds");
  120. delay(dtime * 1000);
  121. //pds("delay complete");
  122. }
  123.  
  124. float convertTemp(float reading)
  125. {
  126. return (5.0 * reading * 100.0) / 1024.0;
  127. }
  128.  
  129.  
  130. // Change the ADC reading to Lux. Assumes a 10K pull down resistor and 5V
  131. int toLux(int adc)
  132. {
  133. if (adc > 868)
  134. return 300 + 5.6 * (adc - 868);
  135. else if (adc > 853)
  136. return 250 + 3.46 * (adc - 853);
  137. else if (adc > 788)
  138. return 150 + 1.52 * (adc - 788);
  139. else if (adc > 731)
  140. return 120 + 0.53 * (adc - 731);
  141. else if (adc > 683)
  142. return 80 + 0.82 * (adc - 683);
  143. else if (adc > 256)
  144. return 10 + 0.16 * (adc - 256);
  145. else
  146. return 0.04 * adc;
  147. }
  148.  
  149. //void showData (float temp, int light, int humidity, boolean tilt)
  150. //{
  151. // String s1, s2, s3, s4;
  152. //
  153. // char charBuf[5];
  154. //
  155. // dtostrf(temp, 5, 2, charBuf);
  156. // if(temp == 0.0)
  157. // {
  158. // Serial.println("<d><temp>0.0</temp><light>" + String(light) + "</light><hum>" + String(humidity) + "</hum><tilt>" + String(tilt) + "</tilt></d>");
  159. // s1 = "00.00" + String(char(0xdf)) + "C";
  160. // }
  161. // else
  162. // {
  163. // Serial.println("<d><temp>" + String(charBuf) + "</temp><light>" + String(light) + "</light><hum>" + String(humidity) + "</hum><tilt>" + String(tilt) + "</tilt></d>");
  164. // s1 = String(charBuf) + String(char(0xdf)) + "C";
  165. // }
  166. // pds(s1);
  167. // //printTemp(s1);
  168. // pds("s1 printed");
  169. // s2 = String (light) + "Lux";
  170. // pds(s2);
  171. // //printLux(s2);
  172. // pds("s2 printed");
  173. //// lcd.setCursor(5,0);
  174. //// lcd.print(lcdSanitise(lcdMakeFirstLineEnd(String(char(0xdf)) + "C", s2)));
  175. // if(humidity == 255)
  176. // s3 = "ERROR";
  177. // else
  178. // s3 = String(humidity) + "%";
  179. //
  180. // pds(s3);
  181. // printHum(s3);
  182. // pds("s3 printed");
  183. // if (tilt)
  184. // s4 = "TILT";
  185. // else
  186. // s4 = "FLAT";
  187. // pds(s4);
  188. // //printTilt(s4);
  189. // pds("s4 printed");
  190. // //lcd.setCursor(0,1);
  191. // //lcd.print(lcdSanitise(lcdMakeLine(s1, s2)));
  192. //}
  193.  
  194. void printTemp(String temp)
  195. {
  196.  
  197. lcd.setCursor(0,0);
  198. lcd.print(temp);
  199. lcd.setCursor(5,0);
  200. lcd.print(String(char(0xdf)) + "C");
  201. }
  202.  
  203. void printLux(String light)
  204. {
  205.  
  206. lcd.setCursor(16 - light.length(),0);
  207. lcd.print(light);
  208. }
  209.  
  210. void printHum(String humidity)
  211. {
  212. lcd.setCursor(0,1);
  213. lcd.print(humidity.length());
  214. // lcd.setCursor(0,0);
  215. // lcd.print(humidity);
  216. }
  217.  
  218. void printTilt(String tilt)
  219. {
  220. lcd.setCursor(12,1);
  221. lcd.print(tilt);
  222. }
  223.  
  224. String lcdMakeFirstLineEnd(String s1, String s2)
  225. {
  226. String spaces = " ";
  227. return s1 + spaces.substring(0, 11 - s1.length() - s2.length()) + s2;
  228. }
  229.  
  230. String lcdMakeLine(String s1, String s2)
  231. {
  232. String spaces = " ";
  233. return s1 + spaces.substring(0, 16 - s1.length() - s2.length()) + s2;
  234. }
  235.  
  236. String lcdSanitise(String textIn)
  237. {
  238. if (textIn.length() > 16)
  239. {
  240. return textIn.substring(0,16);
  241. }
  242. return textIn;
  243. }
  244.  
  245. void pds(String s)
  246. {
  247. #ifdef DEBUG
  248. Serial.println("DEBUG: " + s);
  249. // More debug code...
  250. #endif
  251.  
  252. }
  253.  
  254.  
  255. int prevTemps[10];
  256.  
  257. void arrayRemoveAndShift()
  258. {
  259. for(int i = 8; i>=0; i--)
  260. {
  261. prevTemps[i+1] = prevTemps[i];
  262. }
  263. prevTemps[0] = 0;
  264. }
  265.  
  266. void arrayInsert(int i)
  267. {
  268. prevTemps[0] = i;
  269. }
  270.  
  271. int GetAvgTemp(int currentReading)
  272. {
  273.  
  274. arrayRemoveAndShift();
  275.  
  276. arrayInsert(currentReading);
  277.  
  278. int avg = 0;
  279. int nonZeroVals = 0;
  280.  
  281. for(int i = 9; i>=0; i--)
  282. {
  283. if(prevTemps[i] != 0)
  284. {
  285. nonZeroVals++;
  286. }
  287. }
  288. if (nonZeroVals > 0)
  289. {
  290. for (int i = 0; i < nonZeroVals; i++)
  291. {
  292. // Serial.print(String( prevTemps[i]) + ", ");
  293. avg += prevTemps[i];
  294. }
  295. // Serial.println("- " + String(avg / nonZeroVals));
  296. }
  297. else
  298. {
  299. return 0;
  300. }
  301. return avg / nonZeroVals;
  302. }
  303.  
  304. void showData (int temp, int light, int humidity, boolean tilt)
  305. {
  306.  
  307. String s1, s2, s3;
  308. String spaces = " ";
  309. s1 = String(temp) + char(0xdf) + "C";
  310. s2 = String (light) + "Lux";
  311. s3 = s1 + spaces.substring(0, 16 - s1.length() - s2.length()) + s2;
  312. lcd.setCursor(0,0);
  313. lcd.print(s3);
  314.  
  315. if(humidity == 255)
  316. s1 = "ERROR";
  317. else
  318. s1 = String(humidity) + "%";
  319. if (tilt)
  320. s2 = "TILT";
  321. else
  322. s2 = "FLAT";
  323. s3 = s1 + spaces.substring(0, 16 - s1.length() - s2.length()) + s2;
  324. lcd.setCursor(0,1);
  325. lcd.print(s3);
  326. }
Add Comment
Please, Sign In to add comment