Advertisement
hms11

CoopCommand

Nov 12th, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.97 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3. #include <DHT.h>;
  4. LiquidCrystal_I2C lcd(0x27, 20, 4); // Set I2C Address and display size
  5.  
  6. // pin assignments
  7. const int photocellPin = A0; // analog input pin for photocell
  8. int button1 = 2;
  9. int button2 = 3;
  10. int button3 = 4;
  11. #define DHTPIN 13 // what pin we're connected to
  12. #define DHTTYPE DHT22 // DHT 22 (AM2302)
  13. DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
  14.  
  15. // Menu Variables and States
  16.  
  17. unsigned long dhtDelay = 2000; //delay so sensor is read every 2 seconds
  18. unsigned long lastDhtReadingTime = 0; // the last time the DHT22 was read
  19. bool menuOn = true; // state of the display menu
  20. int menu[5]; // number of menu items
  21. bool doorOpen = true; // is the coop door open or closed
  22. int optionSelect = 0; //which menu option is selected
  23. int lastOptionSelect = 0;
  24. int lastItemSelect = 0;
  25. int itemSelect = 0;
  26. int coopTemp = 4;
  27. int closeDoor = 20;
  28. int hotTemp = 30;
  29. int coldTemp = 2;
  30. int waterTemp = 2;
  31. int chk;
  32. float hum; //Stores humidity value
  33. float temp; //Stores temperature value
  34. int photocellReading; // analog reading of the photocell
  35. int photocellReadingLevel; // photocell reading levels (night, light)
  36. unsigned long photocellReadingDelay = 6000; // 600000 = 10 minute
  37. unsigned long lastPhotocellReadingTime = 0; // the last time the photocell was read
  38. int buttonState1 = 0; // current state of each button
  39. int buttonState2 = 0;
  40. int buttonState3 = 0;
  41. int lastButtonState1 = 0; // previous state of each button
  42. int lastButtonState2 = 0;
  43. int lastButtonState3 = 0;
  44. unsigned long displayTimer = 6000; // timer to automatically turn on the display
  45. unsigned long lastDisplayTimer = 0;
  46.  
  47.  
  48.  
  49. void setup() {
  50. // put your setup code here, to run once:
  51. dht.begin();
  52. pinMode(photocellPin, INPUT);
  53. pinMode(button1, INPUT);
  54. pinMode(button2, INPUT);
  55. pinMode(button3, INPUT);
  56. lcd.begin();
  57. lcd.clear();
  58. lcd.home();
  59. lcd.print(" Coop Command");
  60. lcd.setCursor(0, 1);
  61. lcd.print(" Control Center");
  62. lcd.setCursor(0, 2);
  63. lcd.print(" V1.0");
  64. lcd.setCursor(0, 3);
  65. lcd.print(" LCD TEST/MENU TEST");
  66. delay(2000);
  67. lcd.clear();
  68. }
  69.  
  70.  
  71.  
  72. void ventFan() {
  73. if ((unsigned long)(millis() - lastDhtReadingTime) >= dhtDelay) {
  74. lastDhtReadingTime = millis();
  75. hum = dht.readHumidity();
  76. temp = dht.readTemperature();
  77. coopTemp = temp;
  78. }
  79. }
  80.  
  81. void coopDoor() {
  82. photocellReading = analogRead(photocellPin);
  83. if ((unsigned long)(millis() - lastPhotocellReadingTime) >= photocellReadingDelay) {
  84. lastPhotocellReadingTime = millis();
  85. if (photocellReading >= 0 && photocellReading <= 20) {
  86. photocellReadingLevel = 1;
  87. }
  88. else if (photocellReading >= 125) {
  89. photocellReadingLevel = 0;
  90. }
  91. }
  92. }
  93.  
  94.  
  95. void readButtons() {
  96. buttonState1 = (digitalRead(button1));
  97. buttonState2 = (digitalRead(button2));
  98. buttonState3 = (digitalRead(button3));
  99. if (buttonState1 != lastButtonState1) {
  100. if (buttonState1 == LOW) { //if the enter/back button has been pressed
  101. lastDisplayTimer = millis();
  102. if (itemSelect == 0) { //if we are not in the settings change
  103. itemSelect = 1;
  104. }
  105. else if (itemSelect == 1) {
  106. itemSelect = 0;
  107. }
  108. }
  109. lastButtonState1 = buttonState1;
  110. }
  111. if (itemSelect == 0) { // if we are not adjusting settings
  112.  
  113. if (buttonState2 != lastButtonState2) { //if the right button has been pressed
  114. if (buttonState2 == LOW) { //if the right button has been pressed
  115. lastDisplayTimer = millis();
  116. if (optionSelect <= 3) { //if we are not past the last menu screen
  117. optionSelect ++;
  118. }
  119. else if (optionSelect == 4) { // if we are at the last menu screen
  120. optionSelect = 0;
  121. }
  122. }
  123. lastButtonState2 = buttonState2;
  124. }
  125. if (buttonState3 != lastButtonState3) { //if the left button has been pressed
  126. if (buttonState3 == LOW) { //if the left button has been pressed
  127. lastDisplayTimer = millis();
  128. if (optionSelect > 0) { //if we are not at the first menu screen
  129. optionSelect --;
  130. }
  131. else if (optionSelect == 0) {
  132. optionSelect = 3;
  133. }
  134. }
  135. lastButtonState3 = buttonState3;
  136. }
  137. }
  138.  
  139. else if (itemSelect == 1) { // if we are adjusting settings
  140. buttonState2 = (digitalRead(button2));
  141. buttonState3 = (digitalRead(button3));
  142.  
  143. if (buttonState2 != lastButtonState2) { //if the right button has been pressed
  144. if (buttonState2 == LOW) { //if the right button has been pressed
  145. lastDisplayTimer = millis();
  146. if (optionSelect == 1) { //if we are adjusting the Vent Fan Temp
  147. hotTemp = (hotTemp + 5);
  148. }
  149. if (itemSelect == 2) { // if we are adjusting the Water Heater Temp
  150. coldTemp = (coldTemp + 1);
  151. }
  152. if (itemSelect == 3) { // if we are adjusting the Door Light Sensor
  153. closeDoor = (closeDoor + 5);
  154. }
  155. }
  156. lastButtonState2 = buttonState2;
  157. }
  158. if (buttonState3 != lastButtonState3) { //if the left button has been pressed
  159. if (buttonState3 == LOW) { //if the left button has been pressed
  160. lastDisplayTimer = millis();
  161. if (optionSelect == 1) { //if we are adjusting the Vent Fan Temp
  162. hotTemp = (hotTemp - 5);
  163. }
  164. if (itemSelect == 2) { // if we are adjusting the Water Heater Temp
  165. coldTemp = (coldTemp - 1);
  166. }
  167. if (itemSelect == 3) { // if we are adjusting the Door Light Sensor
  168. closeDoor = (closeDoor - 5);
  169. }
  170. }
  171. lastButtonState3 = buttonState3;
  172. }
  173. }
  174. }
  175.  
  176. void displayMenu() {
  177. if (menuOn) { // if the display is supposed to be on
  178. lcd.display(); // turn the display on
  179. lcd.backlight();
  180.  
  181. if (optionSelect != lastOptionSelect) {
  182. lcd.clear();
  183. lastOptionSelect = optionSelect;
  184. }
  185. else if (itemSelect != lastItemSelect) {
  186. lcd.clear();
  187. lastItemSelect = itemSelect;
  188. }
  189.  
  190. switch (optionSelect) {
  191.  
  192. case 0:
  193. lcd.home();
  194. lcd.print(" Coop Command");
  195. lcd.setCursor(0, 1);
  196. lcd.print("< >");
  197. lcd.setCursor(0, 2);
  198. lcd.print(" Control Center");
  199. lcd.setCursor(0, 3);
  200. lcd.print(" Firmware V1.0");
  201. break;
  202.  
  203. case 1:
  204. if (itemSelect == 0) {
  205. lcd.home();
  206. lcd.print(" Coop Temp: ");
  207. lcd.print(coopTemp);
  208. lcd.print(" C");
  209. lcd.setCursor(0, 1);
  210. lcd.print("< >");
  211. lcd.setCursor(0, 2);
  212. lcd.print("Ventilation Fan: ");
  213. lcd.print("OFF");
  214. }
  215. if (itemSelect == 1) {
  216. lcd.home();
  217. lcd.print(" Coop Temp: ");
  218. lcd.print(coopTemp);
  219. lcd.print(" C");
  220. lcd.setCursor(0, 1);
  221. lcd.print("< >");
  222. lcd.setCursor(0, 2);
  223. lcd.print("Fan On Temp: ");
  224. lcd.print(hotTemp);
  225. }
  226. break;
  227.  
  228. case 2:
  229. if (itemSelect == 0) {
  230. lcd.home();
  231. lcd.print(" Water Temp: ");
  232. lcd.print(waterTemp);
  233. lcd.print(" C");
  234. lcd.setCursor(0, 1);
  235. lcd.print("< >");
  236. lcd.setCursor(0, 2);
  237. lcd.print(" Water Heat: ");
  238. lcd.print("ON");
  239. }
  240. if (itemSelect == 1) {
  241. lcd.home();
  242. lcd.print(" Water Temp: ");
  243. lcd.print(waterTemp);
  244. lcd.print(" C");
  245. lcd.setCursor(0, 1);
  246. lcd.print("< >");
  247. lcd.setCursor(0, 2);
  248. lcd.print(" Heat On Temp: ");
  249. lcd.print(coldTemp);
  250. }
  251. break;
  252.  
  253. case 3:
  254. if (itemSelect == 0) {
  255. lcd.home();
  256. lcd.print(" Coop Door: ");
  257. lcd.print("OPEN");
  258. lcd.setCursor(0, 1);
  259. lcd.print("< >");
  260. lcd.setCursor(0, 2);
  261. lcd.print(" Light Value: ");
  262. lcd.print(photocellReading);
  263. }
  264. if (itemSelect == 1) {
  265. lcd.home();
  266. lcd.print(" Coop Door: ");
  267. lcd.print("OPEN");
  268. lcd.setCursor(0, 1);
  269. lcd.print("< >");
  270. lcd.setCursor(0, 2);
  271. lcd.print(" Light Adjust: ");
  272. lcd.print(closeDoor);
  273. }
  274. break;
  275. }
  276.  
  277. if ((unsigned long)(millis() - lastDisplayTimer) >= displayTimer) {
  278. menuOn = false;
  279. }
  280. }
  281. else if (!menuOn) { // if the display is supposed to be off
  282. lcd.noDisplay(); // turn the display off
  283. lcd.noBacklight();
  284. }
  285. if ((buttonState1 == 0) && (!menuOn)) {
  286. menuOn = true;
  287. lastDisplayTimer = millis();
  288. optionSelect = 0; //back to front screen
  289. itemSelect = 0;
  290. }
  291. }
  292.  
  293.  
  294.  
  295. void loop() {
  296. // put your main code here, to run repeatedly:
  297. readButtons();
  298. ventFan();
  299. coopDoor;
  300. displayMenu();
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement