Show my code:
int irPin = 4; //Sensor pin 1 wired to Arduino's pin D4
int start_bit = 4500; //Start bit threshold (Microseconds)
int bin_1 = 1600; //Binary 1 threshold (Microseconds)
int bin_0 = 560; //Binary 0 threshold (Microseconds)
void setup() {
pinMode(irPin, INPUT);
Serial.begin(9600);
Serial.println("IR/Serial Initialized: ");
}
void loop() {
int key = getIRKey(); //Fetch the key
Serial.println( key );
}
int getIRKey() {
int data[33];
int i;
while(pulseIn(irPin, LOW) < start_bit); //Wait for a start bit
for(i = 0 ; i < 32 ; i++)
data[i] = pulseIn(irPin, HIGH); //Start measuring bits, I only want low pulses
for(i = 0 ; i < 32 ; i++) { //Parse them
//Serial.println( data[i] ); // debug
if(data[i] > bin_1) //is it a 1?
data[i] = 1;
else if(data[i] > bin_0) //is it a 0?
data[i] = 0;
//else
//return -1; //Flag the data as invalid; I don't know what it is! Return -1 on invalid data
}
int result = 0;
for(i = 0 ; i < 32 ; i++) //Convert data bits to integer
if(data[i] == 1) result |= (1<<i);
return result; //Return key number
}
But any times return code random… What’s my problem? I see NEC IR protocol (http://www.sbprojects.com/knowledge/ir/nec.php) and understand…
i’m debug and see:
543
536
536
575
522
535
571
526
1656
1652
1675
1610
1602
1642
1633
1632
1648
1624
1649
570
529
532
579
531
532
579
532
1653
1650
1652
1633
1631