Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal_I2C.h>
- #include <string.h>
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- const int dirPin = 2;
- const int stepPin_Pot = 3;
- const int dirPin_Shield = 4;
- const int stepPin_Shield = 9;
- const int light_sensor_Pin_1 = A1;
- const int light_sensor_Pin_2 = A2;
- const int light_sensor_Pin_3 = A3;
- const int light_sensor_Pin_4 = A4;
- const int humidity_sensor = A0;
- const int lampPin = 5;
- const int pumpPin = 6;
- const int buttonPin1 = 7;
- const int buttonPin2 = 8;
- const int encoder1 = 11;
- const int encoder2 = 12;
- int light_requirement_1 = 0;
- int light_requirement_2 = 0;
- int light_requirement_3 = 0;
- int time = 0;
- int water_need = 400;
- int light_need = 400;
- int buttonState1 = 0;
- int buttonState2 = 0;
- int counter = 0;
- int shield_position = 0;
- int L_min = 3600*light_need;
- String plant = "";
- void setup() {
- cli();//stop interrupts
- pinMode(stepPin_Pot, OUTPUT);
- pinMode(dirPin, OUTPUT);
- pinMode(dirPin_Shield, OUTPUT);
- pinMode(lampPin, OUTPUT);
- pinMode(pumpPin, OUTPUT);
- pinMode(buttonPin1, INPUT);
- pinMode(buttonPin2, INPUT);
- pinMode(encoder1, INPUT);
- pinMode(encoder2, INPUT);
- digitalWrite(dirPin,HIGH);
- digitalWrite(dirPin_Shield,HIGH);
- Serial.begin(9600);
- TCCR1A = 0; //register 1A reset to 0
- TCCR1B = 0; //register 2A reset to 0
- TCCR1B |= B00000101; // prescaler 1024, Pq 1B et pas 1A
- TCNT1 = 0; // timer1 reset to 0
- TIMSK1 |= B00000010; //Set OCIE1A to 1 so we enable compare match A
- OCR1A = 15625; // 1s every 15625 pulses
- sei();//allow interrupts
- lcd.init();
- lcd.backlight();
- lcd.setCursor(1, 0);
- lcd.print("Choose Plant");
- reset_pos();
- }
- void reset_pos() {
- while (digitalRead(encoder1)==LOW){
- digitalWrite(stepPin_Pot,HIGH);
- delayMicroseconds(500);
- digitalWrite(stepPin_Pot,LOW);
- delayMicroseconds(500);
- }
- while (digitalRead(encoder2)==LOW){
- digitalWrite(stepPin_Shield,HIGH);
- delayMicroseconds(500);
- digitalWrite(stepPin_Shield,LOW);
- delayMicroseconds(500);
- }
- }
- void plant_config() {
- buttonState1 = digitalRead(buttonPin1);
- buttonState2 = digitalRead(buttonPin2);
- if (buttonState1 == HIGH && counter<20){ // lcd display doesn't show more than the 20 plants
- counter += 1;
- if (counter==1){
- plant = "Sweet Flag";
- water_need = 500;
- light_need = 810;
- delayMicroseconds(500);
- }
- if (counter==2){
- plant = "Min Sweet Flag";
- water_need = 500;
- light_need = 810;
- delayMicroseconds(500);
- }
- if (counter==3){
- plant = "Magic Flower";
- water_need = 500;
- light_need = 810;
- delayMicroseconds(500);
- }
- if (counter==4){
- plant = "Maidenhair fern";
- water_need = 500;
- light_need = 810;
- delayMicroseconds(500);
- }
- if (counter==5){
- plant = "Crinkle-leaf";
- water_need = 500;
- light_need = 810;
- delayMicroseconds(500);
- }
- if (counter==6){
- plant = "Plover Eggs";
- water_need = 500;
- light_need = 810;
- delayMicroseconds(500);
- }
- if (counter==7){
- plant = "Silver Vase";
- water_need = 500;
- light_need = 810;
- delayMicroseconds(500);
- }
- if (counter==8){
- plant = "Coral Berry";
- water_need = 500;
- light_need = 810;
- delayMicroseconds(500);
- }
- if (counter==9){
- plant = "Bromeliad";
- water_need = 500;
- light_need = 810;
- delayMicroseconds(500);
- }
- if (counter==10){
- plant = "Basket Vine";
- water_need = 500;
- light_need = 2150;
- delayMicroseconds(500);
- }
- if (counter==11){
- plant = "Lipstick Vine";
- water_need = 500;
- light_need = 2150;
- delayMicroseconds(500);
- }
- if (counter==12){
- plant = "Chinese Everg.";
- water_need = 500;
- light_need = 270;
- delayMicroseconds(500);
- }
- if (counter==13){
- plant = "Silver King";
- water_need = 500;
- light_need = 270;
- delayMicroseconds(500);
- }
- if (counter==14){
- plant = "Silver Queen";
- water_need = 500;
- light_need = 270;
- delayMicroseconds(500);
- }
- if (counter==15){
- plant = "Plume Asparagus";
- water_need = 500;
- light_need = 810;
- delayMicroseconds(500);
- }
- if (counter==16){
- plant = "Crystal Anthur.";
- water_need = 500;
- light_need = 810;
- delayMicroseconds(500);
- }
- if (counter==17){
- plant = "Nest Anthur.";
- water_need = 500;
- light_need = 810;
- delayMicroseconds(500);
- }
- if (counter==18){
- plant = "Flamingo Flower";
- water_need = 500;
- light_need = 810;
- delayMicroseconds(500);
- }
- if (counter==19){
- plant = "Zebra Plant";
- water_need = 500;
- light_need = 2150;
- delayMicroseconds(500);
- }
- if (counter==20){
- plant = "Wax Cissus";
- water_need = 500;
- light_need = 2150;
- delayMicroseconds(500);
- }
- L_min = 600*light_need; // light need min for 15min
- light_requirement_1 = 0;
- light_requirement_2 = 0;
- light_requirement_3 = 0;
- time = 0;
- lcd.clear();
- lcd.setCursor(1, 0);
- lcd.print(plant);
- }
- if (buttonState2 == HIGH && counter>1){ //lcd doesn't change when
- counter -= 1;
- if (counter==1){
- plant = "Sweet Flag";
- water_need = 500;
- light_need = 810;
- delayMicroseconds(500);
- }
- if (counter==2){
- plant = "Min Sweet Flag";
- water_need = 500;
- light_need = 810;
- delayMicroseconds(500);
- }
- if (counter==3){
- plant = "Magic Flower";
- water_need = 500;
- light_need = 810;
- delayMicroseconds(500);
- }
- if (counter==4){
- plant = "Maidenhair fern";
- water_need = 500;
- light_need = 810;
- delayMicroseconds(500);
- }
- if (counter==5){
- plant = "Crinkle-leaf";
- water_need = 500;
- light_need = 810;
- delayMicroseconds(500);
- }
- if (counter==6){
- plant = "Plover Eggs";
- water_need = 500;
- light_need = 810;
- delayMicroseconds(500);
- }
- if (counter==7){
- plant = "Silver Vase";
- water_need = 500;
- light_need = 810;
- delayMicroseconds(500);
- }
- if (counter==8){
- plant = "Coral Berry";
- water_need = 500;
- light_need = 810;
- delayMicroseconds(500);
- }
- if (counter==9){
- plant = "Bromeliad";
- water_need = 500;
- light_need = 810;
- delayMicroseconds(500);
- }
- if (counter==10){
- plant = "Basket Vine";
- water_need = 500;
- light_need = 2150;
- delayMicroseconds(500);
- }
- if (counter==11){
- plant = "Lipstick Vine";
- water_need = 500;
- light_need = 2150;
- delayMicroseconds(500);
- }
- if (counter==12){
- plant = "Chinese Everg.";
- water_need = 500;
- light_need = 270;
- delayMicroseconds(500);
- }
- if (counter==13){
- plant = "Silver King";
- water_need = 500;
- light_need = 270;
- delayMicroseconds(500);
- }
- if (counter==14){
- plant = "Silver Queen";
- water_need = 500;
- light_need = 270;
- delayMicroseconds(500);
- }
- if (counter==15){
- plant = "Plume Asparagus";
- water_need = 500;
- light_need = 810;
- delayMicroseconds(500);
- }
- if (counter==16){
- plant = "Crystal Anthur.";
- water_need = 500;
- light_need = 810;
- delayMicroseconds(500);
- }
- if (counter==17){
- plant = "Nest Anthur.";
- water_need = 500;
- light_need = 810;
- delayMicroseconds(500);
- }
- if (counter==18){
- plant = "Flamingo Flower";
- water_need = 500;
- light_need = 810;
- delayMicroseconds(500);
- }
- if (counter==19){
- plant = "Zebra Plant";
- water_need = 500;
- light_need = 2150;
- delayMicroseconds(500);
- }
- if (counter==20){
- plant = "Wax Cissus";
- water_need = 500;
- light_need = 2150;
- delayMicroseconds(500);
- }
- L_min = 600*light_need; // light need for 15min
- light_requirement_1 = 0;
- light_requirement_2 = 0;
- light_requirement_3 = 0;
- time = 0;
- lcd.clear();
- lcd.setCursor(1, 0);
- lcd.print(plant);
- }
- }
- void loop() {
- plant_config();
- if ((light_requirement_1 <= L_min && light_requirement_2 <= L_min && light_requirement_3 <= L_min) && time>2700){ //45min
- digitalWrite(lampPin,HIGH);
- while (light_requirement_1 <= L_min && light_requirement_2 <= L_min && light_requirement_3 <= L_min){
- digitalWrite(lampPin,HIGH);
- }
- }
- else {
- digitalWrite(lampPin,LOW);
- }
- if ((light_requirement_1 >= L_min || light_requirement_2 >= L_min || light_requirement_3 >= L_min) && time<3600){
- for(int i = 0; i<1600; i++){
- digitalWrite(stepPin_Pot,HIGH);
- delayMicroseconds(500);
- digitalWrite(stepPin_Pot,LOW);
- delayMicroseconds(500);
- }
- shield_position = 1;
- int diff_time = 3600 - time;// 3600 is full day time
- while (diff_time>0){
- diff_time = 3600 - time;
- }
- }
- }
- ISR(TIMER1_COMPA_vect){
- time += 1;
- int light_value_1 = analogRead(light_sensor_Pin_1);
- int light_value_2 = analogRead(light_sensor_Pin_2);
- int light_value_3 = analogRead(light_sensor_Pin_3);
- int humidity_value = analogRead(humidity_sensor);
- if (humidity_value < water_need){
- digitalWrite(pumpPin, HIGH);
- }
- if (humidity_value > water_need+30){
- digitalWrite(pumpPin, LOW);
- }
- light_requirement_1 += light_value_1;
- light_requirement_2 += light_value_2;
- light_requirement_3 += light_value_3;
- if (digitalRead(lampPin) == LOW && (light_requirement_1 >= 10000 || (time >= 600 && light_requirement_1 < 1000))){ //10min
- light_requirement_1 = 0;
- for(int i = 0; i<1600; i++){
- digitalWrite(stepPin_Pot,HIGH);
- delayMicroseconds(500);
- digitalWrite(stepPin_Pot,LOW);
- delayMicroseconds(500);
- }
- }
- else if (digitalRead(lampPin) == LOW && (light_requirement_2 >= 18000 || (time >= 600 && light_requirement_2 < 18000))){ //10min
- light_requirement_2 = 0;
- for(int i = 0; i<1600; i++){
- digitalWrite(stepPin_Pot,HIGH);
- delayMicroseconds(500);
- digitalWrite(stepPin_Pot,LOW);
- delayMicroseconds(500);
- }
- }
- else if (digitalRead(lampPin) == LOW && (light_requirement_3 >= 18000 || (time >= 600 && light_requirement_3 < 18000))){ //10min
- light_requirement_3 = 0;
- for(int i = 0; i<1600; i++){
- digitalWrite(stepPin_Pot,HIGH);
- delayMicroseconds(500);
- digitalWrite(stepPin_Pot,LOW);
- delayMicroseconds(500);
- }
- }
- else if (time>3600) {
- if (shield_position==1){
- shield_position = 0;
- for(int i = 3200; i>0; i--){
- digitalWrite(stepPin_Shield,HIGH);
- delayMicroseconds(500);
- digitalWrite(stepPin_Shield,LOW);
- delayMicroseconds(500);
- }
- }
- }
- else if (time>3600){ // 60min
- time = 0;
- light_requirement_1 = 0;
- light_requirement_2 = 0;
- light_requirement_3 = 0;
- }
- TCNT1 = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement