-1

i'm programing sensors on PIC16F15323-I/P so far i added two sensors ( water level sensor, voltage sensor) the problem is when i add the second sensor it swapped between the inputs the water level sensor takes the output for the voltage sensor and the voltage sensor takes the output of the water level sensor i removed one sensor from the code at the time the output is good for both of them but togather is not. i don't understand what is the problem of my code if anyone can help me please

this is the code

#include "Config.h"
  int result=0;
  int result1=0;
void pins ()
{
   TRISC2 = 1;// set RC as voltage sesnor input 
   TRISC5 = 0;// Green LED
   TRISA0 = 0;//relay output 
   TRISC1 = 0;// water level sensor led 
   TRISA1 = 1;//water level sensor input 
}


 void voltage_sensor ()
{
int result1=0;

// Select convertion clock 16Mhz = 101
    
    ADCON1bits.ADCS0=1;
    ADCON1bits.ADCS1=0;
    ADCON1bits.ADCS2=1;
    
             // to store data ( analog channel)
    
    ADCON0bits.CHS0=0; // store the address for analog pin RC2 voltage sensor
    ADCON0bits.CHS1=1;
    ADCON0bits.CHS2=0;
    ADCON0bits.CHS3=0;
    ADCON0bits.CHS4=1;
    ADCON0bits.CHS5=0;  
       
            // result of input 
    
     ADCON1bits.ADFM=1; 
     
    
             //port configuration
    
    ADCON1bits.ADPREF0=0;
    ADCON1bits.ADPREF1=0;
      
             //set statues bit
    ADCON0bits.GOnDONE=1;//start the conversion 
      
            //switch on ADC
      
    ADCON0bits.ADON =1;// turn on the ADC 
              
    ADCON0bits.GOnDONE==1;// start the conversion 
    
    result1=((ADRESH<<8)+ADRESL);// to turn on all the 10 ADC 
           if (result1<800)
               
        {
           PORTC = 0b100000;  // PORTC1 green light 
           PORTA = 0b000000;//PORTA0 relay 
               
        }
        
        else
        {__delay_ms(10000);
           PORTC = 0b000000;
           PORTA = 0b000001;
        }
     
            }

    void watersensor ()
{
    int result=0;

// Select convertion clock 16Mhz = 101
    
    ADCON1bits.ADCS0=1;
    ADCON1bits.ADCS1=0;
    ADCON1bits.ADCS2=1;
    
             // to store data ( analog channel)
    
    ADCON0bits.CHS0=1; 
    ADCON0bits.CHS1=0;
    ADCON0bits.CHS2=0;
    ADCON0bits.CHS3=0;
    ADCON0bits.CHS4=0;
    ADCON0bits.CHS5=0; // store the address for analog pin RA1 
  
   
            // result of input 
    
     ADCON1bits.ADFM=1; 
   
             //port configuration
    
      ADCON1bits.ADPREF0=0;
      ADCON1bits.ADPREF1=0;
      
             //set statues bit
      ADCON0bits.GOnDONE=1;//start the conversion 
      
            //switch on ADC
      
      ADCON0bits.ADON =1;// turn on the ADC 
              
   ADCON0bits.GOnDONE==1;// start the conversion 
   result=((ADRESH<<8)+ADRESL);// to turn on all the 10 ADC 
      if (result>550)
        {
            PORTC = 0b000010;  // PORTC1 
          
        }
        else 
        {
            PORTC = 0b000000; 
        }
        }

void main() 
{

    pins();
    while(1)
    {
  
        __delay_ms(1);
        {
            voltage_sensor(result1);
            result1=0;
        }
         __delay_ms(1);
        {   
            watersensor(result);
            result=0;
    }
}
}
loolysh
  • 9
  • 2
  • It would probably help if you would learn a bit more about coding in the c language first. There are some things I don't understand: Why are you calling the functions `*sensor` with arguments when they are declared without any? And why are you globally defining the `result` variables and also locally inside the functions? – Jan Oct 25 '20 at 07:12
  • i knew about these – loolysh Oct 25 '20 at 23:14
  • i don't think you are answering my question thought – loolysh Oct 25 '20 at 23:14
  • would changing the argument help? – loolysh Oct 25 '20 at 23:27
  • I don't know if it would solve your problem, but if you declutter your code, do proper formatting (indentation), use function calls and variables like they should be used and try to eliminate redundant code-parts then it would be a lot easier to help you with your code. I asked those questions because I don't understand what you're doing in your code and thus cannot help you. Also there is probably a typo `ADCON0bits.GOnDONE == 1;` is a comparison without any reason... it should probably just be an equals-sign. – Jan Oct 26 '20 at 06:48
  • Maybe its also faulty that you are writing to `PORTC` in both functions. Also what do you mean by "one sensor get the output of another sensor as input"? I mean the sensors get their input from the environment don't they? – Jan Oct 26 '20 at 06:54
  • like i'm attaching the water level sensor to yelow light, and voltage sensor to green light and relay but when i compile it, the water level sensor takes the green light and relay and voltage sensor takes the yellow LED which doesn't make sense – loolysh Oct 26 '20 at 23:13

1 Answers1

1

Actually it is very hard to debug your code and I cannot foresee what you are really trying to do with your code

the problem is when i add the second sensor it swapped between the inputs the water level sensor takes the output for the voltage sensor and the voltage sensor takes the output of the water level sensor

What do you mean by "takes the output"? What do you mean by "it swapped between the inputs"?

Other than that I do not really understand what you are trying to do and ask I have one little shot into the blue:

Possible fix

Try to change you assignments to PORTC like so (not overwriting the previous state)

Also check if PORTC (and the other PORTS) really just have a width of 6 bit! If they don't you might need to change your assignments accordingly.

/* ... */

void voltage_sensor() {

  /* ... your code */

  if (result1 < 800)

  {
    PORTC = PORTC | 0b100000; // PORTC1 green light
    PORTA = 0b000000; // PORTA0 relay

  }

  else {
    __delay_ms(10000);
    PORTC = PORTC & 0b011111;
    PORTA = 0b000001;
  }
}

void watersensor() {

  /* ... your code */

  if (result > 550) {
    PORTC = PORTC | 0b000010; // PORTC1

  } else {
    PORTC = PORTC & 0b111101;
  }
}

/* ... */

Jan
  • 2,025
  • 17
  • 27
  • like i'm attaching the water level sensor to yelow light, and voltage sensor to green light and relay but when i compile it, the water level sensor takes the green light and relay and voltage sensor takes the yellow LED which doesn't make sense thats what i mean by swapped – loolysh Oct 26 '20 at 23:14