Macrovision

Control y monitoreo del nivel del tanque(hydroponic)

May 18th, 2022 (edited)
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.13 KB | None | 0 0
  1. /*-systema para riego automatico-*/
  2. /*-en este update se reparo el problema del rebote del relay provocado por la hola que produce el removorbedor-*/
  3.  
  4.  
  5. #include <16f628A.h>
  6. #fuses NOWDT
  7. #fuses MCLR
  8. #fuses NOBROWNOUT
  9. #fuses NOLVP
  10. #USE DELAY(INTERNAL=4000000)
  11. #USE FAST_IO (A)
  12. #USE FAST_IO (B)
  13. //-"direccionamiento de ports-//
  14. #byte port_A = 0x05  
  15. #byte port_B = 0x06
  16. #byte PCON = 0x8e
  17. #define Timer1 PIN_A0
  18. #define Timer2 PIN_A1
  19. #define SetUp PIN_A2
  20. #define Save PIN_A3
  21. #define MinLevel PIN_A4
  22. #define MaxLevel PIN_A6
  23. #define LedGrow PIN_A7
  24. #define IE_Pump PIN_B0
  25. #define Evalve2 PIN_B1
  26. #define Evalve1 PIN_B2
  27. #define Evalve3 PIN_B3
  28. #define Level_1 PIN_B4
  29. #define Level_2 PIN_B5
  30. #define Led_Zona2 PIN_B6
  31. #define LedFlor PIN_B7
  32. #define false 0
  33. #define true 1
  34.  
  35. /*-"Declaracion de funciones"-*/
  36.  
  37. void Retardo_05seg(void);
  38. void Retardo_03seg(void);
  39. void Retardo_40seg(void);
  40. Void tmpVaciado(void);
  41.  
  42.  
  43.  
  44. /*-"Declaracion de VARIABLES Globales"-*/
  45.  
  46.  int l=0;
  47.  
  48.  
  49.     VOID MAIN(){                      
  50.         SET_TRIS_A(0x7f);
  51.         SET_TRIS_b(0x00);
  52.         output_b(0);
  53.         output_low(LedGrow);
  54.       //output_high(Level_1);
  55.        
  56.    //rutina para ver por a donde va el programa despues de un apagon O un reset
  57.    
  58.    
  59.    if(!bit_test(PCON,1) && (bit_test(read_eeprom(0x00),0))){ //testeo PCON para saver si fue apagon o reset, PCON==1 RESET, PCON==0 POWER OFF
  60.            BIT_SET(PCON,1);
  61.            output_low(Level_2);
  62.            //output_low(Level_3);
  63.            output_high(Level_1);
  64.            Delay_ms(50);
  65.            if(!bit_test(read_eeprom(0x03),0)){
  66.             output_High(LedGrow);
  67.            goto Growing;
  68.           }
  69.            
  70.           if(bit_test(read_eeprom(0x05),0)) {
  71.             output_High(LedFlor);
  72.            goto Floration;
  73.           }
  74.          
  75.            if(!bit_test(read_eeprom(0x04),0)){
  76.             //output_High(LedFlor);
  77.            goto Growing_room2;
  78.           }
  79.            
  80.       }else if(!bit_test(PCON,1)&& (bit_test(read_eeprom(0x01),0))){
  81.          
  82.          
  83.            BIT_SET(PCON,1);
  84.            output_low(Level_1);        
  85.            output_high(Level_2);
  86.            Delay_ms(50);
  87.            if(!bit_test(read_eeprom(0x03),0)){
  88.            output_High(LedGrow);
  89.            goto Growing;
  90.           }
  91.            
  92.           if(bit_test(read_eeprom(0x05),0)) {
  93.             output_High(LedFlor);
  94.            goto Floration;
  95.           }
  96.          
  97.            if(!bit_test(read_eeprom(0x04),0)){          
  98.            goto Growing_room2;
  99.           }
  100.    
  101.    }else if(bit_test(PCON,1)){    //Si PCON.1==1 esto es un Reset
  102.                                    
  103.            
  104.            
  105.  //////////////// RUTINA PARA SELECCIONAR EL NIVEL DEL TANQUE\\\\\\\\\\\\\\\\\\\\\
  106.            
  107.   while(true){
  108.   while(1){
  109.             write_eeprom(0x00,0xff);// 0x00 para el nivel1 // Esatas son las direcciones de memoria a testiar para saber que nivel
  110.                                                            //estaba seleccionado antes del apagon.
  111.             write_eeprom(0x01,0x00);// SET FLAG ,REGISTRO 0x01=0X00 DE LA EEEPROM  para el nivel2  
  112.             //write_eeprom(0x02,0x00);// 0x02 para el nivel3
  113.             output_low(Level_2);
  114.             //output_low(Level_3);
  115.             output_high(Level_1);
  116.             delay_ms(500);
  117.  
  118.   do{
  119.                if(!input(Save)){      
  120.                  //delay_ms(50);       //Level-1
  121.                   goto Mode;
  122.               }
  123.                
  124.      }while(input(SetUp));
  125.                write_eeprom(0x00,0x00);
  126.                write_eeprom(0x01,0xff);
  127.                //write_eeprom(0x02,0x00);
  128.                output_low(Level_1);
  129.                //output_low(Level_3);
  130.                output_high(Level_2);
  131.                delay_ms(500);
  132.   do{
  133.            if(!input(Save)){          //Level-2
  134.                  //delay_ms(50);
  135.                   goto Mode;
  136.               }
  137.      }while(input(SetUp));
  138.            
  139.      
  140.        } //End of Select
  141.     //}    
  142.     Mode:
  143.    //delay_ms(250);
  144.      //////////////////SUBRRUTINA PARA SELECCIONAR MODO DE TRABAJO///////////
  145.    while(1){
  146.    
  147.                write_eeprom(0x03,0x00);
  148.                output_Low(LedFlor);
  149.                output_High(LedGrow);
  150.                delay_ms(250);
  151.    do{
  152.                
  153.                
  154.             if(!input(Save))
  155.                {  
  156.                  output_low(LedGrow);
  157.                  delay_ms(250);
  158.                  output_High(LedGrow);
  159.                  goto Growing;
  160.                }
  161.              
  162.      }while(input(SetUp));
  163.                  write_eeprom(0x05,0xff);
  164.                  delay_ms(250);
  165.                  output_Low(LedGrow);
  166.                  output_High(LedFlor);
  167.              
  168.   do{
  169.            if(!input(Save))
  170.               {          
  171.                 output_low(LedFlor);
  172.                 delay_ms(250);
  173.                 output_High(LedFlor);
  174.                 goto Floration;
  175.               }
  176.                
  177.            
  178.                
  179.      }while(input(SetUp));
  180.                            
  181.     } //End del Setup.
  182.   }  //End deel while
  183.  }  // End Del IF-Else.
  184.    /////////////////////////////////////////////////////    
  185.  //Rutina que Atiende el llenado del tanque Mas Las Valvulas de Riego y Pumpa.
  186.  Growing:
  187.      write_eeprom(0x04,0xff);      // Clear Flag
  188.      write_eeprom(0x05,0x00);      // Clear Flag
  189.  while(1){
  190.    
  191.  do{  
  192.      if(input(pin_A6)){           // max level pin_A6             ////////////////////////////////////////////////////////////////////////////////
  193.         output_Low(Pin_b3);       // sierro la valvula de llenado // Rutina para el llenado del tanke mientras espero la entrada del timer      //
  194.      }else{                                                       //                                                                            //
  195.         output_High(Pin_b3);      // Pin_b3 valbula de llenado    ////////////////////////////////////////////////////////////////////////////////
  196.  }
  197.  //}while(input(PIN_A0)||input(PIN_A1));
  198.  }while(input(PIN_A0));           // Timer_1 Pin_A0 / espero trigger Timer1 Zona_1            
  199.      output_Low(Pin_b3);          // Cierro la Valbula de llenado                              
  200.                                                                                      
  201.                                                                                                            
  202.      Retardo_40seg();             // Retardo k garantiza k el programa no entre en un loop
  203.      output_low(PIN_B1);          // Evalve2 pin_B1 / valbula de riego de Spot_2               // En este punto el pin B1 se pone a 0 para garantizar que la valvula del Cuarto 2 este serrada
  204.      output_high(PIN_B0);         // Arranco Bomba de Riego.
  205.      Retardo_05seg();             // Wait 5sec
  206.      output_high(PIN_B2);         // Evalve1 pin_B2 / Abro Valvula de riego de Spot_1          // En este punto se comienza la irrigation.
  207.  do{
  208.       output_toggle(LedGrow);                                                                  // Hago parpadear el led(200ms) para indicar que esta irrigando
  209.       delay_ms(250);
  210.  }while(input(PIN_A4));           // Mini lavel Pin_A4                                         // En este punto se monitorea el pin_a4 para saber cuando el tanke este un su nivel minimo.
  211.      //dalay_ms(x)                // Descomentar Linea Paravaciar el tanke completo.)
  212.      output_high(LedGrow);
  213.      output_Low(PIN_B2);          // Sierro la Valbula de Riego del Cuarto_1
  214.      output_Low(PIN_B1);          // Sierro la Valvula De Riego del Cuarto_2
  215.       Retardo_03seg();
  216.      output_Low(PIN_B0);          // Apago la  Bomba de rego
  217.      Retardo_05seg();
  218.  }  //End Del Growing
  219.  
  220.  Floration:
  221.  
  222. while(1){
  223.  output_High(LedFlor);
  224.  write_eeprom(0x05,0xff);          // Set flag
  225.  write_eeprom(0x03,0xff);          // Clear Flag
  226.  write_eeprom(0x04,0xff);          // Clear Flag
  227.  
  228.  do{
  229.     if(input(pin_A6)){             //llenado del Tanque Para riego de Zona_1//la entrada A6 esta connectada al sensor de maximo nivel
  230.     //delay_ms(x);                 //tiempo introducido para evitar feedback en el sensor de maximo nivel
  231.      output_Low(Pin_b3);           //la valvula de llenado esta connectada a la salida B3
  232.      l=1;
  233.     }else if(l==0){
  234.          output_High(Pin_b3);
  235.      }
  236.      if(!input(PIN_A1)){goto IrriG_zona2;}  // Si el timer_2 se sierra ir a la zona_2
  237.     // }while(input(PIN_A0)||input(PIN_A1));
  238.  }while(input(PIN_A0));            //espero trigger Timer1 Zona_1
  239.        
  240. IrriG_Zona1:
  241.      output_Low(Pin_b3);          // Cierro la valvula de llenado
  242.      Retardo_40seg();
  243.      output_low(PIN_B1);          // Sierro valbula de riego de Spot_2
  244.      output_high(PIN_B0);         // Arranco Bomba de Riego.
  245.      Retardo_05seg();             // Espero 5 seg
  246.      output_high(PIN_B2);         // Abro Valvula de riego de Spot_1
  247.      
  248.  do{
  249.      if(!input(PIN_A1) && (input(pin_A6))) {            
  250.          output_low(LedFlor);
  251.          output_Low(Evalve1);
  252.          output_Low(IE_Pump);
  253.          goto IrriG_zona2;
  254.      }ELSE IF(!input(PIN_A1) && (!input(pin_A6))) {
  255.          output_high(Led_Zona2);
  256.          output_low(LedFlor);
  257.          output_Low(Evalve1);
  258.          output_Low(IE_Pump);
  259.      DO{
  260.          output_High(Pin_b3);
  261.        }WHILE(!input(pin_A6));
  262.          output_Low(Pin_b3);
  263.          goto IrriG_zona2;
  264.     }
  265.         output_toggle(LedFlor);
  266.         delay_ms(250);
  267.      
  268.  }while(input(PIN_A4));           // Sensor Minimo lavel Pin_A4, codicion para Apagar bomba y cierrar Valvulas.
  269.   //tmpVaciado();                 //  Descomentar Linea Para vaciar el tanke completo.
  270.  //Growing_room2:
  271.    l=0;
  272.    output_low(LedFlor);
  273.    output_Low(Evalve1);        /////////////////////////////////////////////////
  274.    output_Low(Evalve2);        //                                             //
  275.    Retardo_03seg();            //                                             //
  276.    output_Low(IE_Pump);        //Cierro Las Valvulas Y apago la bomba de riego//
  277.    Retardo_05seg();            //                                             //
  278.                                /////////////////////////////////////////////////
  279.   Growing_room2:
  280.      output_high(Led_Zona2);
  281.      write_eeprom(0x04,0x00);        // Set Flag
  282.      write_eeprom(0x03,0xff);        // Clear Flag
  283.      write_eeprom(0x05,0x00);        // Clear Flag
  284. do{                              
  285.     if(input(pin_A6)){              // llenado del Tanque para Riego de Zona_2
  286.      //delay_ms(x);                 //tiempo introducido para evitar feedback en el sensor de maximo nivel
  287.      output_Low(Pin_b3);
  288.      l=1;                           //clear flag, l es una bandera para evitar revote en el relay conectado a B3 cuando el hagua se
  289.                                                                                                                   mueve en el tanke.
  290.      }else if(l==0){
  291.          output_High(Pin_b3);
  292.      }
  293.      if(!input(PIN_A0)){goto IrriG_Zona1;}
  294.      
  295.  }while(input(PIN_A1));   //espero trigger timer2 Zona_2
  296.  
  297.  IrriG_Zona2:
  298.      output_Low(Pin_b3);         // Cierro la Valvula dellenado
  299.      Retardo_40seg();
  300.      output_low(PIN_B2);         // Cierro Valve_1_Zona_1
  301.      output_high(PIN_B0);        // Arranco la Bomba.
  302.      Retardo_05seg();            // espero 5 seg.
  303.      output_high(PIN_B1);        // Abro la valvula_2 Para regar en Zona_2
  304.      
  305.  do{
  306.      if(!input(PIN_A0) && (input(pin_A6))) {            
  307.          output_low(Led_Zona2);
  308.          output_Low(PIN_B1);
  309.          output_Low(PIN_B0);
  310.          goto IrriG_zona1;
  311.      }ELSE IF(!input(PIN_A0) && (!input(pin_A6))) {
  312.          output_High(LedFlor);
  313.          output_low(Led_Zona2);
  314.          output_Low(PIN_B1);
  315.          output_Low(PIN_B0);
  316.    DO{
  317.          output_High(Pin_b3);
  318.      }WHILE(!input(pin_A6));
  319.          output_Low(Pin_b3);
  320.          goto IrriG_zona1;
  321.  }
  322.      output_toggle(Led_Zona2);
  323.      delay_ms(250);
  324.    
  325.   }while(input(PIN_A4));         // Termino Riego Zona_2
  326.   //tmpVaciado();                // Descomentar Linea Para(espero un tiempo x para vaciar el tanke completo.)
  327.   l=0;                           // set flag == 0
  328.    output_low(Led_Zona2);
  329.    write_eeprom(0x04,0x00);  
  330.   //bit_clear(eeprom 0x04,0);
  331.    output_Low(PIN_B1);           // cierro valvula_2_Zona_2
  332.    Retardo_03seg();              // espero_3seg
  333.    output_Low(PIN_B2);           // cierro valvula_1_zona_1
  334.    output_Low(PIN_B0);           // Apago_la_bomba_de_riego
  335.    Retardo_05seg();              // Retardo de 5seg.
  336.      
  337.   }
  338. }
  339.  
  340. //funciones
  341.  
  342. Void Retardo_05seg(){
  343.  
  344. for(int i=1;i<=5;i++){
  345.       for(long j=0;j<=1000;j++){
  346.        delay_ms(1);
  347.       }
  348.    }
  349.  }
  350.  
  351.  
  352.  Void Retardo_03seg(){
  353.  
  354. for(int i=1;i<=3;i++){
  355.       for(long j=0;j<=1000;j++){
  356.        delay_ms(1);
  357.       }
  358.    }
  359.  }
  360.  
  361.  //funcion para evitar timer overLap
  362.  
  363.  Void Retardo_60seg(){
  364.    for(int i=0;i<=59;i++){
  365.       for(long j=0;j<=1000;j++){
  366.        delay_ms(1);
  367.       }
  368.       output_toggle(LedGrow);
  369.    }
  370.    output_low(LedGrow);
  371.    //i=0
  372.  }
  373.  
  374.  // Retardo para el vaciado del tanke
  375.  Void tmpVaciado(){
  376.  for(int i=0;i<=59;i++){
  377.       for(long j=0;j<=1000;j++){
  378.        delay_ms(1);
  379.       }
  380.      }
  381.     }
Add Comment
Please, Sign In to add comment