Make led stay on after potentiometer value changes

Hello. How do I keep the LED on, after potentiometer read value changes. I'm completely new to using mills(). Please help me, bright minds.


#define pot A1
int last_pot_read;


void setup() {
  
last_pot_read = 0;

}

 void loop() {

int new_pot_read = map(analogRead(A1), 0, 900, 0, 8);


if (new_pot_read != last_pot_read){ 

LED stay ON for duration of X milliseconds, then turn OFF

}

 
last_pot_read = new_pot_read;
}

Hello paxys

Take a view in the IDE examples to find the BLINKWITHOUTDELAY example. This example contains the mother of all timers in variations used in the Arduino biotop.
Make a copy and build your own timer as needed simply.

Have a nice day and enjoy coding in C++.

once again the blink without explain / understand example from the arduino-IDE

I claim that this tutorial is much better because it explains the basic principle on a everyday example

best regards Stefan

So here is a version that uses my non-blocking timer-function and a flag


#define pot A1
int last_pot_read;
int new_pot_read;

const byte myLED_Pin = 2;
boolean pot_has_changed = false;
unsigned long myLEDTimer;
unsigned long myLED_OnTime = 200;

// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - startOfPeriod >= TimePeriod ) {
    // more time than TimePeriod has elapsed since last time if-condition was true
    startOfPeriod = currentMillis; // a new period starts right here so set new starttime
    return true;
  }
  else return false;            // actual TimePeriod is NOT yet over
}

void setup() {
  last_pot_read = 0;
  pinMode(myLED_Pin, OUTPUT);
}

void loop() {

  new_pot_read = map(analogRead(A1), 0, 900, 0, 8);

  if (new_pot_read != last_pot_read) {
    pot_has_changed = true;        // set flag to true
    digitalWrite(myLED_Pin, HIGH); // switch LED ON
    myLEDTimer = millis();         // initialise Timer-variable with actual time
  }

  if (pot_has_changed) {
    if ( TimePeriodIsOver(myLEDTimer, myLED_OnTime) ) {
      digitalWrite(myLED_Pin, LOW); // switch LED OFF
      pot_has_changed = false;      // reset flag to false
    }
  }

  last_pot_read = new_pot_read;
}

If this LED shall only be switched on after the pot-value has changed
the state of the IO-pin itself could be used as the flag


#define pot A1
int last_pot_read;
int new_pot_read;

const byte myLED_Pin = 2;

unsigned long myLEDTimer;
unsigned long myLED_OnTime = 200;

// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - startOfPeriod >= TimePeriod ) {
    // more time than TimePeriod has elapsed since last time if-condition was true
    startOfPeriod = currentMillis; // a new period starts right here so set new starttime
    return true;
  }
  else return false;            // actual TimePeriod is NOT yet over
}

void setup() {
  last_pot_read = 0;
  pinMode(myLED_Pin, OUTPUT);
}

void loop() {

  new_pot_read = map(analogRead(A1), 0, 900, 0, 8);

  if (new_pot_read != last_pot_read) {
    digitalWrite(myLED_Pin, HIGH); // switch LED ON
    myLEDTimer = millis();         // initialise Timer-variable with actual time
  }

  if (digitalRead(myLED_Pin) == HIGH) { // yes you can read the state of an IO-pin configured as output
    if ( TimePeriodIsOver(myLEDTimer, myLED_OnTime) ) {
      digitalWrite(myLED_Pin, LOW); // switch LED OFF
    }
  }

  last_pot_read = new_pot_read;
}

best regards Stefan

Please see this potentially relevant recent thread

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.