Advertisement
pleasedontcode

**LED Control** rev_01

Dec 6th, 2024
56
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:30:12
  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 A0 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 <EasyButton.h> //https://github.com/evert-arias/EasyButton
  36.  
  37. /****** FUNCTION PROTOTYPES *****/
  38. void setup(void);
  39. void loop(void);
  40.  
  41. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  42. const uint8_t 5_PushButton_PIN_D5       = 5;
  43.  
  44. /***** DEFINITION OF ANALOG INPUT PINS *****/
  45. const uint8_t a7_Potentiometer_Vout_PIN_A0      = A0;
  46.  
  47. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  48. const uint8_t 2_LED_PIN_D2      = 2;
  49. const uint8_t 3_LED_PIN_D3      = 3;
  50. const uint8_t 4_LED_PIN_D4      = 4;
  51.  
  52. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  53. /***** used to store raw data *****/
  54. bool    2_LED_PIN_D2_rawData        = 0;
  55. bool    3_LED_PIN_D3_rawData        = 0;
  56. bool    4_LED_PIN_D4_rawData        = 0;
  57.  
  58. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  59. /***** used to store data after characteristic curve transformation *****/
  60. float   2_LED_PIN_D2_phyData        = 0.0;
  61. float   3_LED_PIN_D3_phyData        = 0.0;
  62. float   4_LED_PIN_D4_phyData        = 0.0;
  63.  
  64. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  65.  
  66. // Variable to hold the speed of LED cycle
  67. unsigned long ledCycleSpeed = 1000; // Default speed in milliseconds
  68.  
  69. void setup(void)
  70. {
  71.     // put your setup code here, to run once:
  72.  
  73.     pinMode(5_PushButton_PIN_D5,    INPUT_PULLUP);
  74.     pinMode(a7_Potentiometer_Vout_PIN_A0,   INPUT);
  75.  
  76.     pinMode(2_LED_PIN_D2,    OUTPUT);
  77.     pinMode(3_LED_PIN_D3,    OUTPUT);
  78.     pinMode(4_LED_PIN_D4,    OUTPUT);
  79. }
  80.  
  81. void turnOffLeds() {
  82.     digitalWrite(4_LED_PIN_D4, LOW);
  83.     digitalWrite(3_LED_PIN_D3, LOW);
  84.     digitalWrite(2_LED_PIN_D2, LOW);
  85. }
  86.  
  87. bool isSwitchPressed() {
  88.     if (digitalRead(5_PushButton_PIN_D5) == LOW) {
  89.         delay(50);
  90.         return digitalRead(5_PushButton_PIN_D5) == LOW;
  91.     }
  92.     return false;
  93. }
  94.  
  95. void loop(void)
  96. {
  97.     // Update the LED cycle speed based on the potentiometer value
  98.     int potValue = analogRead(a7_Potentiometer_Vout_PIN_A0); // Read the potentiometer value
  99.     ledCycleSpeed = map(potValue, 0, 1023, 100, 2000); // Map the value to a range of 100ms to 2000ms
  100.  
  101.     updateOutputs(); // Refresh output data
  102.  
  103.     if (isSwitchPressed()) {
  104.         turnOffLeds();
  105.         return;
  106.     }
  107.     digitalWrite(4_LED_PIN_D4, HIGH);
  108.     digitalWrite(3_LED_PIN_D3, LOW);
  109.     digitalWrite(2_LED_PIN_D2, LOW);
  110.     for (unsigned long startTime = millis(); millis() - startTime < ledCycleSpeed;) {
  111.         if (isSwitchPressed()) {
  112.             turnOffLeds();
  113.             return;
  114.         }
  115.     }
  116.  
  117.     digitalWrite(4_LED_PIN_D4, HIGH);
  118.     digitalWrite(3_LED_PIN_D3, HIGH);
  119.     for (unsigned long startTime = millis(); millis() - startTime < ledCycleSpeed;) {
  120.         if (isSwitchPressed()) {
  121.             turnOffLeds();
  122.             return;
  123.         }
  124.     }
  125.  
  126.     digitalWrite(4_LED_PIN_D4, LOW);
  127.     digitalWrite(3_LED_PIN_D3, LOW);
  128.     digitalWrite(2_LED_PIN_D2, HIGH);
  129.     for (unsigned long startTime = millis(); millis() - startTime < ledCycleSpeed;) {
  130.         if (isSwitchPressed()) {
  131.             turnOffLeds();
  132.             return;
  133.         }
  134.     }
  135.    
  136.     for (int i = 0; i < 4; i++) {
  137.         digitalWrite(2_LED_PIN_D2, HIGH);
  138.         for (unsigned long startTime = millis(); millis() - startTime < ledCycleSpeed;) {
  139.             if (isSwitchPressed()) {
  140.                 turnOffLeds();
  141.                 return;
  142.             }
  143.         }
  144.         digitalWrite(2_LED_PIN_D2, LOW);
  145.         for (unsigned long startTime = millis(); millis() - startTime < ledCycleSpeed;) {
  146.             if (isSwitchPressed()) {
  147.                 turnOffLeds();
  148.                 return;
  149.             }
  150.         }
  151.     }
  152. }
  153.  
  154. void updateOutputs()
  155. {
  156.     digitalWrite(2_LED_PIN_D2, 2_LED_PIN_D2_rawData);
  157.     digitalWrite(3_LED_PIN_D3, 3_LED_PIN_D3_rawData);
  158.     digitalWrite(4_LED_PIN_D4, 4_LED_PIN_D4_rawData);
  159. }
  160.  
  161. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement