# Handling large number of inputs in PIC microcontroller

I am interfacing PIC32 MCU with 24 inputs and UART. When any inputs comes, this data is transferred to PC via uart. I am using multiple if condition like

if(input1==high)
{
putsUART("input 1 ON\n");
}
if(input2==high)
{
putsUART("input 2 ON\n");
}


What this if condition is doing is whenever an input comes, it keep on printing the data like

input 1 ON
input 1 ON
input 1 ON


and it goes on. I want it to be displayed once only. For example, if I applied input1, input 1 ON should be printed once and if remove the input input 1 OFF should be displayed once. I don't know how can I run this if condition only once. Please help. Thanks.!

CODE(just for 3 inputs):

 int main()
{
TRISAbits.TRISA6 = 1;
TRISAbits.TRISA7 = 1;
TRISGbits.TRISG13 = 1;
while(1)
{
if(PORTAbits.RA6 == 0)         //INPUT 1
{
putsUART2("Input: 1 ON\n");
Delayms(1000);
}
if(PORTAbits.RA7 == 0)          //INPUT 2
{
putsUART2("Input: 2 ON\n");
Delayms(1000);
}
if(PORTAbits.RG13 == 0)          //INPUT 3
{
putsUART2("Input: 3 ON\n");
Delayms(1000);
}
}
}

• You need to remember the state of each input, and only output to the UART if it changes. – Roger Rowland Sep 15 '15 at 6:55
• I have edited my question. Can you tell me how can I remember the state for input? – Aircraft Sep 15 '15 at 7:12
• I don't know exactly why your approach is what it is, but why wouldn't you pack your 24 inputs into 3 bytes, and when any one of them changes, or just periodically, sent all three bytes to the computer? – Scott Seidman Sep 15 '15 at 13:13
• @ScottSeidman looks great. Can you give an example.! – Aircraft Sep 16 '15 at 4:18

You need to remember the state of each input, and only output to the UART if it changes.

Something like this, just for one input so you get the idea :

// NB - put these lines outside your loop
uint8_t uLastRA6State = 0;                // last known state of RA6
uint8_t uLastRA7State = 0;                // last known state of RA7
// ... etc.
// use one variable for each input, or if memory is tight, you could
// alternatively use one byte per 8 pins, but this more "wasteful"
// version is easier to understand the principle

while (1)
{
// ...

// NB - put this code inside your loop
if (PORTAbits.RA6 != uLastRA6State)
{
// save current state
uLastRA6State = uLastRA6State ? 0 : 1;

// output change
if (uLastRA6State)
putsUART2("Input: 1 ON\n");
else
putsUART2("Input: 1 OFF\n");
}

// ... do the same for other inputs, e.g.
if (PORTAbits.RA7 != uLastRA7State)
{
// ... similar to above, for RA7
}
}

• thanks for giving me this idea. But its still displaying continuous values – Aircraft Sep 15 '15 at 7:40
• @CZAbhinav Well the code above should never output consecutive ON's or OFF's. Are you saying it is doing so? Are your inputs changing too fast for your UART buffer? – Roger Rowland Sep 15 '15 at 8:15
• yes it is doing so. and I just found out that I have not applied any input but still its showing input 1 ON – Aircraft Sep 15 '15 at 8:27
• You might need a pull-up resistors on your pins to keep the inputs to a stable/default level. If an input isn't connected to anything, it can randomly be 0 or 1.. – m.Alin Sep 15 '15 at 8:55
• I am working on PIC32 starter kit, so I think I dont need any pull ups. Inputs are already stable.! – Aircraft Sep 15 '15 at 8:58