I have a problem with my code, I am programming a microcontroller pic18f4520 to read two reflective sensors, but when I print a status from any sensor become a infinite loop let me explain better
example:
- Sensor A
- Sensor B
- when sensor A is on (0) print "BAG INICIALIZE"
- when sensor A and B are on (0) print "BAG IN PLACE"
- when sensor A and B are off (1) print "BAG REMOVED"
The print is well but is infinite
my referencial code is: everything is inside while(1)
//reading ports when boot
//Sensor A = J13
//Sensor B = J6
void USART_EnviaMsg(char *str);
void USART_EnviaMsg (char *msgout)
{
char letraout;
while(*msgout != 0x00) //As long as I don't come across the null at the end of the string.
{
letraout=*msgout; //Read the pointer letter.
msgout++; //pointer go forward.
while (TXIF==0){} //wait to send
TXREG=letraout; //send the letter.
}
}
while(1){
if ((PORTDbits.RD3 == 0) && (J6 == 1))
{
__delay_ms(100); //Antibounce.
if (PORTDbits.RD3 == 0) //I check again that it is pressed.
{
J6 = 0;
}
}
if (PORTDbits.RD3 == 1)
J6 = 1;
//Bolsa A - J13...
if ((PORTDbits.RD2 == 0) && (J13 == 1)) //J13
{
__delay_ms(100); //Antibounce.
if (PORTDbits.RD2 == 0) //I check again that it is pressed.
{
J13 = 0;
}
}
if (PORTDbits.RD2 == 1)
J13 = 1;
if (J13 == 0 && J6 == 0){
ValorBag = 2;
USART_EnviaMsg("BAG IN PLACE");
USART_EnviaMsg(CRLF);
}
if (J13 == 0 && J6 == 1){
ValorBag = 0;
USART_EnviaMsg("BAG PUTTING");
USART_EnviaMsg(CRLF);
}
if (J13 == 1 && J6 == 0){
ValorBag = 0;
USART_EnviaMsg("BAG ERROR");
USART_EnviaMsg(CRLF);
}
if ((J13 && J6)== 1){
ValorBag = 1;
USART_EnviaMsg("BAG REMOVED");
USART_EnviaMsg(CRLF);
}
}
if some condition is true print infinite loop example:
"BAG REMOVED"
"BAG REMOVED"
"BAG REMOVED"
"BAG REMOVED"
"BAG REMOVED"
"BAG REMOVED"
"BAG REMOVED"
while(1)statement but you neverbreakout of it.while(1)loop.