Project #1:

Hardware:
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.
Part A:
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.
Note: Develop the application as described above and demo to your prof., for evaluation.

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).
Note: Implement Part B and demo to your prof., for evaluation.

In order to complete Part A of the project, here is an example code in C that describes how to implement the Knight Rider Pattern on the Explorer 16 development board:

```c
#include <p24Fxxxx.h>

#define DELAY_TIME 50000 // You can adjust this delay value according to your preference

// Function to initialize the Explorer 16 development board
void initExplorer16() {
AD1PCFG = 0xffff;
TRISA = 0xFF00;
}

// Function to turn off all the LEDs
void turnOffLEDs() {
LATA = 0x00;
}

// Function to turn on a specific LED
void turnOnLED(int pin) {
LATA = 0x01 << pin;
}

int main(void) {
initExplorer16();
int direction = 1; // 1 for left to right, -1 for right to left
int currentLED = 0;

while (1) {
turnOffLEDs();

if (currentLED >= 0 && currentLED < 8) {
turnOnLED(currentLED);
currentLED += direction;
} else {
direction *= -1; // Reverse the direction
currentLED += direction;
}

// Delay to control the speed of the pattern
int count = DELAY_TIME;
while (count > 0) {
count--;
}
}

return 0;
}
```

To complete Part B of the project, you need to modify the code to include the functionality of SW3. Here is an updated code that includes the necessary changes:

```c
#include <p24Fxxxx.h>

#define DELAY_TIME 50000

void initExplorer16() {
AD1PCFG = 0xffff;
TRISA = 0x00FF; // Set the lower 8 pins of PORTA as input
TRISBbits.TRISB5 = 1; // Set RB5 as input
}

void turnOffLEDs() {
LATA = 0x00;
}

void turnOnLED(int pin) {
LATA = 0x01 << pin;
}

int main(void) {
initExplorer16();
int direction = 1;
int currentLED = 0;

while (1) {
turnOffLEDs();

if (direction == 1) {
if (currentLED >= 0 && currentLED < 8) {
turnOnLED(currentLED);
currentLED += direction;
} else {
direction *= -1;
currentLED += direction;
}
} else {
if (currentLED >= 0 && currentLED < 8) {
turnOnLED(7 - currentLED);
currentLED += direction;
} else {
direction *= -1;
currentLED += direction;
}
}

// Check if SW3 is pressed
if (PORTBbits.RB5 == 0) {
direction = 1; // Start from left to right
} else {
direction = -1; // Start from right to left
}

int count = DELAY_TIME;
while (count > 0) {
count--;
}
}

return 0;
}
```

Please note that this code assumes the Explorer 16 development board with the LEDs connected to PORTA and SW3 connected to RB5. Make sure to consult the Explorer16.pdf document for the pin connections and adjust the code accordingly.

To implement this project, you will need to follow these steps:

Part A:
1. Open your preferred Integrated Development Environment (IDE) for working with C programming language.
2. Create a new C project or file.
3. Include the necessary header files for the microcontroller used in the Explorer 16 development board.
4. Define the necessary macros for configuring the pins connected to the LEDs.
5. Initialize the necessary variables.
6. Configure the necessary pins as outputs and set all LEDs to be off initially.
7. Implement a loop that repeats indefinitely.
8. Within the loop, start by turning on LED D3 (the rightmost LED).
9. Use a delay function to pause for a desired amount of time.
10. Implement a loop that iterates from LED D3 to LED D10 (from right to left).
11. Within this loop, turn on the current LED and wait for a desired delay.
12. Turn off the current LED.
13. Repeat steps 9 to 12 for the remaining LEDs in reverse order (from left to right).
14. Repeat steps 7 to 13 indefinitely.

Part B:
1. Add the necessary code to handle the input from SW3 (a push-button usually connected to a pin on the development board).
2. Use an interrupt or polling mechanism to detect when SW3 is pressed.
3. Set a flag or variable to indicate the desired direction of the pattern (e.g., right to left or left to right).
4. Modify the existing code from Part A to check the flag or variable and adjust the loop accordingly.
- If the flag indicates right to left, start from LED D10 and go to LED D3.
- If the flag indicates left to right, start from LED D3 and go to LED D10.
5. Demo the final application to your professor for evaluation.

Note: The specific implementation details and code may vary depending on the microcontroller and IDE you are using. You may need to refer to the documentation provided with the Explorer 16 development board and the datasheet of the microcontroller to accurately configure pins and handle interrupts or polling for the button input.

To complete Project #1, you will need to write a C program to implement the Knight Rider Pattern using the Explorer 16 development board. Here is a step-by-step guide on how to accomplish Part A and Part B:

Part A:
1. Start by including the necessary header files for the microcontroller you are using and defining any constants you may need.

2. Set up the configuration bits, pins, and any other required initialization for the microcontroller.

3. Create a while loop that will repeat indefinitely.

4. Inside the while loop, use a for loop to iterate through each LED.

5. Turn off all the LEDs using the appropriate function or by setting the corresponding pins to low.

6. Turn on the LED at the current position using the appropriate function or by setting the corresponding pin to high.

7. Add a delay of your choice using the appropriate function or a loop to create a desired delay before moving to the next LED.

8. Move to the next LED by incrementing the loop variable.

9. Repeat steps 5-8 until all 8 LEDs are on.

10. Reverse the process by decrementing the loop variable and repeating steps 5-9 to turn off the LEDs one by one.

11. Repeat the entire sequence indefinitely by going back to step 4.

Part B:
1. Add code to handle SW3 input using interrupts or polling methods depending on the microcontroller.

2. Create a variable to keep track of the current direction of the Knight Rider Pattern, starting with the default direction of left to right.

3. Inside the SW3 input handling code, check whether the button is pressed and held down. If it is, change the direction variable to indicate a right to left pattern.

4. If the button is released, change the direction variable back to left to right.

5. Modify the LED sequence code from Part A to check the direction variable before determining the next LED position.

6. If the direction variable indicates a right to left pattern, reverse the LED sequence and decrement the loop variable instead of incrementing it.

7. Test the program by running the modified LED sequence code and pressing/releasing SW3 to change the direction.

8. Repeat the entire sequence indefinitely.

Remember to consult the Explorer16.pdf document for pin connections and refer to the microcontroller's datasheet and reference manual for specific function and register information. It is also important to demonstrate your finished project to your professor for evaluation. Good luck!