Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
964 B
2
Indexable
Never
byte IRpin = 2;

volatile unsigned long irCode = 0;
volatile bool isDecoded = false;

void setup ()
{
  Serial.begin ( 9600 );
  Serial.println ( "\n\tReady for keyboard reading!\n" );
  pinMode ( IRpin, INPUT_PULLUP );
  attachInterrupt(digitalPinToInterrupt(IRpin), IRinterrupt, CHANGE);
}

void IRinterrupt() {
  unsigned long T;
  uint8_t validPulses = 0 ;
  for ( byte n = 0; n < 36 ; n ++ ) // enough to drop long header
  {
    T = pulseIn ( IRpin, HIGH , 12000 );  // 12ms timeout
    if ( T < 4000 && T > 400 && validPulses < 32 ) {
      // valid pulse
      bitWrite ( irCode, validPulses++, T > 1120 );
    }
  }
  isDecoded = true;
  
}

void loop () {
  
  if(isDecoded && irCode > 0){
    switch(irCode){
      case 0x3:
      Serial.println("Button 1");
      break;
      case 0xA1:
      Serial.println("Button 2");
      break;
      case 0x83:
      Serial.println("Button 3");
      break;
    }
  irCode = 0 ;
  isDecoded = false;   
  }
  
}