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: # LED Dimmer
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2026-01-11 21:00:19
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* when i click the button 1 time the led start */
- /* glowing from the inside to out and when i click */
- /* the button 2 times in a row the led start glowing */
- /* all together and when i click the buttin 3 times */
- /* in a row the led start glowing randomly */
- /****** END SYSTEM REQUIREMENTS *****/
- void setup(void)
- {
- // Initialize pin modes
- pinMode(BUTTON_PIN, INPUT_PULLUP);
- pinMode(LED_PIN, OUTPUT);
- // Set initial LED state
- analogWrite(LED_PIN, 0);
- // Initialize serial for debugging
- Serial.begin(9600);
- Serial.println("System initialized");
- Serial.println("Click button 1x = glow from inside to out");
- Serial.println("Click button 2x = glow all together");
- Serial.println("Click button 3x = glow randomly");
- }
- void loop(void)
- {
- // Check button state and debounce
- checkButton();
- // Handle the current glow mode
- switch(glowMode)
- {
- case 1:
- glowFromInsideToOut();
- break;
- case 2:
- glowAllTogether();
- break;
- case 3:
- glowRandomly();
- break;
- default:
- // No active glow mode
- break;
- }
- // Check if click timeout has occurred
- if(clickCount > 0 && (millis() - lastClickTime) > CLICK_TIMEOUT)
- {
- handleClickCount();
- clickCount = 0;
- glowMode = 0;
- }
- }
- // Function to check button state with debouncing
- void checkButton(void)
- {
- int buttonState = digitalRead(BUTTON_PIN);
- unsigned long currentTime = millis();
- // Button is pressed (LOW due to pullup)
- if(buttonState == LOW && !buttonPressed && (currentTime - lastButtonPress) > DEBOUNCE_DELAY)
- {
- buttonPressed = true;
- lastButtonPress = currentTime;
- clickCount++;
- lastClickTime = currentTime;
- Serial.print("Button pressed. Click count: ");
- Serial.println(clickCount);
- }
- // Button is released
- if(buttonState == HIGH && buttonPressed && (currentTime - lastButtonPress) > DEBOUNCE_DELAY)
- {
- buttonPressed = false;
- lastButtonPress = currentTime;
- }
- }
- // Function to handle the click count and activate the corresponding mode
- void handleClickCount(void)
- {
- Serial.print("Total clicks detected: ");
- Serial.println(clickCount);
- if(clickCount == 1)
- {
- // Single click: glow from inside to out
- Serial.println("Activating: Glow from inside to out");
- glowMode = 1;
- currentBrightness = 0;
- glowDirection = 1;
- lastGlowTime = millis();
- }
- else if(clickCount == 2)
- {
- // Double click: glow all together
- Serial.println("Activating: Glow all together");
- glowMode = 2;
- currentBrightness = 255;
- lastGlowTime = millis();
- }
- else if(clickCount == 3)
- {
- // Triple click: glow randomly
- Serial.println("Activating: Glow randomly");
- glowMode = 3;
- lastGlowTime = millis();
- }
- else if(clickCount > 3)
- {
- // More than 3 clicks: reset
- Serial.println("Too many clicks. Resetting.");
- glowMode = 0;
- currentBrightness = 0;
- setLedBrightness(0);
- }
- }
- // Function to create glow from inside to out (brightness increases from 0 to max, then repeats)
- void glowFromInsideToOut(void)
- {
- unsigned long currentTime = millis();
- // Update brightness based on time interval
- if(currentTime - lastGlowTime >= GLOW_SPEED)
- {
- lastGlowTime = currentTime;
- // Increase brightness
- if(glowDirection == 1)
- {
- currentBrightness += 5;
- if(currentBrightness >= 255)
- {
- currentBrightness = 255;
- glowDirection = -1; // Start decreasing
- }
- }
- // Decrease brightness
- else
- {
- currentBrightness -= 5;
- if(currentBrightness <= 0)
- {
- currentBrightness = 0;
- glowDirection = 1; // Start increasing again
- }
- }
- // Set LED brightness
- setLedBrightness(currentBrightness);
- }
- }
- // Function to create glow all together (constant maximum brightness)
- void glowAllTogether(void)
- {
- // Keep LED at maximum brightness
- setLedBrightness(255);
- }
- // Function to create random glow (random brightness changes)
- void glowRandomly(void)
- {
- unsigned long currentTime = millis();
- // Update brightness at random intervals
- if(currentTime - lastGlowTime >= RANDOM_GLOW_INTERVAL)
- {
- lastGlowTime = currentTime;
- // Generate random brightness value
- currentBrightness = random(0, 256);
- // Set LED brightness
- setLedBrightness(currentBrightness);
- Serial.print("Random brightness: ");
- Serial.println(currentBrightness);
- }
- }
- // Function to set LED brightness using PWM
- void setLedBrightness(int brightness)
- {
- // Constrain brightness value between 0 and 255
- brightness = constrain(brightness, 0, 255);
- // Write PWM value to LED pin
- analogWrite(LED_PIN, brightness);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment