// Thelissa Levana Zheng 00000075569
// Establish the I/O registers' memory locations.
#define DDRB_REG (*((volatile uint8_t*)0x24)) // PORTB Data Direction Register
#define PORTB_REG (*((volatile uint8_t*)0x25)) // PORTB Data Register
#define PINC_REG (*((volatile uint8_t*)0x26)) // Input Pins Register or PINC
#define LEDS_MASK 0xFF // The LED pin mask (pins 0 through 7)
#define SWITCH1_MASK 0x04 // Switch 1 mask (pin 10)
#define SWITCH2_MASK 0x02 // Switch 2 mask(pin 9)
#define SWITCH_START_MASK 0x01 // switch_start mask (pin 8)
// Function to initialize ports and pins
void setup() {
DDRB_REG |= LEDS_MASK; // Plot LED pins as output
PORTB_REG &= ~LEDS_MASK; // All LEDs are turn off initially
DDRB_REG &= ~SWITCH_START_MASK; // Plot switch_start pin as input
PORTB_REG |= SWITCH_START_MASK; // Pull-up resistor been enabled for switch_start pin
DDRB_REG &= ~(SWITCH1_MASK | SWITCH2_MASK); // Plot switch pins as input
PORTB_REG |= (SWITCH1_MASK | SWITCH2_MASK); // Pull-up resistors been enabled for switch pins
}
// Function to run LEDs in running mode
void runMode() {
uint8_t ledState = 0x80; // Begin with LED at Digital Pin 7
while (!(PINC_REG & SWITCH_START_MASK)) { // Will be done continously until switch_start is pressed
PORTB_REG = ledState; // LED was turned on corresponding to the current state
ledState >>= 1; // Jump to the next LED
if (ledState == 0) // If it reached the end, it will come back from Pin 7
ledState = 0x80;
_delay_ms(100); // Delay for smooth transition (adjust if needed)
}
}
// Function to run LEDs in bouncing mode
void bounceMode() {
uint8_t ledState = 0x80; // Begin with LED at Digital Pin 7
bool direction = true; // Direction of LED motion
while (!(PINC_REG & SWITCH_START_MASK)) { // Will be done continously until switch_start is pressed
PORTB_REG = ledState; // LED was turned on corresponding to the current state
if (direction) {
ledState >>= 1; // Jump to the next LED
if (ledState == 0) { // If reached the end, replace the direction
ledState = 0x02;
direction = false;
}
} else {
ledState <<= 1; // Jump to the previous LED
if (ledState == 0x80) { // If it reached the end, replace the direction
ledState = 0x40;
direction = true;
}
}
_delay_ms(100); // Delay for smooth transition (adjust if needed)
}
}
void loop() {
// Verify which mode switch is pressed
if (!(PINC_REG & SWITCH1_MASK)) {
runMode(); //If switch_1 was hit, then run mode
} else if (!(PINC_REG & SWITCH2_MASK)) {
bounceMode(); // If switch_2 is pressed, bounce mode happens
}
}