Created
December 2, 2025 23:52
-
-
Save Muhammad-Yunus/1359fc2a6c3ef410f9683ccffe29be9c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <IRremote.h> | |
| // Pins (XIAO ESP32-S3) | |
| #define IR_PIN D8 | |
| #define LED1_PIN D5 | |
| #define LED2_PIN D4 | |
| #define LED3_PIN D3 | |
| #define LED4_PIN D2 | |
| // IR Button Codes (HEX values) | |
| #define CODE_LED4 0x834 | |
| #define CODE_LED3 0x434 | |
| #define CODE_LED2 0x234 | |
| #define CODE_LED1 0x634 | |
| #define CODE_SEQ 0x134 | |
| // LED states | |
| bool led1State = false; | |
| bool led2State = false; | |
| bool led3State = false; | |
| bool led4State = false; | |
| // Animation mode | |
| bool seqMode = false; | |
| // For sequential animation | |
| unsigned long lastAnimTime = 0; | |
| int animIndex = 0; | |
| int animDirection = 1; | |
| unsigned long animInterval = 150; // speed of animation in ms | |
| void setup() { | |
| Serial.begin(115200); | |
| IrReceiver.begin(IR_PIN, ENABLE_LED_FEEDBACK); | |
| pinMode(LED1_PIN, OUTPUT); | |
| pinMode(LED2_PIN, OUTPUT); | |
| pinMode(LED3_PIN, OUTPUT); | |
| pinMode(LED4_PIN, OUTPUT); | |
| digitalWrite(LED1_PIN, LOW); | |
| digitalWrite(LED2_PIN, LOW); | |
| digitalWrite(LED3_PIN, LOW); | |
| digitalWrite(LED4_PIN, LOW); | |
| Serial.println("IR LED Controller Ready..."); | |
| } | |
| void loop() { | |
| // ============================= | |
| // IR RECEIVING (NON-BLOCKING) | |
| // ============================= | |
| if (IrReceiver.decode()) { | |
| uint32_t code = IrReceiver.decodedIRData.decodedRawData; | |
| Serial.print("IR Code: 0x"); | |
| Serial.println(code, HEX); | |
| // Toggle LEDs | |
| if (code == CODE_LED1) { | |
| seqMode = false; // stop animation | |
| led1State = !led1State; | |
| digitalWrite(LED1_PIN, led1State); | |
| } | |
| else if (code == CODE_LED2) { | |
| seqMode = false; | |
| led2State = !led2State; | |
| digitalWrite(LED2_PIN, led2State); | |
| } | |
| else if (code == CODE_LED3) { | |
| seqMode = false; | |
| led3State = !led3State; | |
| digitalWrite(LED3_PIN, led3State); | |
| } | |
| else if (code == CODE_LED4) { | |
| seqMode = false; | |
| led4State = !led4State; | |
| digitalWrite(LED4_PIN, led4State); | |
| } | |
| else if (code == CODE_SEQ) { | |
| // toggle animation mode | |
| seqMode = !seqMode; | |
| // When starting animation, reset everything | |
| if (seqMode) { | |
| animIndex = 0; | |
| animDirection = 1; | |
| } | |
| } | |
| IrReceiver.resume(); | |
| } | |
| // ============================= | |
| // LED SEQUENTIAL ANIMATION | |
| // ============================= | |
| if (seqMode) { | |
| unsigned long now = millis(); | |
| if (now - lastAnimTime >= animInterval) { | |
| lastAnimTime = now; | |
| // Turn off all LEDs | |
| digitalWrite(LED1_PIN, LOW); | |
| digitalWrite(LED2_PIN, LOW); | |
| digitalWrite(LED3_PIN, LOW); | |
| digitalWrite(LED4_PIN, LOW); | |
| // Light LED based on animIndex | |
| switch (animIndex) { | |
| case 0: digitalWrite(LED1_PIN, HIGH); break; | |
| case 1: digitalWrite(LED2_PIN, HIGH); break; | |
| case 2: digitalWrite(LED3_PIN, HIGH); break; | |
| case 3: digitalWrite(LED4_PIN, HIGH); break; | |
| } | |
| // Move index forward/backward | |
| animIndex += animDirection; | |
| if (animIndex >= 3) animDirection = -1; // bounce back | |
| if (animIndex <= 0) animDirection = 1; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment