I am currently working on a project with 10 leds, and every single one of them is connected via a microswitch.
so when a microswitch is touched, the led goes on, but i would like the led to stay on for about a second, but i cant use a regular delay because when delay1 is running and an other microswitch2 is touched, led2 wont go on since the delay is still counting.
is there an easy way to solve this?
this is my program without delays:
Your Sketch requires an event-driven timer and a button manager as a service.
All hardware and time relevant data is declared via a data structure and initialized via an array.
The above services process the data from the structured array.
To do so take a view into some powerful C++ instructions like STRUCT, ENUM, ARRAY, CASE/SWITCH,UDT, RANGE-BASED-FOR-LOOP, etc.
Have a nice day and enjoy coding in C++.
Дайте миру шанс!
That word salad makes it sound difficult. There are only a couple of things to get to grips with, ie structs, arrays and millis() timing
An example which I have deliberately not added comments to in order to promote investigation of how it works but the names of the variables should provide some clues
const byte ledPins[] = {3, 5, 6, 9};
const byte buttonPins[] = {A3, A2, A1, A0};
struct dataLayout
{
byte ledPin;
byte buttonPin;
unsigned long onTime;
int period;
};
dataLayout data[] =
{
{3, A3, 0, 1000},
{5, A2, 0, 1000},
};
byte numberOfLeds = sizeof(data) / sizeof(data[0]);
void setup()
{
Serial.begin(115200);
for (int x = 0; x < numberOfLeds; x++)
{
pinMode(data[x].ledPin, OUTPUT);
pinMode(data[x].buttonPin, INPUT_PULLUP);
digitalWrite(data[x].ledPin, HIGH); //start with LEDs off
}
}
void loop()
{
unsigned long currentTime = millis();
for (int x = 0; x < numberOfLeds; x++)
{
if (digitalRead(data[x].buttonPin) == LOW)
{
data[x].onTime = millis();
digitalWrite(data[x].ledPin, LOW);
}
if (currentTime - data[x].onTime >= data[x].period)
{
digitalWrite(data[x].ledPin, HIGH);
}
}
}
I am not saying that your post did not comply with the forum rules, just that you made the requirement sound more scary than necessary because of the buzz words used
One of the problems that we all have in providing help is judging the level to jump in at and having decided that to decide how the help should be provided.
Of course, we all have our own ideas. All contributions are welcome and it is common for different varieties of help to be provided in the same topic
I am expecting @rclover1 to come back with queries which I (and others) will deal with