interrupt the Left to Right direction sequence when RD6 is pressed on Explorer 16

unsigned int count1 = 500;
unsigned int count2 = 500;
unsigned int timerValue = 0;

int main(void){
TRISA = 0x00; // Set PORTA as output
TRISDbits.TRISD6 = 1; // Set SW3 input

int i;
int j;

while(1) {
// Toggle RUN LED (D10) every 3.5 seconds using Timer1
LATDbits.LATD0 = !LATDbits.LATD0;
delayFunc();

// Knight Rider pattern from Left to Right direction
for(i = 0; i <= 6; i++) {
LATA = (1 << i);
delayFunc();
}

// Knight Rider pattern: Left to Right or Right to Left depending on SW3 state
if (PORTDbits.RD6 == 0) {
for(j = 6; j >= 0; j--) {
LATA = (1 << j);
delayFunc();
}
} else {
for( j = 0; j <= 7; j++) {
LATA = (1 << j);
delayFunc();
}
}
}

return -1;

}

void delayFunc(void)
{
int j,k;
int a;

for(j = 0; j < count1; j++)
{
for(k=0; k < count2; k++)
{
a = 0;
}
}
}

To interrupt the Left to Right direction sequence when RD6 is pressed on Explorer 16, you can use an interrupt service routine. Here's an example of how you can modify the code:

#include <p30Fxxxx.h>
#include <stdlib.h>

_FOSC(CSW_FSCM_OFF & XT_PLL16);
_FWDT(WDT_OFF);

unsigned int count1 = 500;
unsigned int count2 = 500;
unsigned int timerValue = 0;

void __attribute__((interrupt, auto_psv)) _INT0Interrupt(void){
// Interrupt code here
IFS0bits.INT0IF = 0; // Clear INT0 interrupt flag
}

int main(void){
TRISA = 0x00; // Set PORTA as output
TRISDbits.TRISD6 = 1; // Set SW3 input

// Configure INT0 for falling edge detection
INTCON2bits.INT0EP = 1; // Falling edge
IPC0bits.INT0IP = 1; // Set INT0 priority to 1
IFS0bits.INT0IF = 0; // Clear INT0 interrupt flag
IEC0bits.INT0IE = 1; // Enable INT0 interrupt

int i;
int j;

while(1) {
// Toggle RUN LED (D10) every 3.5 seconds using Timer1
LATDbits.LATD0 = !LATDbits.LATD0;
delayFunc();

// Knight Rider pattern from Left to Right direction
for(i = 0; i <= 6; i++) {
LATA = (1 << i);
delayFunc();
}

// Knight Rider pattern: Left to Right or Right to Left depending on SW3 state
if (PORTDbits.RD6 == 0) {
for(j = 6; j >= 0; j--) {
LATA = (1 << j);
delayFunc();
}
} else {
for( j = 0; j <= 7; j++) {
LATA = (1 << j);
delayFunc();
}
}
}

return -1;
}

void delayFunc(void)
{
int j,k;
int a;

for(j = 0; j < count1; j++)
{
for(k=0; k < count2; k++)
{
a = 0;
}
}
}