#define F_CPU 8'000'000UL #include #include #include #define SNAKE_SIZE 4 #if SNAKE_SIZE > 7 #error "SNAKE_SIZE too big" #elif SNAKE_SIZE == 0 #error "SNAKE_SIZE must be greater than 0" #endif int main() { // Leds DDRB = 0xff; PORTB = 0x00; // Buttons DDRD = 0x00; PORTD = 0x01; uint8_t head = SNAKE_SIZE - 1; PORTB = (1 << (SNAKE_SIZE + 1)) - 1; while(true) { if(PIND & 0x01) { PORTB |= (1 << (head + 1)); PORTB &= ~(1 << (head + 8 - SNAKE_SIZE + 1) % 8); ++head; head %= 8; } else { PORTB |= (1 << (head + 8 - SNAKE_SIZE) % 8); PORTB &= ~(1 << head); head += 7; head %= 8; } _delay_ms(1000); } }