/* Combination Lock Program David Johnson-Davies - www.technoblogy.com - 30th November 2020 AVR128DA28 @ 24 MHz (internal) CC BY 4.0 Licensed under a Creative Commons Attribution 4.0 International license: http://creativecommons.org/licenses/by/4.0/ */ const int Key = 0b10010011; void setup() { ADC0.CTRLA &= ~(ADC_ENABLE_bm); // Silicon errata workaround // Configure I/O pins appropriately PORTA.PIN0CTRL = PORT_PULLUPEN_bm; // Pullup on PA0 PORTA.PIN1CTRL = PORT_PULLUPEN_bm; // Pullup on PA1 PORTC.PIN0CTRL = PORT_PULLUPEN_bm; // Pullup on PC0 PORTC.PIN1CTRL = PORT_PULLUPEN_bm; // Pullup on PC1 PORTD.PIN0CTRL = PORT_PULLUPEN_bm; // Pullup on PD0 PORTD.PIN1CTRL = PORT_PULLUPEN_bm; // Pullup on PD1 PORTF.PIN0CTRL = PORT_PULLUPEN_bm; // Pullup on PF0 PORTF.PIN1CTRL = PORT_PULLUPEN_bm; // Pullup on PF1 PORTA.DIR = PIN3_bm; // Pin PA3 as output } void loop() { int bits10 = PORTA.IN & (PIN1_bm | PIN0_bm); int bits32 = PORTC.IN & (PIN1_bm | PIN0_bm); int bits54 = PORTD.IN & (PIN1_bm | PIN0_bm); int bits76 = PORTF.IN & (PIN1_bm | PIN0_bm); int combination = bits76<<6 | bits54<<4 | bits32<<2 | bits10; if (combination == Key) PORTA.OUT |= PIN3_bm; // write PA3 high else PORTA.OUT &= ~PIN3_bm; // write PA3 low delay(1000); }