Guest User

Untitled

a guest
Jan 23rd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.97 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <gfxfont.h>
  3. #include <Adafruit_GFX.h>
  4. #include <Adafruit_SSD1306.h>
  5. #include <EEPROM.h>
  6.  
  7. #define OLED_RESET 4
  8. Adafruit_SSD1306 display(OLED_RESET);
  9.  
  10. //Arrays for user input possibilities
  11. const int ml[11] = {0, 25, 50, 75, 100, 125, 150, 175, 200, 225, 250};
  12. const int hrs[4] = {6, 12, 18, 24};
  13. //EEPROM Memory addresses
  14. const int EEPROMP1ml = 0;
  15. const int EEPROMP1hrs = 1;
  16. const int EEPROMP2ml = 2;
  17. const int EEPROMP2hrs = 3;
  18. const int EEPROMP3ml = 4;
  19. const int EEPROMP3hrs = 5;
  20. const int EEPROMP4ml = 6;
  21. const int EEPROMP4hrs = 7;
  22. //Current preset values variables
  23. int P1ml;
  24. int P1hrs;
  25. int P2ml;
  26. int P2hrs;
  27. int P3ml;
  28. int P3hrs;
  29. int P4ml;
  30. int P4hrs;
  31. //Buttons
  32. int btnLeft = 3;
  33. int btnRight = 4;
  34. //Relays
  35. int pump = 12;
  36. int sp[7] = {5,6,7,8,9,10,11};
  37.  
  38. int runMode = 0;
  39. int selected = 0;
  40. int selected2 = 0;
  41. long int1Last = 0;
  42. int P1Next;
  43. int P2Next;
  44. int P3Next;
  45. int P4Next;
  46. long P1End;
  47. long P2End;
  48. long P3End;
  49. long P4End;
  50.  
  51. int mlEdit;
  52. int hrsEdit;
  53.  
  54. int runTime = 1; //millis in a second of run time, 1000 is real time
  55. bool right = false;
  56. void setup() {
  57. //Display init
  58. display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  59.  
  60. //Assigning current preset values to EEPROM Memory adresses
  61. P1ml = EEPROM.read(EEPROMP1ml);
  62. if(P1ml>10){P1ml=0;EEPROM.write(EEPROMP1ml,0);}
  63. P1hrs = EEPROM.read(EEPROMP1hrs);
  64. if(P1hrs>3){P1hrs=0;EEPROM.write(EEPROMP1hrs,0);}
  65. P1Next = hrs[P1hrs];
  66. P2ml = EEPROM.read(EEPROMP2ml);
  67. if(P2ml>10){P2ml=0;EEPROM.write(EEPROMP2ml,0);}
  68. P2hrs = EEPROM.read(EEPROMP2hrs);
  69. if(P2hrs>3){P2hrs=0;EEPROM.write(EEPROMP2hrs,0);}
  70. P2Next = hrs[P2hrs];
  71. P3ml = EEPROM.read(EEPROMP3ml);
  72. if(P3ml>10){P3ml=0;EEPROM.write(EEPROMP3ml,0);}
  73. P3hrs = EEPROM.read(EEPROMP3hrs);
  74. if(P3hrs>3){P3hrs=0;EEPROM.write(EEPROMP3hrs,0);}
  75. P3Next = hrs[P3hrs];
  76. P4ml = EEPROM.read(EEPROMP4ml);
  77. if(P4ml>10){P4ml=0;EEPROM.write(EEPROMP4ml,0);}
  78. P4hrs = EEPROM.read(EEPROMP4hrs);
  79. if(P4hrs>3){P4hrs=0;EEPROM.write(EEPROMP4hrs,0);}
  80. P4Next = hrs[P4hrs];
  81.  
  82. //Setup buttons
  83. pinMode(btnLeft, INPUT_PULLUP);
  84. pinMode(btnRight, INPUT_PULLUP);
  85. attachInterrupt(1, BtnLeft, FALLING);
  86. runMode = 0;
  87.  
  88. //Setup outputs to relays
  89. pinMode(pump, OUTPUT);
  90. digitalWrite(pump, HIGH);
  91. pinMode(sp[0], OUTPUT);
  92. digitalWrite(sp[0], HIGH);
  93. pinMode(sp[1], OUTPUT);
  94. digitalWrite(sp[1], HIGH);
  95. pinMode(sp[2], OUTPUT);
  96. digitalWrite(sp[2], HIGH);
  97. pinMode(sp[3], OUTPUT);
  98. digitalWrite(sp[3], HIGH);
  99. pinMode(sp[4], OUTPUT);
  100. digitalWrite(sp[4], HIGH);
  101. pinMode(sp[5], OUTPUT);
  102. digitalWrite(sp[5], HIGH);
  103. pinMode(sp[6], OUTPUT);
  104. digitalWrite(sp[6], HIGH);
  105. }
  106.  
  107. void BtnLeft() {
  108. if(millis()-int1Last > 75){
  109. if(runMode == 0){
  110. runMode = 1;
  111. selected = 0;
  112. } else if(runMode == 1){
  113. if(selected == 4){
  114. selected = 0;
  115. } else {
  116. selected++;
  117. }
  118. } else if(runMode == 2){
  119. if(selected2 == 3){
  120. selected2 = 0;
  121. } else {
  122. selected2++;
  123. }
  124. }
  125. }
  126. int1Last = millis();
  127. }
  128.  
  129. void loop() {
  130. //running
  131. bool P1Running = false;
  132. bool P2Running = false;
  133. bool P3Running = false;
  134. bool P4Running = false;
  135.  
  136. while (runMode == 0) {
  137. long n = millis()/runTime;
  138. int h = (n / 3600) % 24;
  139. int m = (n / 60) % 60;
  140. int s = n % 60;
  141.  
  142. if(not(P1Running or P2Running or P3Running or P4Running)) {
  143. if((P1Next <= h and P1Next >= 6) or (h < 6 and P1Next <= h)){
  144. P1Running = true;
  145. P1Next = P1Next + hrs[P1hrs];
  146. if(P1Next>=24){P1Next = P1Next % 24;}
  147. digitalWrite(sp[0], LOW);
  148. digitalWrite(pump, LOW);
  149. P1End = n + 1000;
  150. } else if((P2Next <= h and P2Next >= 6) or (h < 6 and P2Next <= h)){
  151. P2Running = true;
  152. P2Next = P2Next + hrs[P2hrs];
  153. if(P2Next>=24){P2Next = P2Next % 24;}
  154. digitalWrite(sp[1], LOW);
  155. digitalWrite(pump, LOW);
  156. P2End = n + 1000;
  157. } else if((P3Next <= h and P3Next >= 6) or (h < 6 and P3Next <= h)){
  158. P3Running = true;
  159. P3Next = P3Next + hrs[P3hrs];
  160. if(P3Next>=24){P3Next = P3Next % 24;}
  161. digitalWrite(sp[2], LOW);
  162. digitalWrite(pump, LOW);
  163. P3End = n + 1000;
  164. } else if((P4Next <= h and P4Next >= 6) or (h < 6 and P4Next <= h)){
  165. P4Running = true;
  166. P4Next = P4Next + hrs[P4hrs];
  167. if(P4Next>=24){P4Next = P4Next % 24;}
  168. digitalWrite(sp[3], LOW);
  169. digitalWrite(pump, LOW);
  170. P4End = n + 1000;
  171. }
  172. }
  173.  
  174. if(P1Running and n > P1End){
  175. P1Running = false;
  176. digitalWrite(pump, HIGH);
  177. digitalWrite(sp[0], HIGH);
  178. }
  179. if(P2Running and n > P2End){
  180. P2Running = false;
  181. digitalWrite(pump, HIGH);
  182. digitalWrite(sp[1], HIGH);
  183. }
  184. if(P3Running and n > P3End){
  185. P3Running = false;
  186. digitalWrite(pump, HIGH);
  187. digitalWrite(sp[2], HIGH);
  188. }
  189. if(P4Running and n > P4End){
  190. P4Running = false;
  191. digitalWrite(pump, HIGH);
  192. digitalWrite(sp[3], HIGH);
  193. }
  194.  
  195. display.clearDisplay();
  196. display.setTextSize(1);
  197. display.setTextColor(WHITE);
  198. display.setCursor(0,0);
  199. display.print("Running...");
  200. display.setCursor(75,0);
  201. display.print(h);display.print(":");if(m<10){display.print("0");}display.print(m);display.print(":");if(s<10){display.print("0");}display.print(s);
  202. display.setCursor(0,8);
  203. if(P1Running == true){display.print(" [1]");}else{display.print(" 1");}
  204. display.setCursor(32,8);
  205. if(P2Running == true){display.print(" [2]");}else{display.print(" 2");}
  206. display.setCursor(64,8);
  207. if(P3Running == true){display.print(" [3]");}else{display.print(" 3");}
  208. display.setCursor(96,8);
  209. if(P4Running == true){display.print(" [4]");}else{display.print(" 4");}
  210. display.setCursor(0,16);
  211. display.print(P1Next);display.print(":00");
  212. display.setCursor(32,16);
  213. display.print(P2Next);display.print(":00");
  214. display.setCursor(64,16);
  215. display.print(P3Next);display.print(":00");
  216. display.setCursor(96,16);
  217. display.print(P4Next);display.print(":00");
  218. display.setCursor(0,24);
  219. display.setTextColor(BLACK,WHITE);
  220. display.print("Edit");
  221. display.display();
  222. delay(100);
  223. }
  224.  
  225. while (runMode == 1) {
  226. //Make sure everything is off
  227. digitalWrite(pump, HIGH);
  228. digitalWrite(sp[0], HIGH);
  229. digitalWrite(sp[1], HIGH);
  230. digitalWrite(sp[2], HIGH);
  231. digitalWrite(sp[3], HIGH);
  232. //Read the right button
  233. right = not(digitalRead(btnRight));
  234. if(selected == 4 and right == true){
  235. //check if next is in the past and correct
  236. if(P1Next <= (millis()/(3600*runTime)) % 24) {P1Next = ((millis()/(3600*runTime)) % 24) + hrs[P1hrs];if(P1Next>=24){P1Next = P1Next % 24;}}
  237. if(P2Next <= (millis()/(3600*runTime)) % 24) {P2Next = ((millis()/(3600*runTime)) % 24) + hrs[P2hrs];if(P2Next>=24){P2Next = P2Next % 24;}}
  238. if(P3Next <= (millis()/(3600*runTime)) % 24) {P3Next = ((millis()/(3600*runTime)) % 24) + hrs[P3hrs];if(P3Next>=24){P3Next = P3Next % 24;}}
  239. if(P4Next <= (millis()/(3600*runTime)) % 24) {P4Next = ((millis()/(3600*runTime)) % 24) + hrs[P4hrs];if(P4Next>=24){P4Next = P4Next % 24;}}
  240. runMode = 0;
  241. } else if(right == true){
  242. selected2 = 0;
  243. mlEdit = selected;
  244. hrsEdit = selected;
  245. runMode = 2;
  246. }
  247. display.clearDisplay();
  248. display.setTextSize(1);
  249. display.setTextColor(WHITE);
  250. display.setCursor(0,0);
  251. display.print("Preset:");
  252. display.setCursor(96,0);
  253. if(selected == 4){display.setTextColor(BLACK,WHITE);}display.print(" Run ");display.setTextColor(WHITE);
  254. display.setCursor(0,8);
  255. if(selected == 0){display.setTextColor(BLACK,WHITE);}display.print(" 1 ");display.setTextColor(WHITE);
  256. display.setCursor(32,8);
  257. if(selected == 1){display.setTextColor(BLACK,WHITE);}display.print(" 2 ");display.setTextColor(WHITE);
  258. display.setCursor(64,8);
  259. if(selected == 2){display.setTextColor(BLACK,WHITE);}display.print(" 3 ");display.setTextColor(WHITE);
  260. display.setCursor(96,8);
  261. if(selected == 3){display.setTextColor(BLACK,WHITE);}display.print(" 4 ");display.setTextColor(WHITE);
  262. display.setCursor(0,16);
  263. display.print(ml[P1ml]);display.print("/");display.print(hrs[P1hrs]);
  264. display.setCursor(32,16);
  265. display.print(ml[P2ml]);display.print("/");display.print(hrs[P2hrs]);
  266. display.setCursor(64,16);
  267. display.print(ml[P3ml]);display.print("/");display.print(hrs[P3hrs]);
  268. display.setCursor(96,16);
  269. display.print(ml[P4ml]);display.print("/");display.print(hrs[P4hrs]);
  270. display.setCursor(0,24);
  271. display.setTextColor(BLACK,WHITE);
  272. display.print("Select");
  273. display.setCursor(96,24);
  274. display.print("Enter");
  275. display.display();
  276. delay(100);
  277. }
  278.  
  279. while(runMode == 2){
  280. right = not(digitalRead(btnRight));
  281. if(right and selected2 == 0){
  282. runMode = 1;
  283. }
  284. if(selected2 == 1 and right) {
  285. if(mlEdit >= 10){
  286. mlEdit = 0;
  287. } else {
  288. mlEdit++;
  289. }
  290. }
  291. if(selected2 == 2 and right) {
  292. if(hrsEdit >= 3){
  293. hrsEdit = 0;
  294. } else {
  295. hrsEdit++;
  296. }
  297. }
  298. display.clearDisplay();
  299. display.setTextSize(1);
  300. display.setTextColor(WHITE);
  301. display.setCursor(0,0);
  302. display.print("Preset ");display.print(selected + 1);
  303. display.setCursor(58,0);
  304. if(selected2 == 0){display.setTextColor(BLACK,WHITE);}display.print("Cancel");display.setTextColor(WHITE);
  305. display.setCursor(96,0);
  306. if(selected2 == 3){display.setTextColor(BLACK,WHITE);}display.print("Save");display.setTextColor(WHITE);
  307. display.setCursor(0,8);
  308. if(selected2 == 1){display.setTextColor(BLACK,WHITE);}display.print(ml[mlEdit]);display.setTextColor(WHITE);display.print(" ml");
  309. display.setCursor(0,16);
  310. if(selected2 == 2){display.setTextColor(BLACK,WHITE);}display.print(hrs[hrsEdit]);display.setTextColor(WHITE);display.print(" hrs");
  311. display.setCursor(0,24);
  312. display.setTextColor(BLACK,WHITE);
  313. display.print("Select");
  314. display.setTextColor(BLACK,WHITE);
  315. display.setCursor(90,24);
  316. if(selected2 == 0 or selected2 == 3) {
  317. display.print("Enter");
  318. } else {
  319. display.print("Adjust");
  320. }
  321. display.display();
  322. delay(100);
  323. }
  324. }
Add Comment
Please, Sign In to add comment