byte led1 = 8;
byte led2 = 9;
byte led3 = 10;
byte knop = 5;
byte var=0;
bool status = 0;
void setup()
{
pinMode(led3, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led1, OUTPUT);
}
void loop()
{
status = digitalRead(knop);
if(status ==HIGH){
var +=1;
delay(1000);
}
switch (var) {
case 0:
digitalWrite(led1,HIGH);
digitalWrite(led2,LOW);
digitalWrite(led3,LOW);
break;
case 1:
digitalWrite(led2,HIGH);
digitalWrite(led1,LOW);
digitalWrite(led3,LOW);
break;
case 2:
digitalWrite(led3,HIGH);
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
break;
case 3:
digitalWrite(led3,LOW);
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
break;
default:
var = 0;
}
}
I can't for the life of me figure out how to fit a program into this that will enable me to turn off all LEDs by holding a button for 5 seconds. Is there anyone that could help?
Record the time (using millis()) when the switch (which is missing a pinMode) becomes pressed (take a look at the state change detection example in the IDE) and if it is still pressed after 5000 milliseconds, do your thing.
If you're open to using a library to do the lifting for you... I wrote myself a simple button library a while back where you can set the timeout before it returns a long press.
If you used it, your code could look something like this:
#include "KTS_Button.h"
#define NUM_LEDS 3
#define BTN_PIN 5
#define LED1_PIN 8
#define LED2_PIN 9
#define LED3_PIN 10
KTS_Button button(BTN_PIN);
byte leds[NUM_LEDS] = { LED1_PIN, LED2_PIN, LED3_PIN };
byte activeLed = NUM_LEDS - 1;
void cycleActiveLed() {
for (byte i = 0; i < 3; i++)
digitalWrite(leds[i], LOW);
digitalWrite(leds[++activeLed %= 3], HIGH);
}
void turnOffLeds() {
for (byte i = 0; i < 3; i++)
digitalWrite(leds[i], LOW);
activeLed = NUM_LEDS - 1;
}
void setup() {
for (byte i = 0; i < 3; i++)
pinMode(leds[i], OUTPUT);
button.setLongPressTimeout(5000);
}
void loop() {
switch (button.read()) {
case SINGLE_PRESS:
cycleActiveLed();
break;
case LONG_PRESS:
turnOffLeds();
break;
}
}
I appreciate there could be a line or two that aren't so beginner-friendly.
This is the KTS_Button.h file: (likely better ways of making a button library but it does the job for me for now)
Just a side note: you can save yourself some typing if you have a function with 3 arguments take care of the switching and you just invoke it with the configuration you want.
This seems to be doing a good job of what I'm looking for, however, when holding down the button the LED's continue to change in the order in what they light up. How could I fix this? I'd like for them not to change when I hold down the button, only once when I click on it.
I'll try this when I've ordered a functioning LED strip, as i'm currently using TinkerCAD to simulate the design and I don't think I can add extern libraries
What does "this" refer to? The function for the LEDs? The algorithm for reading the button?
Are you familiar with functions returning values instead of void? If you can make a function return how long the button was pressed, you are halfway there.
Actually I understand what you had typed now, I've had school classes for arduino for 2 years but I haven't seen that type of function though. I think I've only used it once to make an interrupt.