const int IR_PIN = 2; // Digital pin connected to the IR receiver
const int MAX_BITS = 32; // Maximum number of bits to be received
void setup() {
pinMode(IR_PIN, INPUT); // Configure IR receiver pin as input
Serial.begin(9600); // Initialize serial communication
}
void loop() {
unsigned long receivedCode = 0; // Variable to store received code
int receivedBits = 0; // Variable to count received bits
// Wait for the start of the IR signal
while (digitalRead(IR_PIN) == HIGH);
// Measure the duration of the pulses and spaces
while (receivedBits < MAX_BITS) {
unsigned long duration = pulseIn(IR_PIN, HIGH);
receivedCode = receivedCode << 1; // Shift previous bits to the left
if (duration > 1000) {
receivedCode = receivedCode | 0x01; // Append a logic "1"
}
receivedBits++;
}
// Print the received code
Serial.println(receivedCode, HEX);
}