Advertisement
pleasedontcode

"LED Control" rev_04

Dec 6th, 2024
59
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:39:37
  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>
  36.  
  37. /****** FUNCTION PROTOTYPES *****/
  38. void setup(void);
  39. void loop(void);
  40. void updateOutputs();
  41. void turnOffLeds();
  42. bool isSwitchPressed();
  43. int readPotentiometer();
  44.  
  45. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  46. const uint8_t 5_PushButton_PIN_D5 = 5;
  47.  
  48. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  49. const uint8_t 2_LED_PIN_D2 = 2;
  50. const uint8_t 3_LED_PIN_D3 = 3;
  51. const uint8_t 4_LED_PIN_D4 = 4;
  52.  
  53. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  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. float 2_LED_PIN_D2_phyData = 0.0;
  60. float 3_LED_PIN_D3_phyData = 0.0;
  61. float 4_LED_PIN_D4_phyData = 0.0;
  62.  
  63. /***** DEFINITION OF ANALOG INPUT PINS *****/
  64. const uint8_t POTENTIOMETER_PIN_A7 = A7;
  65.  
  66. void setup(void)
  67. {
  68.     pinMode(5_PushButton_PIN_D5, INPUT_PULLUP);
  69.     pinMode(2_LED_PIN_D2, OUTPUT);
  70.     pinMode(3_LED_PIN_D3, OUTPUT);
  71.     pinMode(4_LED_PIN_D4, OUTPUT);
  72.     pinMode(POTENTIOMETER_PIN_A7, INPUT);
  73. }
  74.  
  75. void loop(void)
  76. {
  77.     updateOutputs(); // Refresh output data
  78.  
  79.     if (isSwitchPressed()) {
  80.         turnOffLeds();
  81.         return;
  82.     }
  83.  
  84.     digitalWrite(4_LED_PIN_D4, HIGH); // Red LED
  85.     digitalWrite(3_LED_PIN_D3, LOW); // Blue LED
  86.     digitalWrite(2_LED_PIN_D2, LOW); // Green LED
  87.     delay(readPotentiometer()); // Adjust delay based on potentiometer
  88.  
  89.     digitalWrite(4_LED_PIN_D4, HIGH);
  90.     digitalWrite(3_LED_PIN_D3, HIGH);
  91.     delay(readPotentiometer()); // Adjust delay based on potentiometer
  92.  
  93.     digitalWrite(4_LED_PIN_D4, LOW);
  94.     digitalWrite(3_LED_PIN_D3, LOW);
  95.     digitalWrite(2_LED_PIN_D2, HIGH);
  96.     delay(readPotentiometer()); // Adjust delay based on potentiometer
  97.  
  98.     for (int i = 0; i < 4; i++) {
  99.         digitalWrite(2_LED_PIN_D2, HIGH);
  100.         delay(readPotentiometer()); // Adjust delay based on potentiometer
  101.         digitalWrite(2_LED_PIN_D2, LOW);
  102.         delay(readPotentiometer()); // Adjust delay based on potentiometer
  103.     }
  104. }
  105.  
  106. void updateOutputs()
  107. {
  108.     digitalWrite(2_LED_PIN_D2, 2_LED_PIN_D2_rawData);
  109.     digitalWrite(3_LED_PIN_D3, 3_LED_PIN_D3_rawData);
  110.     digitalWrite(4_LED_PIN_D4, 4_LED_PIN_D4_rawData);
  111. }
  112.  
  113. void turnOffLeds() {
  114.     digitalWrite(4_LED_PIN_D4, LOW);
  115.     digitalWrite(3_LED_PIN_D3, LOW);
  116.     digitalWrite(2_LED_PIN_D2, LOW);
  117. }
  118.  
  119. bool isSwitchPressed() {
  120.     if (digitalRead(5_PushButton_PIN_D5) == LOW) {
  121.         delay(50);
  122.         return digitalRead(5_PushButton_PIN_D5) == LOW;
  123.     }
  124.     return false;
  125. }
  126.  
  127. int readPotentiometer() {
  128.     int potValue = analogRead(POTENTIOMETER_PIN_A7);
  129.     // Map the potentiometer value (0-1023) to a suitable delay range (e.g., 100-1000 ms)
  130.     int delayTime = map(potValue, 0, 1023, 100, 1000);
  131.     return delayTime;
  132. }
  133.  
  134. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement