int timer = 35; // Delay time between steps
void setup() {
// Set pins 2-10 as outputs by writing to DDRD and DDRB memory addresses
*((volatile uint8_t *)0x2A) |= 0b11111100; // DDRD: Set pins 2-7 as outputs
*((volatile uint8_t *)0x24) |= 0b00000011; // DDRB: Set pins 8-9 as outputs
}
void loop() {
// Move LEDs forward (pins 2-10)
for (int thisPin = 2; thisPin <= 10; thisPin++) {
setPin(thisPin, HIGH);
delay(timer);
setPin(thisPin, LOW);
}
// Move LEDs backward (pins 10-2)
for (int thisPin = 10; thisPin >= 2; thisPin--) {
setPin(thisPin, HIGH);
delay(timer);
setPin(thisPin, LOW);
}
}
// Function to set pin HIGH or LOW using memory addresses
void setPin(int pin, bool state) {
if (pin >= 2 && pin <= 7) {
// PORTD (pins 2-7) at address 0x2B
volatile uint8_t *portD = (volatile uint8_t *)0x2B;
if (state) *portD |= (1 << pin);
else *portD &= ~(1 << pin);
} else if (pin >= 8 && pin <= 9) {
// PORTB (pins 8-9) at address 0x25
volatile uint8_t *portB = (volatile uint8_t *)0x25;
if (state) *portB |= (1 << (pin - 8));
else *portB &= ~(1 << (pin - 8));
}
}