Advertisement
pleasedontcode

"Color Toggle" rev_01

Jun 10th, 2025
942
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: "Color Toggle"
  13.     - Source Code NOT compiled for: Arduino Pro Mini 5V
  14.     - Source Code created on: 2025-06-10 17:48:56
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* when button is pressed led most change color from */
  21.     /* red to white */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <FastLED.h>    //https://github.com/FastLED/FastLED
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32.  
  33. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  34. const uint8_t ws_WS2812_DIN_PIN_D2      = 2;
  35. #define DATA_PIN    5 // This defines the data pin for FastLED
  36. //#define CLK_PIN   4 // CLK_PIN is not used in this setup, so it is commented out
  37.  
  38. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  39. /***** used to store raw data *****/
  40. bool    ws_WS2812_DIN_PIN_D2_rawData        = 0;
  41.  
  42. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  43. /***** used to store data after characteristic curve transformation *****/
  44. float   ws_WS2812_DIN_PIN_D2_phyData        = 0.0;
  45.  
  46. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  47. CRGB leds[NUM_LEDS]; // Declare the array of LEDs
  48. byte ledIndex[NUM_LEDS] = {2}; // LED index array
  49.  
  50. #define BRIGHTNESS          96
  51. #define FRAMES_PER_SECOND  120
  52. byte ch1Hue = 135;
  53. byte maxHue = 240;
  54.  
  55. // Define button pin
  56. const int BUTTON_PIN = 3; // Assuming the button is connected to pin 3
  57. bool ledColorState = false; // false for red, true for white
  58.  
  59. /****** FUNCTION PROTOTYPES FOR USER CODE *****/
  60. void potentiometers();
  61. void channelMenu();
  62. void setAllLeds(byte hue_, byte randomness_);
  63. void updateLedColor(); // Function to update LED color based on button press
  64.  
  65. /****** SETUP FUNCTION *****/
  66. void setup(void)
  67. {
  68.     // put your setup code here, to run once:
  69.     pinMode(ws_WS2812_DIN_PIN_D2,    OUTPUT);
  70.     pinMode(BUTTON_PIN, INPUT_PULLUP); // Initialize button pin
  71.  
  72.     // FastLED setup
  73.     FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  74.     FastLED.setBrightness(BRIGHTNESS);
  75.     setAllLeds(ch1Hue, 30); // set all leds at once with a hue (hue, randomness)
  76.     FastLED.show();
  77.  
  78.     // Initialize buttons with pull up resistors
  79.     for (int i = 0; i < N_BUTTONS_ARDUINO; i++) {
  80.         pinMode(BUTTON_ARDUINO_PIN[i], INPUT_PULLUP);
  81.     }
  82.     pinMode(CHANNEL_BUTTON_PIN, INPUT_PULLUP);
  83.    
  84.     // Initialize pots with pull up resistors
  85.     for (int i = 0; i < N_POTS_ARDUINO; i++) {
  86.         pinMode(POT_ARDUINO_PIN[i], INPUT_PULLUP);
  87.     }
  88.    
  89.     // Setup threads for potentiometers and channel menu
  90.     threadPotentiometers.setInterval(15);
  91.     threadPotentiometers.onRun(potentiometers);
  92.     threadChannelMenu.setInterval(40);
  93.     threadChannelMenu.onRun(channelMenu);
  94.     cpu.add(&threadPotentiometers);
  95.     cpu.add(&threadChannelMenu);
  96. }
  97.  
  98. /****** LOOP FUNCTION *****/
  99. void loop(void)
  100. {
  101.     // put your main code here, to run repeatedly:
  102.     updateOutputs(); // Refresh output data
  103.     MIDIread();
  104.     buttons();
  105.     cpu.run(); // for threads
  106.  
  107.     // Update LED color based on button press
  108.     updateLedColor();
  109.  
  110.     // FastLED.show(); // Uncomment if you want to show LEDs in loop
  111.     // insert a delay to keep the framerate modest
  112.     // FastLED.delay(1000 / FRAMES_PER_SECOND);
  113. }
  114.  
  115. void updateOutputs()
  116. {
  117.     digitalWrite(ws_WS2812_DIN_PIN_D2, ws_WS2812_DIN_PIN_D2_rawData);
  118. }
  119.  
  120. void updateLedColor() {
  121.     if (digitalRead(BUTTON_PIN) == LOW) { // Button pressed
  122.         ledColorState = !ledColorState; // Toggle color state
  123.         if (ledColorState) {
  124.             setAllLeds(255, 0); // Set to white
  125.         } else {
  126.             setAllLeds(255, 0, 0); // Set to red
  127.         }
  128.         FastLED.show(); // Update LED display
  129.         delay(200); // Debounce delay
  130.     }
  131. }
  132.  
  133. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement