How can I make the led run Left to Right only instead of running both ways when SW3 is pressed.

void main(void) {
TRISA = 0x00;
LATA = 0x00;

while(1) {
int i;
int j;

// Right to Left sequence
for(int i=0; i<8; i++) {
LATA = (1 << i);
delayFunc();
}

// Left to Right sequence if SW3 is pressed
if(PORTDbits.RA5 == 0) {
for(j=6; j>=-1; j--) {
LATA |= (1<<j);
delayFunc();
}

}
}

return -1;
}

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

for(i = 0; i < 1000; i++){
for(j=0; j < 20; j++){
k = j;
}
}
}

To make the LED run from left to right only when SW3 is pressed, you can modify the code as follows:

1. Inside the main while loop, add a condition to check if SW3 is pressed before executing the right to left sequence.

2. Replace the for loop for the left to right sequence with a single for loop that runs from 6 to -1 (instead of 8 to -1).

Here is the modified code:

```c
void main(void) {
TRISA = 0x00;
LATA = 0x00;

while(1) {
int i;
int j;

// Right to Left sequence
for(int i=0; i<8; i++) {
LATA = (1 << i);
delayFunc();
}

// Left to Right sequence if SW3 is pressed
if(PORTDbits.RA5 == 0) {
for(j=6; j>=-1; j--) {
LATA = (1 << j);
delayFunc();
}
}
}

return -1;
}

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

for(i = 0; i < 1000; i++){
for(j=0; j < 20; j++){
k = j;
}
}
}
```

Note: Please ensure that the pin assignments and the delay function are appropriate for your specific microcontroller setup.

To make the LED run only from left to right when SW3 is pressed, you can modify the code as follows:

1. Move the declaration of variable `j` outside the while loop, before the main function.
2. Change the condition in the for loop for `j` to `j <= 6` instead of `j >= -1`.
3. Change the bitwise OR assignment (`|=`) to bitwise AND (`&=`) on the line `LATA |= (1<<j);`.

Here's the modified code:

```c
void main(void) {
TRISA = 0x00;
LATA = 0x00;

int j; // Declare j outside the while loop

while(1) {
int i;

// Right to Left sequence
for(int i=0; i<8; i++) {
LATA = (1 << i);
delayFunc();
}

// Left to Right sequence if SW3 is pressed
if(PORTDbits.RA5 == 0) {
for(j=6; j>=0; j--) { // Change condition to j <= 6
LATA &= ~(1<<j); // Change |= to &= and ~(1<<j)
delayFunc();
}

}
}

return -1;
}

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

for(i = 0; i < 1000; i++){
for(j=0; j < 20; j++){
k = j;
}
}
}
```

Note that this assumes that the LED is connected to the LATA pins, and the switch SW3 is connected to the RA5 pin.

To make the LED run only from left to right when SW3 is pressed and not in both directions, you can modify the code as follows:

1. Define a variable `direction` to keep track of the LED sequence direction. Initialize it as 1 for right to left and 0 for left to right.

2. Inside the main loop, instead of using a separate loop for right to left sequence, use `for` loop from `j=0` to `7` when `direction` is 1.

3. When SW3 is pressed, set `direction` to 0 to indicate left to right sequence.

4. Modify the loop for left to right sequence with the condition `j>=0` instead of `j>=-1`. This ensures that only 7 LEDs will be lit in the left to right sequence.

Here is the modified code:

```c
void main(void) {
TRISA = 0x00;
LATA = 0x00;

int direction = 1; // 1 indicates right to left sequence, 0 indicates left to right sequence

while(1) {
int i;
int j;

// Right to Left sequence
if (direction == 1) {
for(i=0; i<8; i++) {
LATA = (1 << i);
delayFunc();
}
}

// Left to Right sequence if SW3 is pressed
if (PORTDbits.RA5 == 0) {
direction = 0; // Set direction to 0 for left to right sequence
for(j=6; j>=0; j--) {
LATA = (1 << j);
delayFunc();
}
} else {
direction = 1; // Set direction back to 1 for right to left sequence
}
}

return -1;
}

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

for(i = 0; i < 1000; i++){
for(j=0; j < 20; j++){
k = j;
}
}
}
```

With this modification, the LED will only run from left to right when SW3 is pressed.