Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: "LED Control"
- - Source Code NOT compiled for: Arduino Nano
- - Source Code created on: 2024-12-06 23:39:37
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Add rotary potentiometer (a7 pin) to change speed */
- /* of led's cycle. So, i want current values of time */
- /* (millis startTime) be like middle, standart, when */
- /* i rotate a potentiometer, the speed must increase */
- /* or decrease respectively. */
- /****** SYSTEM REQUIREMENT 2 *****/
- /* Implement a rotary potentiometer on pin A7 to */
- /* adjust the LED cycle speed. The speed should vary */
- /* based on the potentiometer's position, affecting */
- /* the timing of LEDs on pins D2, D3, and D4. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Arduino.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void updateOutputs();
- void turnOffLeds();
- bool isSwitchPressed();
- int readPotentiometer();
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t 5_PushButton_PIN_D5 = 5;
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t 2_LED_PIN_D2 = 2;
- const uint8_t 3_LED_PIN_D3 = 3;
- const uint8_t 4_LED_PIN_D4 = 4;
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- bool 2_LED_PIN_D2_rawData = 0;
- bool 3_LED_PIN_D3_rawData = 0;
- bool 4_LED_PIN_D4_rawData = 0;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- float 2_LED_PIN_D2_phyData = 0.0;
- float 3_LED_PIN_D3_phyData = 0.0;
- float 4_LED_PIN_D4_phyData = 0.0;
- /***** DEFINITION OF ANALOG INPUT PINS *****/
- const uint8_t POTENTIOMETER_PIN_A7 = A7;
- void setup(void)
- {
- pinMode(5_PushButton_PIN_D5, INPUT_PULLUP);
- pinMode(2_LED_PIN_D2, OUTPUT);
- pinMode(3_LED_PIN_D3, OUTPUT);
- pinMode(4_LED_PIN_D4, OUTPUT);
- pinMode(POTENTIOMETER_PIN_A7, INPUT);
- }
- void loop(void)
- {
- updateOutputs(); // Refresh output data
- if (isSwitchPressed()) {
- turnOffLeds();
- return;
- }
- digitalWrite(4_LED_PIN_D4, HIGH); // Red LED
- digitalWrite(3_LED_PIN_D3, LOW); // Blue LED
- digitalWrite(2_LED_PIN_D2, LOW); // Green LED
- delay(readPotentiometer()); // Adjust delay based on potentiometer
- digitalWrite(4_LED_PIN_D4, HIGH);
- digitalWrite(3_LED_PIN_D3, HIGH);
- delay(readPotentiometer()); // Adjust delay based on potentiometer
- digitalWrite(4_LED_PIN_D4, LOW);
- digitalWrite(3_LED_PIN_D3, LOW);
- digitalWrite(2_LED_PIN_D2, HIGH);
- delay(readPotentiometer()); // Adjust delay based on potentiometer
- for (int i = 0; i < 4; i++) {
- digitalWrite(2_LED_PIN_D2, HIGH);
- delay(readPotentiometer()); // Adjust delay based on potentiometer
- digitalWrite(2_LED_PIN_D2, LOW);
- delay(readPotentiometer()); // Adjust delay based on potentiometer
- }
- }
- void updateOutputs()
- {
- digitalWrite(2_LED_PIN_D2, 2_LED_PIN_D2_rawData);
- digitalWrite(3_LED_PIN_D3, 3_LED_PIN_D3_rawData);
- digitalWrite(4_LED_PIN_D4, 4_LED_PIN_D4_rawData);
- }
- void turnOffLeds() {
- digitalWrite(4_LED_PIN_D4, LOW);
- digitalWrite(3_LED_PIN_D3, LOW);
- digitalWrite(2_LED_PIN_D2, LOW);
- }
- bool isSwitchPressed() {
- if (digitalRead(5_PushButton_PIN_D5) == LOW) {
- delay(50);
- return digitalRead(5_PushButton_PIN_D5) == LOW;
- }
- return false;
- }
- int readPotentiometer() {
- int potValue = analogRead(POTENTIOMETER_PIN_A7);
- // Map the potentiometer value (0-1023) to a suitable delay range (e.g., 100-1000 ms)
- int delayTime = map(potValue, 0, 1023, 100, 1000);
- return delayTime;
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement