// Can you please check my code, when I press a button on IR remote I dont' get any serial print for the pressed key.
#include <IRremote.h>
int receiverPin = 11; // Signal Pin of IR receiver to Arduino Digital Pin 11
const byte BUZZER_PIN = 12; // Active buzzer
int latch = 9; //74HC595 pin 9 STCP
int clock = 10; //74HC595 pin 10 SHCP
int data = 8; //74HC595 pin 8 DS
unsigned char table[] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71, 0x00 };
/*-----( Declare objects )-----*/
// IRrecv receiver(receiverPin); // create instance of 'irrecv'
//vairable uses to store the last decodedRawData
int last_decodedRawData = 0;
// --- IR Codes (Copied from Elegoo Code) ---
const int IR_1 = 48; // Code for button 1
const int IR_2 = 24; // Code for button 2
const int IR_3 = 122; // Code for button 3
const int IR_4 = 16; // Code for button 4
const int IR_5 = 56; // Code for button 5
const int IR_6 = 90; // Code for button 6
const int IR_7 = 66; // Code for button 7
const int IR_8 = 74; // Code for button 8
const int IR_9 = 82; // Code for button 9
// For start/stop and reset if you want to use any other key then just replace the below code with the key you want to use.
const int IR_POWER = 162; // Start / Stop
const int IR_RESET = 194; // Reset
// --- Timer State ---
enum State { IDLE,
RUNNING,
FINISHED };
State state = IDLE;
byte timerValue = 0; // 0 to 9
unsigned long lastSecond = 0;
int lastIRCode = 0;
void handleIR() { // Function to handl IR input
// int code = receiver.decodedIRData.command;
int code = IrReceiver.decodedIRData.command;
Serial.print("Received Button Code:");
Serial.println(code);
// Check if user press a button from 1-9 then set that as delay and set the state as IDLE
if (code == IR_1) {
timerValue = 1;
state = IDLE;
} else if (code == IR_2) {
timerValue = 2;
state = IDLE;
} else if (code == IR_3) {
timerValue = 3;
state = IDLE;
} else if (code == IR_4) {
timerValue = 4;
state = IDLE;
} else if (code == IR_5) {
timerValue = 5;
state = IDLE;
} else if (code == IR_6) {
timerValue = 6;
state = IDLE;
} else if (code == IR_7) {
timerValue = 7;
state = IDLE;
} else if (code == IR_8) {
timerValue = 8;
state = IDLE;
} else if (code == IR_9) {
timerValue = 9;
state = IDLE;
} else if (code == IR_POWER) { // Check for Power button to pause/start the couting
if ((state == IDLE || state == FINISHED) && timerValue > 0) {
state = RUNNING;
} else if (state == RUNNING) {
state = IDLE;
}
} else if (code == IR_RESET) {
timerValue = 0;
state = IDLE;
digitalWrite(BUZZER_PIN, LOW); // ensure off
}
// receiver.resume();
}
void Display(unsigned char num) {
digitalWrite(latch, LOW);
shiftOut(data, clock, MSBFIRST, table[num]);
digitalWrite(latch, HIGH);
}
void setup() {
pinMode(latch, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(data, OUTPUT);
// Buzzer
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
Serial.begin(9600);
Serial.println("IR Receiver Button Decode");
// receiver.enableIRIn(); // Start the receiver
IrReceiver.begin(receiverPin, ENABLE_LED_FEEDBACK); // start receiver
Display(0); // Show digit 0 in the start
}
void loop() {
// if (receiver.decode()) {
// handleIR(); // Keep checking IR handle function
// receiver.resume(); // Receive the next value
// }
if (IrReceiver.decode()) {
handleIR();
IrReceiver.resume();
}
// Update display
if (state == RUNNING || state == IDLE || state == FINISHED) {
Display(timerValue);
}
// Countdown logic
if (state == RUNNING) {
if (millis() - lastSecond >= 1000) {
lastSecond = millis();
if (timerValue > 0) {
timerValue--;
if (timerValue == 0) {
state = FINISHED;
digitalWrite(BUZZER_PIN, HIGH);
// Buzzer will turn off after 1 second via delay (acceptable here)
delay(1000); // short blocking is OK for alert
digitalWrite(BUZZER_PIN, LOW);
}
}
}
}
}