Pic Programming - Nested Loops

I'm having some problem with my code and I can't get my two loops working properly. I'm new to C programming so I might have got the basic loop concepts wrong. I've got 10 LED lights connected to my different ports on my microchip which are rotating in order from the first to last, and then back to the first again. The issue I'm facing is that I'm trying to get one of the buttons on the microchip to work as a start for the loop, but my code won't work. If you could take a look at this I'd greatly appreciate it.

void delay(int d);

int main(void)
{

TRISCbits.TRISC3 = 0;
TRISCbits.TRISC4 = 0;
TRISCbits.TRISC5 = 0;
TRISBbits.TRISB4 = 0;
TRISAbits.TRISA9 = 0;
TRISBbits.TRISB2 = 0;
TRISBbits.TRISB0 = 0;
TRISAbits.TRISA1 = 0;
TRISAbits.TRISA11 = 0;

while (1)
{
if (PORTAbits.RA11 == 1) {
PORTCbits.RC3 = 1;
delay(500);
PORTCbits.RC3 = 0;
delay(500);

PORTCbits.RC4 = 1;
delay(500);
PORTCbits.RC4 = 0;
delay(500);

PORTCbits.RC5 = 1;
delay(500);
PORTCbits.RC5 = 0;
delay(500);

PORTBbits.RB4 = 1;
delay(500);
PORTBbits.RB4 = 0;
delay(500);

PORTAbits.RA9 = 1;
delay(500);
PORTAbits.RA9 = 0;
delay(500);

PORTBbits.RB2 = 1;
delay(500);
PORTBbits.RB2 = 0;
delay(500);

PORTBbits.RB0 = 1;
delay(500);
PORTBbits.RB0 = 0;
delay(500);

PORTAbits.RA1 = 1;
delay(500);
PORTAbits.RA1 = 0;
delay(500);

PORTBbits.RB0 = 1;
delay(500);
PORTBbits.RB0 = 0;
delay(500);

PORTBbits.RB2 = 1;
delay(500);
PORTBbits.RB2 = 0;
delay(500);

PORTAbits.RA9 = 1;
delay(500);
PORTAbits.RA9 = 0;
delay(500);

PORTBbits.RB4 = 1;
delay(500);
PORTBbits.RB4 = 0;
delay(500);

PORTCbits.RC5 = 1;
delay(500);
PORTCbits.RC5 = 0;
delay(500);

PORTCbits.RC4 = 1;
delay(500);
PORTCbits.RC4 = 0;
delay(500);
}

else {
TRISCbits.TRISC3 = 0;
TRISCbits.TRISC4 = 0;
TRISCbits.TRISC5 = 0;
TRISBbits.TRISB4 = 0;
TRISAbits.TRISA9 = 0;
TRISBbits.TRISB2 = 0;
TRISBbits.TRISB0 = 0;
TRISAbits.TRISA1 = 0;
TRISAbits.TRISA11 = 0;
}

}

return 1;

}

void delay(int d)
{
int i;

while(d)
{
i = 100;
while (i)
{
i--;
}

d--;
}
}

• What exactly isn't working? If you're hoping that reading RA11 will trigger some action, it'd need to be an input, you've set it as an output and don't ever set its state such that the main 'if' ever does anything. – Phil G Apr 18 at 13:49
• @PhilG I'm sorry if I wasn't clear enough. The button is not working properly. When I start the program, the while loop runs once, and then it stops. I want the loop to start when the button is pressed down, but nothing happens to the lights when I press it down. (The button is called A11, which initially is set to 0, and then I use as a condition in the if-loop.) – D.Jhn Apr 18 at 13:55
• In the ELSE section, shouldn't they be PORTx or LATx not TRISx? I presume you are trying to clear all the outputs when the button is released. As others have said, TRISA11 should be 1. – HandyHowie Apr 18 at 14:16
• Which processor are you using? Are there other functions on A11 that are overriding the GPIO? – HandyHowie Apr 18 at 14:18
• A schematic would also be useful. – HandyHowie Apr 18 at 14:23