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:33:47
- ********* 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 *****/
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void updateOutputs();
- void turnOffLeds(); // Added function prototype
- bool isSwitchPressed(); // Added function prototype
- int readPotentiometer(); // New function prototype to read potentiometer value
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t 5_PushButton_PIN_D5 = 5;
- /***** DEFINITION OF ANALOG INPUT PINS *****/
- const uint8_t a7_Potentiometer_Vout_PIN_A7 = A7; // Updated to correct pin for potentiometer
- /***** 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 *****/
- /***** used to store raw data *****/
- 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 *****/
- /***** used to store data after characteristic curve transformation *****/
- float 2_LED_PIN_D2_phyData = 0.0;
- float 3_LED_PIN_D3_phyData = 0.0;
- float 4_LED_PIN_D4_phyData = 0.0;
- void setup(void)
- {
- // put your setup code here, to run once:
- pinMode(5_PushButton_PIN_D5, INPUT_PULLUP);
- pinMode(a7_Potentiometer_Vout_PIN_A7, INPUT);
- pinMode(2_LED_PIN_D2, OUTPUT);
- pinMode(3_LED_PIN_D3, OUTPUT);
- pinMode(4_LED_PIN_D4, OUTPUT);
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- updateOutputs(); // Refresh output data
- // USER CODE loop
- if (isSwitchPressed()) {
- turnOffLeds();
- return;
- }
- digitalWrite(4_LED_PIN_D4, HIGH);
- digitalWrite(3_LED_PIN_D3, LOW);
- digitalWrite(2_LED_PIN_D2, LOW);
- unsigned long delayTime = readPotentiometer(); // Get delay time from potentiometer
- for (unsigned long startTime = millis(); millis() - startTime < delayTime;) {
- if (isSwitchPressed()) {
- turnOffLeds();
- return;
- }
- }
- digitalWrite(4_LED_PIN_D4, HIGH);
- digitalWrite(3_LED_PIN_D3, HIGH);
- for (unsigned long startTime = millis(); millis() - startTime < delayTime;) {
- if (isSwitchPressed()) {
- turnOffLeds();
- return;
- }
- }
- digitalWrite(4_LED_PIN_D4, LOW);
- digitalWrite(3_LED_PIN_D3, LOW);
- digitalWrite(2_LED_PIN_D2, HIGH);
- for (unsigned long startTime = millis(); millis() - startTime < delayTime;) {
- if (isSwitchPressed()) {
- turnOffLeds();
- return;
- }
- }
- for (int i = 0; i < 4; i++) {
- digitalWrite(2_LED_PIN_D2, HIGH);
- for (unsigned long startTime = millis(); millis() - startTime < delayTime;) {
- if (isSwitchPressed()) {
- turnOffLeds();
- return;
- }
- }
- digitalWrite(2_LED_PIN_D2, LOW);
- for (unsigned long startTime = millis(); millis() - startTime < delayTime;) {
- if (isSwitchPressed()) {
- turnOffLeds();
- return;
- }
- }
- }
- }
- 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;
- }
- // New function to read the potentiometer and map its value to a suitable delay time
- int readPotentiometer() {
- int potValue = analogRead(a7_Potentiometer_Vout_PIN_A7); // Read potentiometer value
- int delayTime = map(potValue, 0, 1023, 100, 2000); // Map to a delay range (100ms to 2000ms)
- return delayTime; // Return the mapped delay time
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement