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.
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;
}
}
}
1 answer