pleasedontcode

# LED Dimmer rev_01

Jan 11th, 2026
23
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: # LED Dimmer
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2026-01-11 21:00:19
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* when i click the button 1 time the led start */
  21.     /* glowing from the inside to out and when i click */
  22.     /* the  button 2 times in a row the led start glowing */
  23.     /* all together and when i click the buttin 3 times */
  24.     /* in a row the led start glowing randomly */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27.  
  28. void setup(void)
  29. {
  30.     // Initialize pin modes
  31.     pinMode(BUTTON_PIN, INPUT_PULLUP);
  32.     pinMode(LED_PIN, OUTPUT);
  33.    
  34.     // Set initial LED state
  35.     analogWrite(LED_PIN, 0);
  36.    
  37.     // Initialize serial for debugging
  38.     Serial.begin(9600);
  39.     Serial.println("System initialized");
  40.     Serial.println("Click button 1x = glow from inside to out");
  41.     Serial.println("Click button 2x = glow all together");
  42.     Serial.println("Click button 3x = glow randomly");
  43. }
  44.  
  45. void loop(void)
  46. {
  47.     // Check button state and debounce
  48.     checkButton();
  49.    
  50.     // Handle the current glow mode
  51.     switch(glowMode)
  52.     {
  53.         case 1:
  54.             glowFromInsideToOut();
  55.             break;
  56.         case 2:
  57.             glowAllTogether();
  58.             break;
  59.         case 3:
  60.             glowRandomly();
  61.             break;
  62.         default:
  63.             // No active glow mode
  64.             break;
  65.     }
  66.    
  67.     // Check if click timeout has occurred
  68.     if(clickCount > 0 && (millis() - lastClickTime) > CLICK_TIMEOUT)
  69.     {
  70.         handleClickCount();
  71.         clickCount = 0;
  72.         glowMode = 0;
  73.     }
  74. }
  75.  
  76. // Function to check button state with debouncing
  77. void checkButton(void)
  78. {
  79.     int buttonState = digitalRead(BUTTON_PIN);
  80.     unsigned long currentTime = millis();
  81.    
  82.     // Button is pressed (LOW due to pullup)
  83.     if(buttonState == LOW && !buttonPressed && (currentTime - lastButtonPress) > DEBOUNCE_DELAY)
  84.     {
  85.         buttonPressed = true;
  86.         lastButtonPress = currentTime;
  87.         clickCount++;
  88.         lastClickTime = currentTime;
  89.        
  90.         Serial.print("Button pressed. Click count: ");
  91.         Serial.println(clickCount);
  92.     }
  93.    
  94.     // Button is released
  95.     if(buttonState == HIGH && buttonPressed && (currentTime - lastButtonPress) > DEBOUNCE_DELAY)
  96.     {
  97.         buttonPressed = false;
  98.         lastButtonPress = currentTime;
  99.     }
  100. }
  101.  
  102. // Function to handle the click count and activate the corresponding mode
  103. void handleClickCount(void)
  104. {
  105.     Serial.print("Total clicks detected: ");
  106.     Serial.println(clickCount);
  107.    
  108.     if(clickCount == 1)
  109.     {
  110.         // Single click: glow from inside to out
  111.         Serial.println("Activating: Glow from inside to out");
  112.         glowMode = 1;
  113.         currentBrightness = 0;
  114.         glowDirection = 1;
  115.         lastGlowTime = millis();
  116.     }
  117.     else if(clickCount == 2)
  118.     {
  119.         // Double click: glow all together
  120.         Serial.println("Activating: Glow all together");
  121.         glowMode = 2;
  122.         currentBrightness = 255;
  123.         lastGlowTime = millis();
  124.     }
  125.     else if(clickCount == 3)
  126.     {
  127.         // Triple click: glow randomly
  128.         Serial.println("Activating: Glow randomly");
  129.         glowMode = 3;
  130.         lastGlowTime = millis();
  131.     }
  132.     else if(clickCount > 3)
  133.     {
  134.         // More than 3 clicks: reset
  135.         Serial.println("Too many clicks. Resetting.");
  136.         glowMode = 0;
  137.         currentBrightness = 0;
  138.         setLedBrightness(0);
  139.     }
  140. }
  141.  
  142. // Function to create glow from inside to out (brightness increases from 0 to max, then repeats)
  143. void glowFromInsideToOut(void)
  144. {
  145.     unsigned long currentTime = millis();
  146.    
  147.     // Update brightness based on time interval
  148.     if(currentTime - lastGlowTime >= GLOW_SPEED)
  149.     {
  150.         lastGlowTime = currentTime;
  151.        
  152.         // Increase brightness
  153.         if(glowDirection == 1)
  154.         {
  155.             currentBrightness += 5;
  156.             if(currentBrightness >= 255)
  157.             {
  158.                 currentBrightness = 255;
  159.                 glowDirection = -1;  // Start decreasing
  160.             }
  161.         }
  162.         // Decrease brightness
  163.         else
  164.         {
  165.             currentBrightness -= 5;
  166.             if(currentBrightness <= 0)
  167.             {
  168.                 currentBrightness = 0;
  169.                 glowDirection = 1;  // Start increasing again
  170.             }
  171.         }
  172.        
  173.         // Set LED brightness
  174.         setLedBrightness(currentBrightness);
  175.     }
  176. }
  177.  
  178. // Function to create glow all together (constant maximum brightness)
  179. void glowAllTogether(void)
  180. {
  181.     // Keep LED at maximum brightness
  182.     setLedBrightness(255);
  183. }
  184.  
  185. // Function to create random glow (random brightness changes)
  186. void glowRandomly(void)
  187. {
  188.     unsigned long currentTime = millis();
  189.    
  190.     // Update brightness at random intervals
  191.     if(currentTime - lastGlowTime >= RANDOM_GLOW_INTERVAL)
  192.     {
  193.         lastGlowTime = currentTime;
  194.        
  195.         // Generate random brightness value
  196.         currentBrightness = random(0, 256);
  197.        
  198.         // Set LED brightness
  199.         setLedBrightness(currentBrightness);
  200.        
  201.         Serial.print("Random brightness: ");
  202.         Serial.println(currentBrightness);
  203.     }
  204. }
  205.  
  206. // Function to set LED brightness using PWM
  207. void setLedBrightness(int brightness)
  208. {
  209.     // Constrain brightness value between 0 and 255
  210.     brightness = constrain(brightness, 0, 255);
  211.    
  212.     // Write PWM value to LED pin
  213.     analogWrite(LED_PIN, brightness);
  214. }
  215.  
  216. /* END CODE */
  217.  
Advertisement
Add Comment
Please, Sign In to add comment