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
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
January 11, 2018, 5:05pm
2
using the millis() function
The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.
when the LED is switched on note the millis() value and after 10 seconds switch it off
something like this?
It seems to totally ignore what'ive written. It just toggles.
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);
}
}
system
January 11, 2018, 5:41pm
4
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.
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
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...
system
January 11, 2018, 7:05pm
7
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...
Look at your code. There is a place where you decide that the LED should be turned on:
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().
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();~~
~~ }~~
Thanks Paul for your patience
Not quite in goal yet.
Still has no effect..
updated the variable names
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);
}
}
Try
if ((millis - trackLedOnTime) > ledOnDelay) {
ledState = LOW;
}
Doesn't work, unfortunately.
horace
January 13, 2018, 6:46am
12
when you compiled you should have seen the warnings
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
Oh, I see it now...
if ((millis - trackLedOnTime) > ledOnDelay) {
ledState = LOW;
}
Try
if ((millis() - trackLedOnTime) > ledOnDelay) {
ledState = LOW;
}
Nope. Doesnt work
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
January 13, 2018, 6:17pm
15
what does not work?
if I press the button the LED lights and goes off after 10 seconds