// Jeffery Durand // // v_05: The brightness is a randomly chosen value // added hexadecimal tries and libraries // Button PA3 (pin 10) // LED PA7 (pin 6) // Cristal Xtal 1 & 2 (pin 2 & 3) #include // basic library #include /* Functions to waste time */ #include // contains the booleans #include // contains rand() #include // contains math functions, but is big! // bitwise manipulations ! (though for several at a time the explicit manipulation is nice) // WARNING: no spacing between the name and params: BIT (x) will crash #define BIT(x) (1 << x) #define bit_get(p,x) (p & BIT(x)) // to read: makes all zero except wanted bit x #define bit_set(p,x) (p |= BIT(x)) // assign a 1 to wanted bit x #define bit_clear(p,x) (p &= ~BIT(x)) // assign a 0 to wanted bit x void flash (void) { bit_set (PORTA, PA7); // turn the LED pin on _delay_ms(100); // wait } int main (void) { /* Initialization */ // try out hexadecimals DDRA = 0x80; PORTA =0x08; // full brightness at first int delay = 0; // using smaller data type for efficiency uint8_t count = 0; // unsigned 8-bit integer // need to declare i here, does not work in for loop int i; // tells if the button has just been pressed bool pressed = false; /* Event Loop */ while (1) { /* action */ if (bit_get(PINA, PA3) == 0 && pressed == false) // meaning the input on PA3 is low, so ground is connected, so button is pressed { bit_clear (PORTA, PA7); // turn the led pin off // increase count and use exponential growth for the delay count = floor(rand() % 12); delay = pow (2, count); // set to pressed true so that the delay is not changed again while button remains pressed pressed = true; } else { if (bit_get(PINA, PA3) != 0) { pressed = false; } bit_clear (PORTA, PA7); // turn the led pin off // need the for loop, cannot use variables inside _delay for (i = 0; i < delay; i++) { _delay_us(1); } bit_set (PORTA, PA7); // turn the LED pin on _delay_us(1); // wait } } return (0); // never reached }