Advertisement
Nalyd1002

plant robot

Nov 23rd, 2022 (edited)
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.31 KB | None | 0 0
  1. #include <LiquidCrystal_I2C.h>
  2. #include <string.h>
  3.  
  4. LiquidCrystal_I2C lcd(0x27, 16, 2);
  5.  
  6. const int dirPin = 2;
  7. const int stepPin_Pot = 3;
  8. const int dirPin_Shield = 4;
  9. const int stepPin_Shield = 9;
  10. const int light_sensor_Pin_1 = A1;
  11. const int light_sensor_Pin_2 = A2;
  12. const int light_sensor_Pin_3 = A3;
  13. const int light_sensor_Pin_4 = A4;
  14. const int humidity_sensor = A0;
  15. const int lampPin = 5;
  16. const int pumpPin = 6;
  17. const int buttonPin1 = 7;
  18. const int buttonPin2 = 8;
  19. const int encoder1 = 11;
  20. const int encoder2 = 12;
  21.  
  22. int light_requirement_1 = 0;
  23. int light_requirement_2 = 0;
  24. int light_requirement_3 = 0;
  25. int time = 0;
  26. int water_need = 400;
  27. int light_need = 400;
  28. int buttonState1 = 0;
  29. int buttonState2 = 0;
  30. int counter = 0;
  31. int shield_position = 0;
  32. int L_min = 3600*light_need;
  33.  
  34. String plant = "";
  35.  
  36.  
  37. void setup() {
  38.  
  39. cli();//stop interrupts
  40. pinMode(stepPin_Pot, OUTPUT);
  41. pinMode(dirPin, OUTPUT);
  42. pinMode(dirPin_Shield, OUTPUT);
  43. pinMode(lampPin, OUTPUT);
  44. pinMode(pumpPin, OUTPUT);
  45. pinMode(buttonPin1, INPUT);
  46. pinMode(buttonPin2, INPUT);
  47. pinMode(encoder1, INPUT);
  48. pinMode(encoder2, INPUT);
  49. digitalWrite(dirPin,HIGH);
  50. digitalWrite(dirPin_Shield,HIGH);
  51. Serial.begin(9600);
  52. TCCR1A = 0; //register 1A reset to 0
  53. TCCR1B = 0; //register 2A reset to 0
  54. TCCR1B |= B00000101; // prescaler 1024, Pq 1B et pas 1A
  55. TCNT1 = 0; // timer1 reset to 0
  56. TIMSK1 |= B00000010; //Set OCIE1A to 1 so we enable compare match A
  57. OCR1A = 15625; // 1s every 15625 pulses
  58. sei();//allow interrupts
  59. lcd.init();
  60. lcd.backlight();
  61. lcd.setCursor(1, 0);
  62. lcd.print("Choose Plant");
  63. reset_pos();
  64.  
  65. }
  66.  
  67. void reset_pos() {
  68. while (digitalRead(encoder1)==LOW){
  69. digitalWrite(stepPin_Pot,HIGH);
  70. delayMicroseconds(500);
  71. digitalWrite(stepPin_Pot,LOW);
  72. delayMicroseconds(500);
  73. }
  74. while (digitalRead(encoder2)==LOW){
  75. digitalWrite(stepPin_Shield,HIGH);
  76. delayMicroseconds(500);
  77. digitalWrite(stepPin_Shield,LOW);
  78. delayMicroseconds(500);
  79. }
  80. }
  81.  
  82. void plant_config() {
  83.  
  84. buttonState1 = digitalRead(buttonPin1);
  85. buttonState2 = digitalRead(buttonPin2);
  86.  
  87. if (buttonState1 == HIGH && counter<20){ // lcd display doesn't show more than the 20 plants
  88. counter += 1;
  89. if (counter==1){
  90. plant = "Sweet Flag";
  91. water_need = 500;
  92. light_need = 810;
  93. delayMicroseconds(500);
  94. }
  95. if (counter==2){
  96. plant = "Min Sweet Flag";
  97. water_need = 500;
  98. light_need = 810;
  99. delayMicroseconds(500);
  100. }
  101. if (counter==3){
  102. plant = "Magic Flower";
  103. water_need = 500;
  104. light_need = 810;
  105. delayMicroseconds(500);
  106. }
  107. if (counter==4){
  108. plant = "Maidenhair fern";
  109. water_need = 500;
  110. light_need = 810;
  111. delayMicroseconds(500);
  112. }
  113. if (counter==5){
  114. plant = "Crinkle-leaf";
  115. water_need = 500;
  116. light_need = 810;
  117. delayMicroseconds(500);
  118. }
  119. if (counter==6){
  120. plant = "Plover Eggs";
  121. water_need = 500;
  122. light_need = 810;
  123. delayMicroseconds(500);
  124. }
  125. if (counter==7){
  126. plant = "Silver Vase";
  127. water_need = 500;
  128. light_need = 810;
  129. delayMicroseconds(500);
  130. }
  131. if (counter==8){
  132. plant = "Coral Berry";
  133. water_need = 500;
  134. light_need = 810;
  135. delayMicroseconds(500);
  136. }
  137. if (counter==9){
  138. plant = "Bromeliad";
  139. water_need = 500;
  140. light_need = 810;
  141. delayMicroseconds(500);
  142. }
  143. if (counter==10){
  144. plant = "Basket Vine";
  145. water_need = 500;
  146. light_need = 2150;
  147. delayMicroseconds(500);
  148. }
  149. if (counter==11){
  150. plant = "Lipstick Vine";
  151. water_need = 500;
  152. light_need = 2150;
  153. delayMicroseconds(500);
  154. }
  155. if (counter==12){
  156. plant = "Chinese Everg.";
  157. water_need = 500;
  158. light_need = 270;
  159. delayMicroseconds(500);
  160. }
  161. if (counter==13){
  162. plant = "Silver King";
  163. water_need = 500;
  164. light_need = 270;
  165. delayMicroseconds(500);
  166. }
  167. if (counter==14){
  168. plant = "Silver Queen";
  169. water_need = 500;
  170. light_need = 270;
  171. delayMicroseconds(500);
  172. }
  173. if (counter==15){
  174. plant = "Plume Asparagus";
  175. water_need = 500;
  176. light_need = 810;
  177. delayMicroseconds(500);
  178. }
  179. if (counter==16){
  180. plant = "Crystal Anthur.";
  181. water_need = 500;
  182. light_need = 810;
  183. delayMicroseconds(500);
  184. }
  185. if (counter==17){
  186. plant = "Nest Anthur.";
  187. water_need = 500;
  188. light_need = 810;
  189. delayMicroseconds(500);
  190. }
  191. if (counter==18){
  192. plant = "Flamingo Flower";
  193. water_need = 500;
  194. light_need = 810;
  195. delayMicroseconds(500);
  196. }
  197. if (counter==19){
  198. plant = "Zebra Plant";
  199. water_need = 500;
  200. light_need = 2150;
  201. delayMicroseconds(500);
  202. }
  203. if (counter==20){
  204. plant = "Wax Cissus";
  205. water_need = 500;
  206. light_need = 2150;
  207. delayMicroseconds(500);
  208. }
  209. L_min = 600*light_need; // light need min for 15min
  210. light_requirement_1 = 0;
  211. light_requirement_2 = 0;
  212. light_requirement_3 = 0;
  213. time = 0;
  214. lcd.clear();
  215. lcd.setCursor(1, 0);
  216. lcd.print(plant);
  217. }
  218. if (buttonState2 == HIGH && counter>1){ //lcd doesn't change when
  219. counter -= 1;
  220. if (counter==1){
  221. plant = "Sweet Flag";
  222. water_need = 500;
  223. light_need = 810;
  224. delayMicroseconds(500);
  225. }
  226. if (counter==2){
  227. plant = "Min Sweet Flag";
  228. water_need = 500;
  229. light_need = 810;
  230. delayMicroseconds(500);
  231. }
  232. if (counter==3){
  233. plant = "Magic Flower";
  234. water_need = 500;
  235. light_need = 810;
  236. delayMicroseconds(500);
  237. }
  238. if (counter==4){
  239. plant = "Maidenhair fern";
  240. water_need = 500;
  241. light_need = 810;
  242. delayMicroseconds(500);
  243. }
  244. if (counter==5){
  245. plant = "Crinkle-leaf";
  246. water_need = 500;
  247. light_need = 810;
  248. delayMicroseconds(500);
  249. }
  250. if (counter==6){
  251. plant = "Plover Eggs";
  252. water_need = 500;
  253. light_need = 810;
  254. delayMicroseconds(500);
  255. }
  256. if (counter==7){
  257. plant = "Silver Vase";
  258. water_need = 500;
  259. light_need = 810;
  260. delayMicroseconds(500);
  261. }
  262. if (counter==8){
  263. plant = "Coral Berry";
  264. water_need = 500;
  265. light_need = 810;
  266. delayMicroseconds(500);
  267. }
  268. if (counter==9){
  269. plant = "Bromeliad";
  270. water_need = 500;
  271. light_need = 810;
  272. delayMicroseconds(500);
  273. }
  274. if (counter==10){
  275. plant = "Basket Vine";
  276. water_need = 500;
  277. light_need = 2150;
  278. delayMicroseconds(500);
  279. }
  280. if (counter==11){
  281. plant = "Lipstick Vine";
  282. water_need = 500;
  283. light_need = 2150;
  284. delayMicroseconds(500);
  285. }
  286. if (counter==12){
  287. plant = "Chinese Everg.";
  288. water_need = 500;
  289. light_need = 270;
  290. delayMicroseconds(500);
  291. }
  292. if (counter==13){
  293. plant = "Silver King";
  294. water_need = 500;
  295. light_need = 270;
  296. delayMicroseconds(500);
  297. }
  298. if (counter==14){
  299. plant = "Silver Queen";
  300. water_need = 500;
  301. light_need = 270;
  302. delayMicroseconds(500);
  303. }
  304. if (counter==15){
  305. plant = "Plume Asparagus";
  306. water_need = 500;
  307. light_need = 810;
  308. delayMicroseconds(500);
  309. }
  310. if (counter==16){
  311. plant = "Crystal Anthur.";
  312. water_need = 500;
  313. light_need = 810;
  314. delayMicroseconds(500);
  315. }
  316. if (counter==17){
  317. plant = "Nest Anthur.";
  318. water_need = 500;
  319. light_need = 810;
  320. delayMicroseconds(500);
  321. }
  322. if (counter==18){
  323. plant = "Flamingo Flower";
  324. water_need = 500;
  325. light_need = 810;
  326. delayMicroseconds(500);
  327. }
  328. if (counter==19){
  329. plant = "Zebra Plant";
  330. water_need = 500;
  331. light_need = 2150;
  332. delayMicroseconds(500);
  333. }
  334. if (counter==20){
  335. plant = "Wax Cissus";
  336. water_need = 500;
  337. light_need = 2150;
  338. delayMicroseconds(500);
  339. }
  340. L_min = 600*light_need; // light need for 15min
  341. light_requirement_1 = 0;
  342. light_requirement_2 = 0;
  343. light_requirement_3 = 0;
  344. time = 0;
  345. lcd.clear();
  346. lcd.setCursor(1, 0);
  347. lcd.print(plant);
  348. }
  349. }
  350.  
  351. void loop() {
  352.  
  353. plant_config();
  354.  
  355. if ((light_requirement_1 <= L_min && light_requirement_2 <= L_min && light_requirement_3 <= L_min) && time>2700){ //45min
  356. digitalWrite(lampPin,HIGH);
  357. while (light_requirement_1 <= L_min && light_requirement_2 <= L_min && light_requirement_3 <= L_min){
  358. digitalWrite(lampPin,HIGH);
  359. }
  360. }
  361. else {
  362. digitalWrite(lampPin,LOW);
  363. }
  364. if ((light_requirement_1 >= L_min || light_requirement_2 >= L_min || light_requirement_3 >= L_min) && time<3600){
  365. for(int i = 0; i<1600; i++){
  366. digitalWrite(stepPin_Pot,HIGH);
  367. delayMicroseconds(500);
  368. digitalWrite(stepPin_Pot,LOW);
  369. delayMicroseconds(500);
  370. }
  371. shield_position = 1;
  372. int diff_time = 3600 - time;// 3600 is full day time
  373. while (diff_time>0){
  374. diff_time = 3600 - time;
  375. }
  376. }
  377. }
  378.  
  379.  
  380. ISR(TIMER1_COMPA_vect){
  381. time += 1;
  382. int light_value_1 = analogRead(light_sensor_Pin_1);
  383. int light_value_2 = analogRead(light_sensor_Pin_2);
  384. int light_value_3 = analogRead(light_sensor_Pin_3);
  385. int humidity_value = analogRead(humidity_sensor);
  386.  
  387. if (humidity_value < water_need){
  388. digitalWrite(pumpPin, HIGH);
  389. }
  390. if (humidity_value > water_need+30){
  391. digitalWrite(pumpPin, LOW);
  392. }
  393.  
  394. light_requirement_1 += light_value_1;
  395. light_requirement_2 += light_value_2;
  396. light_requirement_3 += light_value_3;
  397.  
  398. if (digitalRead(lampPin) == LOW && (light_requirement_1 >= 10000 || (time >= 600 && light_requirement_1 < 1000))){ //10min
  399. light_requirement_1 = 0;
  400. for(int i = 0; i<1600; i++){
  401. digitalWrite(stepPin_Pot,HIGH);
  402. delayMicroseconds(500);
  403. digitalWrite(stepPin_Pot,LOW);
  404. delayMicroseconds(500);
  405. }
  406. }
  407. else if (digitalRead(lampPin) == LOW && (light_requirement_2 >= 18000 || (time >= 600 && light_requirement_2 < 18000))){ //10min
  408. light_requirement_2 = 0;
  409. for(int i = 0; i<1600; i++){
  410. digitalWrite(stepPin_Pot,HIGH);
  411. delayMicroseconds(500);
  412. digitalWrite(stepPin_Pot,LOW);
  413. delayMicroseconds(500);
  414. }
  415. }
  416. else if (digitalRead(lampPin) == LOW && (light_requirement_3 >= 18000 || (time >= 600 && light_requirement_3 < 18000))){ //10min
  417. light_requirement_3 = 0;
  418. for(int i = 0; i<1600; i++){
  419. digitalWrite(stepPin_Pot,HIGH);
  420. delayMicroseconds(500);
  421. digitalWrite(stepPin_Pot,LOW);
  422. delayMicroseconds(500);
  423. }
  424. }
  425.  
  426. else if (time>3600) {
  427. if (shield_position==1){
  428. shield_position = 0;
  429. for(int i = 3200; i>0; i--){
  430. digitalWrite(stepPin_Shield,HIGH);
  431. delayMicroseconds(500);
  432. digitalWrite(stepPin_Shield,LOW);
  433. delayMicroseconds(500);
  434. }
  435. }
  436. }
  437. else if (time>3600){ // 60min
  438. time = 0;
  439. light_requirement_1 = 0;
  440. light_requirement_2 = 0;
  441. light_requirement_3 = 0;
  442. }
  443.  
  444. TCNT1 = 0;
  445. }
  446.  
  447.  
Tags: Arduino
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement