Advertisement
pleasedontcode

**IR Control** rev_01

Apr 17th, 2025
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **IR Control**
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2025-04-17 20:00:42
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The system shall control an LED based on the state */
  21.     /* of a push button, turning the LED on when the */
  22.     /* button is pressed and off when released, ensuring */
  23.     /* responsive feedback for user interactions. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <FastLED.h>
  30. #include <IRremote.h>
  31. #include <EEPROM.h>
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36. void handleIrCommand(unsigned long command);
  37. void setAllLeds(CRGB color);
  38. int adjustValue(int value, long command);
  39. void updateLeds();
  40. void ledChase();
  41. void blinkLeds();
  42. void playBuzzer(int value);
  43. void handleButtonPress(); // New function prototype for button handling
  44.  
  45. /****** CONSTANTS AND VARIABLES *****/
  46. #define NUM_LEDS 40
  47. #define DATA_PIN 4
  48. #define IR_PIN 2
  49. #define BUTTON_PIN 3 // Pin for the push button
  50. #define EEPROM_BRIGHTNESS_ADDR 0
  51. #define BUZZER_PIN 10 // Pin for the buzzer
  52.  
  53. CRGB leds[NUM_LEDS];
  54. IRrecv irrecv(IR_PIN);
  55. decode_results results;
  56.  
  57. int redValue = 0;
  58. int greenValue = 0;
  59. int blueValue = 0;
  60. int brightness = 125;
  61. bool ledsOn = false;
  62. bool redMode = false;
  63. bool greenMode = false;
  64. bool blueMode = false;
  65. bool blinking = false;
  66. unsigned long previousBlinkTime = 0;
  67. unsigned long previousChaseTime = 0;
  68. const long blinkInterval = 500;
  69. const long chaseInterval = 500;
  70. int chasingLed = NUM_LEDS - 1;
  71. bool isChasing = false;
  72. CRGB chasingColor;
  73. CRGB originalColors[NUM_LEDS];
  74.  
  75. void setup(void) {
  76.   // put your setup code here, to run once:
  77.   FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  78.   FastLED.setBrightness(brightness);
  79.   irrecv.enableIRIn();
  80.   brightness = EEPROM.read(EEPROM_BRIGHTNESS_ADDR);
  81.   if (brightness == 255) {
  82.     brightness = 125;
  83.   }
  84.   pinMode(BUZZER_PIN, OUTPUT);
  85.   pinMode(BUTTON_PIN, INPUT_PULLUP); // Set button pin as input with pull-up resistor
  86. }
  87.  
  88. void loop(void) {
  89.   // put your main code here, to run repeatedly:
  90.   if (irrecv.decode(&results)) {
  91.     handleIrCommand(results.value);
  92.     irrecv.resume();
  93.   }
  94.   handleButtonPress(); // Check the button state
  95.   if (blinking) {
  96.     blinkLeds();
  97.   }
  98.   if (isChasing) {
  99.     ledChase();
  100.   }
  101. }
  102.  
  103. void handleButtonPress() {
  104.   // Read the button state
  105.   if (digitalRead(BUTTON_PIN) == LOW) { // Button is pressed
  106.     setAllLeds(CRGB::Red); // Turn on LED
  107.     ledsOn = true; // Update the LED state
  108.   } else { // Button is released
  109.     setAllLeds(CRGB::Black); // Turn off LED
  110.     ledsOn = false; // Update the LED state
  111.   }
  112. }
  113.  
  114. void handleIrCommand(unsigned long command) {
  115.   switch (command) {
  116.     // Existing IR command handling code...
  117.     // (No changes made to existing IR commands)
  118.     // ...
  119.   }
  120.   // Existing code for adjusting RGB values and updating LEDs...
  121.   // ...
  122. }
  123.  
  124. void setAllLeds(CRGB color) {
  125.   for (int i = 0; i < NUM_LEDS; i++) {
  126.     leds[i] = color;
  127.   }
  128.   FastLED.show();
  129.   if (!ledsOn) {
  130.     EEPROM.write(EEPROM_BRIGHTNESS_ADDR, brightness);
  131.   }
  132.   playBuzzer(color.r + color.g + color.b); // Buzzer based on RGB values
  133. }
  134.  
  135. int adjustValue(int value, long command) {
  136.   if (command == 0xFF629D) {
  137.     return constrain(value + 10, 0, 255);
  138.   } else {
  139.     return constrain(value - 10, 0, 255);
  140.   }
  141. }
  142.  
  143. void updateLeds() {
  144.   for (int i = 0; i < NUM_LEDS; i++) {
  145.     leds[i] = CRGB(redValue, greenValue, blueValue);
  146.   }
  147.   FastLED.show();
  148.   if (!ledsOn) {
  149.     EEPROM.write(EEPROM_BRIGHTNESS_ADDR, brightness);
  150.   }
  151. }
  152.  
  153. void ledChase() {
  154.   unsigned long currentMillis = millis();
  155.   if (currentMillis - previousChaseTime >= chaseInterval) {
  156.     previousChaseTime = currentMillis;
  157.     leds[chasingLed] = CRGB::Black;
  158.     chasingLed = (chasingLed - 1 + NUM_LEDS) % NUM_LEDS;
  159.     leds[chasingLed] = chasingColor;
  160.     FastLED.show();
  161.     if (chasingLed == NUM_LEDS - 1) {
  162.       isChasing = false;
  163.     }
  164.   }
  165. }
  166.  
  167. void blinkLeds() {
  168.   unsigned long currentMillis = millis();
  169.   if (currentMillis - previousBlinkTime >= blinkInterval) {
  170.     previousBlinkTime = currentMillis;
  171.     for (int i = 0; i < NUM_LEDS; i++) {
  172.       if (leds[i].r || leds[i].g || leds[i].b) {
  173.         leds[i] = CRGB::Black;
  174.       } else {
  175.         leds[i] = originalColors[i];
  176.       }
  177.     }
  178.     FastLED.show();
  179.   }
  180. }
  181.  
  182. void playBuzzer(int value) {
  183.   int frequency = map(value, 0, 255, 100, 2000); // Map value to audible frequency
  184.   tone(BUZZER_PIN, frequency, 50); // Generate tone for 50 ms
  185. }
  186.  
  187. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement