Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: **IR Control**
- - Source Code NOT compiled for: Arduino Nano
- - Source Code created on: 2025-04-17 20:00:42
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* The system shall control an LED based on the state */
- /* of a push button, turning the LED on when the */
- /* button is pressed and off when released, ensuring */
- /* responsive feedback for user interactions. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <FastLED.h>
- #include <IRremote.h>
- #include <EEPROM.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void handleIrCommand(unsigned long command);
- void setAllLeds(CRGB color);
- int adjustValue(int value, long command);
- void updateLeds();
- void ledChase();
- void blinkLeds();
- void playBuzzer(int value);
- void handleButtonPress(); // New function prototype for button handling
- /****** CONSTANTS AND VARIABLES *****/
- #define NUM_LEDS 40
- #define DATA_PIN 4
- #define IR_PIN 2
- #define BUTTON_PIN 3 // Pin for the push button
- #define EEPROM_BRIGHTNESS_ADDR 0
- #define BUZZER_PIN 10 // Pin for the buzzer
- CRGB leds[NUM_LEDS];
- IRrecv irrecv(IR_PIN);
- decode_results results;
- int redValue = 0;
- int greenValue = 0;
- int blueValue = 0;
- int brightness = 125;
- bool ledsOn = false;
- bool redMode = false;
- bool greenMode = false;
- bool blueMode = false;
- bool blinking = false;
- unsigned long previousBlinkTime = 0;
- unsigned long previousChaseTime = 0;
- const long blinkInterval = 500;
- const long chaseInterval = 500;
- int chasingLed = NUM_LEDS - 1;
- bool isChasing = false;
- CRGB chasingColor;
- CRGB originalColors[NUM_LEDS];
- void setup(void) {
- // put your setup code here, to run once:
- FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
- FastLED.setBrightness(brightness);
- irrecv.enableIRIn();
- brightness = EEPROM.read(EEPROM_BRIGHTNESS_ADDR);
- if (brightness == 255) {
- brightness = 125;
- }
- pinMode(BUZZER_PIN, OUTPUT);
- pinMode(BUTTON_PIN, INPUT_PULLUP); // Set button pin as input with pull-up resistor
- }
- void loop(void) {
- // put your main code here, to run repeatedly:
- if (irrecv.decode(&results)) {
- handleIrCommand(results.value);
- irrecv.resume();
- }
- handleButtonPress(); // Check the button state
- if (blinking) {
- blinkLeds();
- }
- if (isChasing) {
- ledChase();
- }
- }
- void handleButtonPress() {
- // Read the button state
- if (digitalRead(BUTTON_PIN) == LOW) { // Button is pressed
- setAllLeds(CRGB::Red); // Turn on LED
- ledsOn = true; // Update the LED state
- } else { // Button is released
- setAllLeds(CRGB::Black); // Turn off LED
- ledsOn = false; // Update the LED state
- }
- }
- void handleIrCommand(unsigned long command) {
- switch (command) {
- // Existing IR command handling code...
- // (No changes made to existing IR commands)
- // ...
- }
- // Existing code for adjusting RGB values and updating LEDs...
- // ...
- }
- void setAllLeds(CRGB color) {
- for (int i = 0; i < NUM_LEDS; i++) {
- leds[i] = color;
- }
- FastLED.show();
- if (!ledsOn) {
- EEPROM.write(EEPROM_BRIGHTNESS_ADDR, brightness);
- }
- playBuzzer(color.r + color.g + color.b); // Buzzer based on RGB values
- }
- int adjustValue(int value, long command) {
- if (command == 0xFF629D) {
- return constrain(value + 10, 0, 255);
- } else {
- return constrain(value - 10, 0, 255);
- }
- }
- void updateLeds() {
- for (int i = 0; i < NUM_LEDS; i++) {
- leds[i] = CRGB(redValue, greenValue, blueValue);
- }
- FastLED.show();
- if (!ledsOn) {
- EEPROM.write(EEPROM_BRIGHTNESS_ADDR, brightness);
- }
- }
- void ledChase() {
- unsigned long currentMillis = millis();
- if (currentMillis - previousChaseTime >= chaseInterval) {
- previousChaseTime = currentMillis;
- leds[chasingLed] = CRGB::Black;
- chasingLed = (chasingLed - 1 + NUM_LEDS) % NUM_LEDS;
- leds[chasingLed] = chasingColor;
- FastLED.show();
- if (chasingLed == NUM_LEDS - 1) {
- isChasing = false;
- }
- }
- }
- void blinkLeds() {
- unsigned long currentMillis = millis();
- if (currentMillis - previousBlinkTime >= blinkInterval) {
- previousBlinkTime = currentMillis;
- for (int i = 0; i < NUM_LEDS; i++) {
- if (leds[i].r || leds[i].g || leds[i].b) {
- leds[i] = CRGB::Black;
- } else {
- leds[i] = originalColors[i];
- }
- }
- FastLED.show();
- }
- }
- void playBuzzer(int value) {
- int frequency = map(value, 0, 255, 100, 2000); // Map value to audible frequency
- tone(BUZZER_PIN, frequency, 50); // Generate tone for 50 ms
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement