Advertisement
pleasedontcode

**LED Control** rev_02

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