Advertisement
pleasedontcode

**LED Control** rev_05

Dec 6th, 2024
63
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 Control**
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2024-12-06 23:51:33
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Add rotary potentiometer (a7 pin) to change speed */
  21.     /* of led's cycle. So, i want current values of time */
  22.     /* (millis startTime) be like middle, standart, when */
  23.     /* i rotate a potentiometer, the speed must increase */
  24.     /* or decrease respectively. */
  25. /****** SYSTEM REQUIREMENT 2 *****/
  26.     /* Implement a rotary potentiometer on pin A7 to */
  27.     /* adjust the LED cycle speed. The speed should vary */
  28.     /* based on the potentiometer's position, affecting */
  29.     /* the timing of LEDs on pins D2, D3, and D4. */
  30. /****** END SYSTEM REQUIREMENTS *****/
  31.  
  32. /* START CODE */
  33.  
  34. /****** DEFINITION OF LIBRARIES *****/
  35. #include <Arduino.h> // Include Arduino library for basic functions
  36.  
  37. /****** FUNCTION PROTOTYPES *****/
  38. void setup(void);
  39. void loop(void);
  40. void turnOffLeds(); // Added function prototype
  41. bool isSwitchPressed(); // Added function prototype
  42.  
  43. // Define the pin for the rotary potentiometer
  44. const int potPin = A7; // Pin connected to the potentiometer
  45.  
  46. void setup(void)
  47. {
  48.     // put your setup code here, to run once:
  49.     pinMode(4, OUTPUT); // red led
  50.     pinMode(3, OUTPUT); // blue led
  51.     pinMode(2, OUTPUT); // green led
  52.     pinMode(5, INPUT_PULLUP); // switch
  53. }
  54.  
  55. void turnOffLeds() {
  56.     digitalWrite(4, LOW);
  57.     digitalWrite(3, LOW);
  58.     digitalWrite(2, LOW);
  59. }
  60.  
  61. bool isSwitchPressed() {
  62.     if (digitalRead(5) == LOW) {
  63.         delay(50);
  64.         return digitalRead(5) == LOW;
  65.     }
  66.     return false;
  67. }
  68.  
  69. void loop(void)
  70. {
  71.     // put your main code here, to run repeatedly:
  72.     if (isSwitchPressed()) {
  73.         turnOffLeds();
  74.         return;
  75.     }
  76.  
  77.     // Read the potentiometer value and map it to a suitable range for timing
  78.     int potValue = analogRead(potPin);
  79.     int ledCycleTime = map(potValue, 0, 1023, 500, 3000); // Map to a range of 500ms to 3000ms
  80.  
  81.     digitalWrite(4, HIGH);
  82.     digitalWrite(3, LOW);
  83.     digitalWrite(2, LOW);
  84.     for (unsigned long startTime = millis(); millis() - startTime < ledCycleTime;) {
  85.         if (isSwitchPressed()) {
  86.             turnOffLeds();
  87.             return;
  88.         }
  89.     }
  90.  
  91.     digitalWrite(4, HIGH);
  92.     digitalWrite(3, HIGH);
  93.     for (unsigned long startTime = millis(); millis() - startTime < ledCycleTime;) {
  94.         if (isSwitchPressed()) {
  95.             turnOffLeds();
  96.             return;
  97.         }
  98.     }
  99.  
  100.     digitalWrite(4, LOW);
  101.     digitalWrite(3, LOW);
  102.     digitalWrite(2, HIGH);
  103.     for (unsigned long startTime = millis(); millis() - startTime < ledCycleTime;) {
  104.         if (isSwitchPressed()) {
  105.             turnOffLeds();
  106.             return;
  107.         }
  108.     }
  109.    
  110.     for (int i = 0; i < 4; i++) {
  111.         digitalWrite(2, HIGH);
  112.         for (unsigned long startTime = millis(); millis() - startTime < 250;) {
  113.             if (isSwitchPressed()) {
  114.                 turnOffLeds();
  115.                 return;
  116.             }
  117.         }
  118.         digitalWrite(2, LOW);
  119.         for (unsigned long startTime = millis(); millis() - startTime < 170;) {
  120.             if (isSwitchPressed()) {
  121.                 turnOffLeds();
  122.                 return;
  123.             }
  124.         }
  125.     }
  126. }
  127.  
  128. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement