I am doing a project which has a custom made board based on PIC32 MCU
. It has 30 Inputs
and 10 Output
. I have to monitor inputs and send there information to UART. Each inputs is defined a code. For example: if input 1 is ON,then data to be send is 11
{'1'--> Input 1, '1' --> ON}
. If input 1 is OFF, then data to be send is 10
{'1'--> Input 1, '0' --> OFF}
. Similarly is 24th input is on, then 241
, if its off, then 240
and same goes for all the inputs.
Now the final data to be send over UART will contain information about all the inputs:
10 20 30 41 50 61.....271 280 291 300
As I am beginner in programming, what I have done to achieve above syntax is I have defined variables for every inputs.
char input1ON[] = "11";
char input1OFF[] = "10";
bool input1Stat = false;
char input2ON[] = "21";
char input2OFF[] = "20";
bool input2Stat = false;
and same for complete 30 inputs. Now in main function:
if(PORTDbits.RD6 == 1) // if input1 is high
{
input1Stat = true; //make input1 status true
}
if(PORTDbits.RD6 == 0) //if input1 is low
{
input1Stat = false; //make input1 status false
}
if(PORTDbits.RD7 == 1) // if input2 is high
{
input2Stat = true; //make input2 status true
}
if(PORTDbits.RD7 == 0) //if input2 is low
{
input2Stat = false; //make input2 status false
}
/*
* same goes for rest of the inputs
*/
then after all the if statements, I am checking if input1stat is true then strcat it, like below:
if(input1Stat == true) //if input 1 status is true
{
strcat(inputs,input1ON); //concatenate input1ON variable with inputs variable
}
else
strcat(inputs,input1OFF); //concatenate input1OFF variable with inputs variable
if(input2Stat == true) //if input 2 status is true
{
strcat(inputs,input2ON); //concatenate input2ON variable with inputs variable
}
else
strcat(inputs,input2OFF); //concatenate input2OFF variable with inputs variable
So at the end I am just putting inputs
to UART:
putsUART1(inputs);
so as a result I am getting the above string of data on UART. But the problem is there are many if else statements in my code which is making the code unreadable. I think there may be some other way of handling this. As I am beginner, I don't know how to remove all these if else statements. Any one can just give a demo to remove all the if else statements. Thanks.
EDIT
Input 1 and Input 2 is nothing but PORTDbits.RD5
& PORTDbits.RD6
. I used Input 1 and Input 2 just for demo. I have made the changes.