Arduino led system delay

hello everyone,

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:

pinMode(5, OUTPUT);
pinMode(6, INPUT);
pinMode(7, OUTPUT);
pinMode(8, INPUT);
pinMode(9, OUTPUT);
pinMode(10, INPUT);
pinMode(11, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:



if(digitalRead(sensor1) == 1)
{
  digitalWrite(led1, LOW);
}
else {
  
digitalWrite(led1, HIGH);
}
////////////////////////////////////////////////
{

if(digitalRead(sensor2) == 1)
{
  digitalWrite(led2, LOW);
}
else {
  
digitalWrite(led2, HIGH);
}
{

if(digitalRead(sensor3) == 1)
{
  digitalWrite(led3, LOW);
}
else {
  
digitalWrite(led3, HIGH);
}
{

if(digitalRead(sensor4) == 1)
{
  digitalWrite(led4, LOW);
}
else {
  
digitalWrite(led4, HIGH);
}
}
{

if(digitalRead(sensor5) == 1)
{
  digitalWrite(led5, LOW);
}
else {
  
digitalWrite(led5, HIGH);
}
}
{

if(digitalRead(sensor6) == 1)
{
  digitalWrite(led6, LOW);
}
else {
  
digitalWrite(led6, HIGH);
}
}
}}}

thanks

Check out the Blink Without Delay example.

See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

Your sketch would also benefit hugely from the use of arrays

Once you understand millis() timing, take a look at arrays.

Your code will shrink by at least 75% and become clearer, more maintainable, and easier to modify!

Hello rclover1

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);
    }
  }
}

oké thanks, i will try this

I am sorry that my post does not comply with the forum rules. I will try to take this into account in the future.

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

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