Advertisement
pleasedontcode

**LED Control** rev_03

Dec 6th, 2024
64
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:37:02
  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(); // Added function prototype
  42. bool isSwitchPressed(); // Added function prototype
  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 7_Potentiometer_Vout_PIN_A7 = A7; // Changed to A7 for the 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(7_Potentiometer_Vout_PIN_A7, INPUT); // Set A7 as input for potentiometer
  72.  
  73.     pinMode(2_LED_PIN_D2, OUTPUT);
  74.     pinMode(3_LED_PIN_D3, OUTPUT);
  75.     pinMode(4_LED_PIN_D4, OUTPUT);
  76.  
  77.     // USER CODE setup
  78.     pinMode(4, OUTPUT); // red led
  79.     pinMode(3, OUTPUT); // blue led
  80.     pinMode(2, OUTPUT); // green led
  81.     pinMode(5, INPUT_PULLUP); // switch
  82. }
  83.  
  84. void loop(void)
  85. {
  86.     // put your main code here, to run repeatedly:
  87.     updateOutputs(); // Refresh output data
  88.  
  89.     // USER CODE loop
  90.     if (isSwitchPressed()) {
  91.         turnOffLeds();
  92.         return;
  93.     }
  94.  
  95.     digitalWrite(4, HIGH);
  96.     digitalWrite(3, LOW);
  97.     digitalWrite(2, LOW);
  98.  
  99.     // Read potentiometer value and map it to delay time
  100.     int potValue = analogRead(7_Potentiometer_Vout_PIN_A7); // Read potentiometer
  101.     unsigned long delayTime = map(potValue, 0, 1023, 500, 2000); // Map to delay range
  102.  
  103.     for (unsigned long startTime = millis(); millis() - startTime < delayTime;) {
  104.         if (isSwitchPressed()) {
  105.             turnOffLeds();
  106.             return;
  107.         }
  108.     }
  109.  
  110.     digitalWrite(4, HIGH);
  111.     digitalWrite(3, HIGH);
  112.  
  113.     for (unsigned long startTime = millis(); millis() - startTime < delayTime;) {
  114.         if (isSwitchPressed()) {
  115.             turnOffLeds();
  116.             return;
  117.         }
  118.     }
  119.  
  120.     digitalWrite(4, LOW);
  121.     digitalWrite(3, LOW);
  122.     digitalWrite(2, HIGH);
  123.  
  124.     for (unsigned long startTime = millis(); millis() - startTime < delayTime;) {
  125.         if (isSwitchPressed()) {
  126.             turnOffLeds();
  127.             return;
  128.         }
  129.     }
  130.    
  131.     for (int i = 0; i < 4; i++) {
  132.         digitalWrite(2, HIGH);
  133.         for (unsigned long startTime = millis(); millis() - startTime < 250;) {
  134.             if (isSwitchPressed()) {
  135.                 turnOffLeds();
  136.                 return;
  137.             }
  138.         }
  139.         digitalWrite(2, LOW);
  140.         for (unsigned long startTime = millis(); millis() - startTime < 170;) {
  141.             if (isSwitchPressed()) {
  142.                 turnOffLeds();
  143.                 return;
  144.             }
  145.         }
  146.     }
  147. }
  148.  
  149. void updateOutputs()
  150. {
  151.     digitalWrite(2_LED_PIN_D2, 2_LED_PIN_D2_rawData);
  152.     digitalWrite(3_LED_PIN_D3, 3_LED_PIN_D3_rawData);
  153.     digitalWrite(4_LED_PIN_D4, 4_LED_PIN_D4_rawData);
  154. }
  155.  
  156. void turnOffLeds() {
  157.     digitalWrite(4, LOW);
  158.     digitalWrite(3, LOW);
  159.     digitalWrite(2, LOW);
  160. }
  161.  
  162. bool isSwitchPressed() {
  163.     if (digitalRead(5) == LOW) {
  164.         delay(50);
  165.         return digitalRead(5) == LOW;
  166.     }
  167.     return false;
  168. }
  169.  
  170. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement