Electronic – Input and UART error in PIC32

inputpicuart

I have a PIC32 starter kit and I am making a program such that when I apply input 1, MCU will send this data to my PC using UART and INPUT 1 ON will be displayed on terminal, same goes for input 2 & 3. These inputs are coming from opto couplers and these are basically 3.1v. So whenever I apply these inputs, MCU pin become high, data is send to PC using UART and INPUT 1 ON or INPUT 2 ON is displayed.

But the problem is let say, I have applied input 1 and terminal is displaying INPUT 1 ON and at the same time if I apply input 2, INPUT 1 ON goes off and INPUT 2 ON started displaying. I want that both INPUT 1 ON INPUT 2 ON should be displayed and if I apply input 3 at the same time INPUT 3 ON should also be displayed along with INPUT 1 ON INPUT 2 ON. I dont know how to make logic for this. Can anyone just help me with this. Thanks

CODE:

    if(PORTAbits.RA6 == 0)
    {
        putsUART2(">>Input: 1 ON\r\n");

    }
    if(PORTAbits.RA6 == 1)
    {
        putsUART2(">>Input: 1 OFF\r\n");

    }
    if(PORTAbits.RA7 == 0)
    {
        putsUART2(">>Input: 2 ON\r\n");

    }
    if(PORTAbits.RA7 == 1)
    {
        putsUART2(">>Input: 2 OFF\r\n");

    }
    if(PORTGbits.RG13 == 0)
    {
        putsUART2(">>Input: 3 ON\r\n");

    }
    if(PORTGbits.RG13 == 1)
    {
        putsUART2(">>Input: 3 OFF\r\n");

    }

EDIT

Right now what I am trying to do is if(PORTAbits.RA6 == 0) the it should display Input: 1 ON and if if(PORTAbits.RA6 == 1) then it should display Input: 1 OFF and same for other inputs. But what's happening is when there is no input Input: 1 OFF Input: 2 OFF Input: 3 OFF is displayed but when if(PORTAbits.RA6 == 0) comes it shows Input: 1 ON for just one time and then continuously displays Input: 1 OFF Input: 2 OFF Input: 3 OFF. I dont know why its behaving like this.

Best Answer

Your current code shows (at most) one message, because the second and third parts are in the else statements. Someone deleted J.A. 's answer, but he was totally correct on this part. (Which 'owner' did this deleting an why?? It is half of the solution!)

You second problem is probably that (when you remove the 'else' keywords) each message overwrites the previous one or is written to a separate line. You should write \r\n only once, for instance like this:

char newline = 0;
if(PORTAbits.RA6 == 0){
   putsUART2("Input: 1 ON ");
   newline = 1;
}

if(PORTAbits.RA7 == 0){
   putsUART2("Input: 2 ON ");
   newline = 1;
}

if(PORTGbits.RG13 == 0){
   putsUART2("Input: 3 ON ");
   newline = 1;
}

if( newline ){
   putsUART2("\r\n");
}