In this project, you will be using the Explorer 16 development board. Specifically, you will be using the 8 LEDs on the board to generate a Knight Rider Pattern on those LEDs. Please, check Explorer16.pdf for pin connections.

Write an application (in C) which implement the sequence as below:

1. Make all the LEDs OFF.
2. Make LED D3 (Right most) to be ON .
3. After a while (a delay of your choice), make next (to the Left) LED to be ON.
4. After a while, make the next LED to be ON. Repeat this process until all the 8 LEDs (from Right most to LEFT most) becomes ON.
5. Repeat the above processes (1-4), this time, starting from LEFT most LED and walk to the RIGHT most.
6. Repeat the above sequence (1-5) infinitely.

5 answers

```c
#include <p24Fxxxx.h>

#define LED_RANGE 8

// Function to set all LEDs off
void turnOffAllLEDs(){
LATB &= 0xFF00; // Clear all bits except the first 8
}

// Function to turn on the specified LED
void turnOnLED(int led){
LATBINV = (1 << (led-1)); // Set the corresponding bit to turn on LED
__delay_ms(500); // Delay for a short period to visualize the pattern
}

int main(){
TRISB = 0xFF00; // Set pins RB8-RB15 as outputs
turnOffAllLEDs(); // Make all LEDs off

while(1){
// Toggle LEDs from Right most to Left
for(int i = LED_RANGE; i >= 1; i--){
turnOnLED(i);
}

turnOffAllLEDs(); // Make all LEDs off

// Toggle LEDs from Left most to Right
for(int i = 1; i <= LED_RANGE; i++){
turnOnLED(i);
}

turnOffAllLEDs(); // Make all LEDs off
}

return 0;
}
```

Note: The code assumes that the LEDs are connected to the PORTB pins RB8-RB15 on the Explorer 16 development board. Make sure to refer to the "Explorer16.pdf" file for the correct pin connections. Additionally, the code uses the built-in __delay_ms() function to provide a delay of 500 milliseconds. Adjust the delay as desired to achieve the desired pattern speed.
Can you make the code simpler?
Certainly! Here's a simpler version of the code:

```c
#include <xc.h>
#include <stdbool.h>

#define LED_RANGE 8
#define DELAY_MS 500

void delay(unsigned int ms) {
for (unsigned int i = 0; i < ms; i++) {
for (unsigned int j = 0; j < 3125; j++) {} // Delay loop for 1 ms
}
}

int main() {
TRISB = 0xFF00; // Set pins RB8-RB15 as outputs
LATB = 0xFF00; // Turn off all LEDs initially

while (true) {
// Knight rider pattern from right to left
for (int led = LED_RANGE; led >= 1; led--) {
LATBINV = (1 << (led - 1)); // Toggle the corresponding LED
delay(DELAY_MS);
}

// Knight rider pattern from left to right
for (int led = 1; led <= LED_RANGE; led++) {
LATBINV = (1 << (led - 1)); // Toggle the corresponding LED
delay(DELAY_MS);
}
}

return 0;
}
```

This version uses the built-in delay() function to create a delay of 1 millisecond. The delay is called in a loop to achieve the desired delay time. The code uses the LATBINV register to toggle the corresponding LED on and off. The while loop ensures that the pattern repeats infinitely.

Note: Make sure to include the appropriate XC header file for your specific microcontroller and configure your development environment accordingly.
Part B:

Utilize SW3 of the explorer 16 board to change the direction of the Knight Rider Pattern designed in Part A. Any time, if SW3 is pushed down and hold, the above pattern starts from LEFT to RIGHT and when SW3 is released, the pattern walks from RIGHT to LEFT (as in Part A).
To implement the functionality of changing the direction of the Knight Rider pattern using SW3, you can modify the code as follows:

```c
#include <xc.h>
#include <stdbool.h>

#define LED_RANGE 8
#define DELAY_MS 500

void delay(unsigned int ms) {
for (unsigned int i = 0; i < ms; i++) {
for (unsigned int j = 0; j < 3125; j++) {} // Delay loop for 1 ms
}
}

void toggleDirection(bool *direction) {
if (*direction) {
LATB = 0xFF00; // Turn off all LEDs
*direction = false;
} else {
*direction = true;
}
}

int main() {
TRISB = 0xFF00; // Set pins RB8-RB15 as outputs
TRISBbits.TRISB4 = 1; // Set SW3 (RB4) as input

bool direction = true; // Flag to track pattern direction

while (true) {
if (PORTBbits.RB4 == 0) { // SW3 is pressed
toggleDirection(&direction);
while (PORTBbits.RB4 == 0) {} // Wait until SW3 is released
}

if (direction) { // Knight rider pattern from right to left
for (int led = LED_RANGE; led >= 1; led--) {
LATBINV = (1 << (led - 1)); // Toggle the corresponding LED
delay(DELAY_MS);
}
} else { // Knight rider pattern from left to right
for (int led = 1; led <= LED_RANGE; led++) {
LATBINV = (1 << (led - 1)); // Toggle the corresponding LED
delay(DELAY_MS);
}
}
}

return 0;
}
```

In this updated code, SW3 is configured as an input by setting the TRISBbits.TRISB4 register bit to 1. The toggleDirection() function is used to toggle the direction flag between true and false depending on the state of SW3. When SW3 is pressed and held, the direction flag is changed, and when it's released, the flag remains unchanged.

The main loop checks the state of SW3 using PORTBbits.RB4. If SW3 is pressed, the toggleDirection() function is called to change the direction of the pattern. The pattern is then executed accordingly based on the value of the direction flag.

Lastly, the code includes a delay of 500 milliseconds between LED toggles for better visualization of the pattern. Adjust the delay as desired.
Similar Questions
  1. Project #1:Hardware: In this project, you will be using the Explorer 16 development board. Specifically, you will be using the 8
    1. answers icon 1 answer
    1. answers icon 3 answers
  2. New Zealand and Tasmania were named by a(1 point) Responses German explorer. German explorer. British explorer. British
    1. answers icon 1 answer
    1. answers icon 1 answer
more similar questions