Skip to content

Instantly share code, notes, and snippets.

@Grippy98
Last active February 26, 2019 17:36
Show Gist options
  • Save Grippy98/b6e4db5aeae44b9b542953da58989a62 to your computer and use it in GitHub Desktop.
Save Grippy98/b6e4db5aeae44b9b542953da58989a62 to your computer and use it in GitHub Desktop.
Arduino Code for Keyfob Decoder - Using Interrupts and Low Level AVR Bit Shifts
//ECET2700 Paralax Keyfob Decoder - Interrupt Driven
//Meant for Arduino Xplained Mini but portable to any Arduino or derivative
//Andrei Aldea 2/25/2019
#define buttonReadDelay 200 //200 milliseconds we give the fob between when we want to read and when VT interrupt goes high
void setup()
{
DDRC = 0x00; //SET Register C to INPUTS
DDRD = 0xFF; //Outputs, Bottom Two bits are Serial, 3rd bit is Interrupt
DDRB = 0xFF; //OutputsOnly need bottom three bits
PORTD = 0x00; //Initialize to Off
PORTB = 0x00; //Initalize to Off
Serial.begin(115200); //Gotta go fast
attachInterrupt(digitalPinToInterrupt(2), key_decoder, RISING); //Trigger interrupt on Rising Edge
while (!Serial.available() ) {
//Wait for Serial to become available... if it's not for some reason... good for native USB boards like 32u4
}
Serial.println("Decoder Started");
}
void loop()
{
//Do nothing here, because interrupts!
}
void key_decoder()
{
delay(buttonReadDelay);
Serial.print("Interrupt Tirggered: ");
Serial.print(PINC, BIN); //Print it in Binary
Serial.print(" AKA ");
switch (PINC)
{
//OR check individual bits with: bit = (number >> n) & 1U;
case 0b00100000: //A
PORTD ^= 1UL << 4; //Toggle Bit 3
Serial.println("A");
break;
case 0b000010000: //B
PORTD ^= 1UL << 5; //Toggle Bit 4
Serial.println("B");
break;
case 0b00001000: //C
PORTD ^= 1UL << 6; //Toggle Bit 5
Serial.println("C");
break;
case 0b00000100: //D
PORTD ^= 1UL << 7; //Toggle Bit 6
Serial.println("D");
break;
case 0b00110000: //AB
PORTB ^= 1UL << 2; //Toggle Bit 7
Serial.println("AB");
break;
case 0b00001100: //CD
PORTB ^= 1UL << 3; //Toggle PortB Bit 0
Serial.println("CD");
break;
case 0b00101000: //AC
PORTB ^= 1UL << 4; //Toggle PortB Bit 1
Serial.println("AC");
break;
case 0b00010100: //BD
PORTB ^= 1UL << 5 ; //Toggle PortB Bit 2
Serial.println("BD");
break;
default:
Serial.println("Other combo");
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment