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.

```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.

Sure! Here's a step-by-step guide to implementing the Knight Rider Pattern on the Explorer 16 development board using C:

1. Set up the necessary pin connections as per the Explorer16.pdf document.

2. Include the necessary header files and define the delay value:
```c
#include <xc.h>

#define DELAY_TIME 500 // The delay time in milliseconds (ms)
```

3. Create a function to set all LEDs to OFF:
```c
void turnOffAllLEDs() {
LATB = 0x00; // Turn off all LEDs
}
```

4. Create a function to turn on a specific LED:
```c
void turnOnLED(int ledNumber) {
LATBbits.LATB(8 - ledNumber) = 1; // Turn on the specified LED
}
```

5. Create a function to generate the Knight Rider pattern:
```c
void generateKnightRiderPattern() {
int i;

while (1) { // Infinite loop for continuous pattern generation
// Right to Left sequence (1-4)
for (i = 1; i <= 8; i++) {
turnOffAllLEDs(); // Step 1: Turn off all LEDs
turnOnLED(i); // Step 2-4: Turn on LEDs from right to left
__delay_ms(DELAY_TIME); // Delay for specified time
}

// Left to Right sequence (5)
for (i = 7; i >= 2; i--) {
turnOffAllLEDs(); // Step 1: Turn off all LEDs
turnOnLED(i); // Step 5: Turn on LEDs from left to right
__delay_ms(DELAY_TIME); // Delay for specified time
}
}
}
```

6. In the main function, initialize the necessary configurations and call the `generateKnightRiderPattern` function:
```c
int main(void) {
TRISB = 0x00; // Set PORTB as output (for LEDs)
turnOffAllLEDs(); // Turn off all LEDs initially

generateKnightRiderPattern(); // Call the pattern generation function

return 0;
}
```

That's it! Compile and upload the code to your Explorer 16 development board, and you should see the Knight Rider pattern being generated on the 8 LEDs.

To implement the Knight Rider pattern on the 8 LEDs of the Explorer 16 development board, you can follow these steps:

Step 1: Setting up the pin connections
Refer to the Explorer16.pdf documentation to determine the pin connections for the 8 LEDs. The documentation should provide the pin numbers or labels for each LED. Make sure you understand which pins correspond to which LEDs on the board.

Step 2: Writing the code in C
You can write a C program to control the LEDs on the board. Here's an example implementation:

```c
#include <xc.h>

// Define the LED pins
#define LED1 LATBbits.LATB0
#define LED2 LATBbits.LATB1
#define LED3 LATBbits.LATB2
#define LED4 LATBbits.LATB3
#define LED5 LATBbits.LATB4
#define LED6 LATBbits.LATB5
#define LED7 LATBbits.LATB6
#define LED8 LATBbits.LATB7

// Delay function (adjust the delay time as needed)
void delay(unsigned int milliseconds) {
unsigned int i, j;
for (i = 0; i < milliseconds; i++) {
for (j = 0; j < 1000; j++) {
// Delay loop
}
}
}

// Knight Rider pattern function
void knightRider() {
// Turn off all LEDs
LED1 = 0;
LED2 = 0;
LED3 = 0;
LED4 = 0;
LED5 = 0;
LED6 = 0;
LED7 = 0;
LED8 = 0;

// Delay for a while
delay(1000);

// Walk from right to left
LED1 = 1;
delay(1000);
LED2 = 1;
delay(1000);
LED3 = 1;
delay(1000);
LED4 = 1;
delay(1000);
LED5 = 1;
delay(1000);
LED6 = 1;
delay(1000);
LED7 = 1;
delay(1000);
LED8 = 1;
delay(1000);

// Walk from left to right
LED8 = 0;
delay(1000);
LED7 = 0;
delay(1000);
LED6 = 0;
delay(1000);
LED5 = 0;
delay(1000);
LED4 = 0;
delay(1000);
LED3 = 0;
delay(1000);
LED2 = 0;
delay(1000);
LED1 = 0;
delay(1000);
}

// Main program loop
int main() {
// Set the LED pins as outputs
TRISBbits.TRISB0 = 0;
TRISBbits.TRISB1 = 0;
TRISBbits.TRISB2 = 0;
TRISBbits.TRISB3 = 0;
TRISBbits.TRISB4 = 0;
TRISBbits.TRISB5 = 0;
TRISBbits.TRISB6 = 0;
TRISBbits.TRISB7 = 0;

while (1) {
// Execute the Knight Rider pattern infinitely
knightRider();
}

return 0;
}
```

Step 3: Compiling and Programming
Once you have written the code, you can compile it using a C compiler that supports the microcontroller on the Explorer 16 development board. Make sure to set up the compiler and linker settings correctly for your specific development environment.

After compiling, you can program the microcontroller on the Explorer 16 development board with the compiled code using a suitable programming tool, such as a programmer/debugger that is compatible with the microcontroller.

Once programmed, the Knight Rider pattern should start running on the LEDs and repeat infinitely, as specified in the requirements.

Note: The code provided is just an example implementation. Depending on your specific setup and requirements, you may need to modify the code accordingly.