Advertisement
DanielKrastev-bit

ir lib remote

May 15th, 2024
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 4.04 KB | Source Code | 0 0
  1. /*
  2.    Created by DIYables
  3.  
  4.    This example code is in the public domain
  5.  
  6.    Product page: https://diyables.io/products/infrared-ir-remote-control-kit-with-17-key-controller-and-receiver
  7. */
  8.  
  9. #include <DIYables_IRcontroller.h> // DIYables_IRcontroller library
  10. #define IR_RECEIVER_PIN 8 // The Arduino pin connected to IR controller
  11. int RLed = 9;
  12. int GLed = 10;
  13. int BLed = 11;
  14.  
  15. DIYables_IRcontroller_17 irController(IR_RECEIVER_PIN, 200); // debounce time is 200ms
  16.  
  17. void setup() {
  18.   Serial.begin(9600);
  19.   irController.begin();
  20.   pinMode(RLed, OUTPUT);
  21.   pinMode(GLed, OUTPUT);
  22.   pinMode(BLed, OUTPUT);
  23. }
  24.  
  25. void loop() {
  26.   Key17 command = irController.getKey();
  27.   if (command != Key17::NONE) {
  28.     switch (command) {
  29.       case Key17::KEY_1:
  30.         Serial.println("Red");
  31.         setColor(255, 0, 0); // Red
  32.         break;
  33.  
  34.       case Key17::KEY_2:
  35.         Serial.println("Green");
  36.         setColor(0, 255, 0); // Green
  37.         break;
  38.  
  39.       case Key17::KEY_3:
  40.         Serial.println("Blue");
  41.         setColor(0, 0, 255); // Blue
  42.         break;
  43.  
  44.       case Key17::KEY_4:
  45.         Serial.println("Yellow");
  46.         setColor(255, 255, 0); // Yellow
  47.         break;
  48.  
  49.       case Key17::KEY_5:
  50.         Serial.println("Cyan");
  51.         setColor(0, 255, 255); // Cyan
  52.         break;
  53.  
  54.       case Key17::KEY_6:
  55.         Serial.println("Magenta");
  56.         setColor(255, 0, 255); // Magenta
  57.         break;
  58.  
  59.       case Key17::KEY_7:
  60.         Serial.println("White");
  61.         setColor(255, 255, 255); // White
  62.         break;
  63.  
  64.       case Key17::KEY_8:
  65.         Serial.println("Off");
  66.         setColor(0, 0, 0); // Off
  67.         break;
  68.  
  69.       case Key17::KEY_9:
  70.         Serial.println("Blinking Red");
  71.         blinkColor(255, 0, 0); // Blinking Red
  72.         break;
  73.  
  74.       case Key17::KEY_STAR:
  75.         Serial.println("Blinking Green");
  76.         blinkColor(0, 255, 0); // Blinking Green
  77.         break;
  78.  
  79.       case Key17::KEY_0:
  80.         Serial.println("Blinking Blue");
  81.         blinkColor(0, 0, 255); // Blinking Blue
  82.         break;
  83.  
  84.       case Key17::KEY_SHARP:
  85.         Serial.println("Blinking White");
  86.         blinkColor(255, 255, 255); // Blinking White
  87.         break;
  88.  
  89.       case Key17::KEY_UP:
  90.         Serial.println("Brightness Up");
  91.         adjustBrightness(10); // Increase brightness
  92.         break;
  93.  
  94.       case Key17::KEY_DOWN:
  95.         Serial.println("Brightness Down");
  96.         adjustBrightness(-10); // Decrease brightness
  97.         break;
  98.  
  99.       case Key17::KEY_LEFT:
  100.         Serial.println("Cycle Colors");
  101.         cycleColors(); // Cycle through colors
  102.         break;
  103.  
  104.       case Key17::KEY_RIGHT:
  105.         Serial.println("Rainbow");
  106.         rainbowCycle(); // Rainbow effect
  107.         break;
  108.  
  109.       case Key17::KEY_OK :
  110.         Serial.println("Reset");
  111.         reset(); // Reset to default state
  112.         break;
  113.  
  114.       default:
  115.         Serial.println("WARNING: undefined command:");
  116.         break;
  117.     }
  118.   }
  119. }
  120.  
  121. void setColor(int r, int g, int b) {
  122.   analogWrite(RLed, r);
  123.   analogWrite(GLed, g);
  124.   analogWrite(BLed, b);
  125. }
  126.  
  127. void blinkColor(int r, int g, int b) {
  128.   for (int i = 0; i < 5; i++) { // Blink 5 times
  129.     setColor(r, g, b);
  130.     delay(500);
  131.     setColor(0, 0, 0);
  132.     delay(500);
  133.   }
  134. }
  135.  
  136. void adjustBrightness(int adjustment) {
  137.   static int brightness = 255; // Initial brightness
  138.   brightness = constrain(brightness + adjustment, 0, 255);
  139.   analogWrite(RLed, brightness);
  140.   analogWrite(GLed, brightness);
  141.   analogWrite(BLed, brightness);
  142. }
  143.  
  144. void cycleColors() {
  145.   setColor(255, 0, 0); delay(1000);
  146.   setColor(0, 255, 0); delay(1000);
  147.   setColor(0, 0, 255); delay(1000);
  148.   setColor(255, 255, 0); delay(1000);
  149.   setColor(0, 255, 255); delay(1000);
  150.   setColor(255, 0, 255); delay(1000);
  151. }
  152.  
  153. void rainbowCycle() {
  154.   for (int j = 0; j < 256; j++) { // 1 cycle of all 256 colors in the wheel
  155.     for (int i = 0; i < 3; i++) {
  156.       setColor((255 - i * 85 + j) % 255, (i * 85 + j) % 255, (255 - j) % 255);
  157.       delay(10);
  158.     }
  159.   }
  160. }
  161.  
  162. void reset() {
  163.   setColor(0, 0, 0); // Turn off LED
  164. }
  165.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement