Go Down

Topic: manage led output (Read 292 times) previous topic - next topic

moretti1993

Hi There. im fairly new at coding and i'm not sure how to solve this problem myself.

Looking to make a code that, toggles a LED with a debounced switch.
But should also turn off the LED if  the LED has been high for more than 10 sek.

I've read and finally unerstood the default switch debouncing code in the example lab.
but im not sure where to put the 10 sec delay function

Hope to get some help :)

Code: [Select]

const int buttonPin = 2;   
const int ledPin = 13;     


int ledState = HIGH;         
int buttonState;             
int lastButtonState = LOW;   


unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;   

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() {
 
  int reading = digitalRead(buttonPin);

 
  if (reading != lastButtonState) {
   
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
   
    if (reading != buttonState) {
      buttonState = reading;

   
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }

 
  digitalWrite(ledPin, ledState);

 
  lastButtonState = reading;
}

horace

#1
Jan 11, 2018, 06:05 pm Last Edit: Jan 11, 2018, 06:06 pm by horace
 using the millis() function
https://www.arduino.cc/reference/en/language/functions/time/millis/

when the LED is switched on note the millis() value and after 10 seconds switch it off

moretti1993

something like this?
It seems to totally ignore what'ive written. It just toggles.
Code: [Select]

const int buttonPin = 0;   
const int ledPin = 3;     


int ledState = HIGH;         
int buttonState;             
int lastButtonState = LOW;   

unsigned long ledCountdownDelay = 10000;
unsigned long ledCountdown = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;   

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() {
 
  int reading = digitalRead(buttonPin);

 
  if (reading != lastButtonState) {
   
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
   
    if (reading != buttonState) {
      buttonState = reading;

   
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }

 
  digitalWrite(ledPin, ledState);
  lastButtonState = reading;
 
  if(ledState == HIGH){
  ledCountdown = millis();
  }
 
  if ((millis() - ledCountdown) > ledCountdownDelay) {
    digitalWrite(ledPin, LOW);
     }
}

PaulS

How does ledCountdown relate to the time that you turn the pin on? The relationship would be perfectly obvious if the name was ledOnTime.

You want to record when you turn the LED on, not when you see that it is on. Every time through loop(), you see that the pin is on, so you reset the time that it is on. No wonder it never turns off.
The art of getting good answers lies in asking good questions.

UKHeliBob

Quote
It seems to totally ignore what'ive written. It just toggles.
Take note of what Paul said and take a look at the examples in Using millis() for timing.  A beginners guide
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

moretti1993

hmm.. Thanks for the spoon feeding. It hasn't been enough tho. I've looked at it for several hours now and apparently i cant get into the correct thought patterns.

I cant see how to avoid making it loop over and over again...

PaulS

hmm.. Thanks for the spoon feeding. It hasn't been enough tho. I've looked at it for several hours now and apparently i cant get into the correct thought patterns.

I cant see how to avoid making it loop over and over again...

Look at your code. There is a place where you decide that the LED should be turned on:
Code: [Select]
      if (buttonState == HIGH) {
        ledState = !ledState;
      }

If you make ledState HIGH as a result of doing this, THAT is when/where you want to set ledCountdown to millis().

Code: [Select]
      if (buttonState == HIGH) {
        ledState = !ledState;
        if(ledState)
          ledCountdown = millis();
      }


You do NOT want to set ledCountdown when you discover that the pin is HIGH:
  if(ledState == HIGH){
  ledCountdown = millis();
  }
The art of getting good answers lies in asking good questions.

moretti1993

#7
Jan 11, 2018, 08:20 pm Last Edit: Jan 11, 2018, 08:42 pm by moretti1993
Thanks Paul for your patience :)

Not quite in goal yet.
Still has no effect..
updated the variable names

Code: [Select]

const int buttonPin = 0;   
const int ledPin = 3;     


int ledState = HIGH;         
int buttonState;             
int lastButtonState = LOW;   


unsigned long ledOnDelay = 10000;
unsigned long trackLedOnTime = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;   

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() {
 
  int reading = digitalRead(buttonPin);

 
  if (reading != lastButtonState) {
   
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
   
    if (reading != buttonState) {
      buttonState = reading;

   
      if (buttonState == HIGH) {
        ledState = !ledState;
         if(ledState)
         trackLedOnTime = millis();       
      }
    }
  }

 
  digitalWrite(ledPin, ledState);
  lastButtonState = reading;

  if ((millis - trackLedOnTime) > ledOnDelay) {
    digitalWrite(ledPin, LOW);
     }
  }

moretti1993

Can anyone help me?

good weekend :)

aarg

#9
Jan 13, 2018, 01:37 am Last Edit: Jan 13, 2018, 03:11 pm by aarg
Try
Code: [Select]
  if ((millis - trackLedOnTime) > ledOnDelay) {
    ledState =  LOW;
  }
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

moretti1993

Doesn't work, unfortunately.

horace

when you compiled you should have seen the warnings
Code: [Select]
sketch_jan13a\sketch_jan13a.ino:52:17: warning: pointer to a function used in arithmetic [-Wpointer-arith]

   if ((millis - trackLedOnTime) > ledOnDelay) {

fix the problem(s) and then try it

aarg

Oh, I see it now... :)
Code: [Select]
  if ((millis - trackLedOnTime) > ledOnDelay) {
    ledState =  LOW;
  }

Try
Code: [Select]
  if ((millis() - trackLedOnTime) > ledOnDelay) {
    ledState =  LOW;
  }
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

moretti1993

Nope. Doesnt work
Code: [Select]

const int buttonPin = 0;   
const int ledPin = 3;     


int ledState = LOW;         
int buttonState;             
int lastButtonState = HIGH;   


unsigned long ledOnDelay = 10000;
unsigned long trackLedOnTime = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;   

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() {
 
  int reading = digitalRead(buttonPin);

 
  if (reading != lastButtonState) {
   
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
   
    if (reading != buttonState) {
      buttonState = reading;

   
      if (buttonState == HIGH) {
        ledState = !ledState;
         if(ledState)
         trackLedOnTime = millis();       
      }
    }
  }

 
  digitalWrite(ledPin, ledState);
  lastButtonState = reading;

  if ((millis() - trackLedOnTime) > ledOnDelay) {
    ledState =  LOW;
  }
     }
 

horace

what does not work? 
 if I press the button the LED lights and goes off after 10 seconds

Go Up