Newbie question ( dealing with delays )

Hello,

Yes I am a newbie and sorry for question which may come noob for some.

I have 1x4 keypad , 3 different color of power LEDs and 4 relays

What I want to do is; define different LED flashing styles for each key of keypad.

For instance, when I press key 1 on keypad, LEDs will SOS ( 3 times red then 3 times of green )

When I press 2 LEDs will strobe ( 2 times red then 2 times blue then 2 times green )

And so…

To make LEDs flash I use delay function, the problem is, during delay if I press a key on keypad, arduino can not catch it. I should hole my finger until it catches.

millis didnt work, relay behaved unstable.
attachinterrupt also didnt work, or I couldnt make it work…

I need some oppinions please.

int tus1analogdeger = 1021;
int tus2analogdeger = 0;
int tus3analogdeger = 0;
int tus4analogdeger = 0;

void setup()
{
pinMode (9, OUTPUT);
pinMode (10, OUTPUT);
pinMode (11, OUTPUT);
pinMode (12, OUTPUT);
pinMode (A1, INPUT);
pinMode (A2, INPUT);
pinMode (A3, INPUT);
pinMode (A4, INPUT);
Serial.begin(9600);
}

void oku()
{tus1analogdeger = analogRead(A1);
tus2analogdeger = analogRead(A2);
tus3analogdeger = analogRead(A3);
tus4analogdeger = analogRead(A4);
}

void pr1()
{
digitalWrite (9,LOW);
digitalWrite (10,HIGH);
digitalWrite (11,HIGH);
digitalWrite (12,HIGH);
oku();
if (tus2analogdeger > 1020)
{ pr2(); }

if (tus3analogdeger > 1020)
{ pr3(); }

if (tus4analogdeger > 1020)
{ pr4(); }
tus1analogdeger=1021;
}

void pr2()
{

digitalWrite (9,HIGH);
digitalWrite (10,LOW);
digitalWrite (11,HIGH);
digitalWrite (12,HIGH);

delay(40);
{digitalWrite (10,HIGH);}
delay(70);
{digitalWrite (10,LOW);}
delay(40);
{digitalWrite (10,HIGH);}
delay(200);
{digitalWrite (11,LOW);}
delay(40);
{digitalWrite (11,HIGH);}
delay(70);
{digitalWrite (11,LOW);}
delay(40);
{digitalWrite (11,HIGH);}
delay(600);

oku();
if (tus1analogdeger > 1020)
{ pr1(); }

if (tus3analogdeger > 1020)
{ pr3(); }

if (tus4analogdeger > 1020)
{ pr4(); }

tus2analogdeger=1021;
}

void pr3()
{
digitalWrite (9,HIGH);
digitalWrite (10,HIGH);
digitalWrite (11,LOW);
digitalWrite (12,HIGH);
oku();
if (tus1analogdeger > 1020)
{ pr1(); }

if (tus2analogdeger > 1020)
{ pr2(); }

if (tus4analogdeger > 1020)
{ pr4(); }

tus3analogdeger=1021;
}
void pr4()
{
tus4analogdeger=1021;
digitalWrite (9,HIGH);
digitalWrite (10,HIGH);
digitalWrite (11,HIGH);
digitalWrite (12,LOW);
oku();
if (tus1analogdeger > 1020)
{ pr1(); }

if (tus2analogdeger > 1020)
{ pr3(); }

if (tus3analogdeger > 1020)
{ pr3(); }

tus4analogdeger=1021;
}

void loop()
{
Serial.print("Tus 1 = “);
Serial.print(tus1analogdeger);
Serial.print(” - Tus 2 = “);
Serial.print(tus2analogdeger);
Serial.print(” - Tus 3 = “);
Serial.print(tus3analogdeger);
Serial.print(” - Tus 4 = ");
Serial.println(tus4analogdeger);

if (tus1analogdeger > 1020)
{ pr1(); }

if (tus2analogdeger > 1020)
{ pr2(); }

if (tus3analogdeger > 1020)
{ pr3(); }

if (tus4analogdeger > 1020)
{ pr4(); }
}

millis didnt work

Nonsense. What you meant was that the code you wrote that used millis() did not do what you expected.

Isn't that the code you need help with?

Have a look at how millis() is used to manage timing without blocking in Several things at a time

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum Your code is too long for me to study quickly without copying to a text editor.

…R

As PaulS says, show the code that did not work. That way we can see where you went wrong and advise accordingly.

in the sketch below please focus on pr2()

I know it never enters inside of if loop. Because all if conditions are always true. I couldnt fix this.

  int tus1analogdeger = 1021;
  int tus2analogdeger = 0;
  int tus3analogdeger = 0;
  int tus4analogdeger = 0;
  unsigned long previousMillis = 0; 
  unsigned long currentMillis = 0;

  
  void setup()
{
  pinMode (9, OUTPUT);
  pinMode (10, OUTPUT);
  pinMode (11, OUTPUT);
  pinMode (12, OUTPUT);
  pinMode (A1, INPUT);
  pinMode (A2, INPUT);
  pinMode (A3, INPUT);
  pinMode (A4, INPUT);
  Serial.begin(9600);
  currentMillis = millis();
}

void oku()
 {tus1analogdeger = analogRead(A1);
  tus2analogdeger = analogRead(A2);
  tus3analogdeger = analogRead(A3);
  tus4analogdeger = analogRead(A4);
  }

 void pr1()
 {
  digitalWrite (9,LOW);
  digitalWrite (10,HIGH);
  digitalWrite (11,HIGH);
  digitalWrite (12,HIGH);
oku();
  if (tus2analogdeger > 1020)
    {      pr2();    }
   
  if (tus3analogdeger > 1020)
    {      pr3();    }
  
  if (tus4analogdeger > 1020)
    {      pr4();    }
    tus1analogdeger=1021;
  }

 void pr2()
 {  
  
        digitalWrite (9,HIGH);
        digitalWrite (10,LOW);
        digitalWrite (11,HIGH);
        digitalWrite (12,HIGH);

//delay(40); 
if ( currentMillis - previousMillis >= 40)  //instead of delay(40);
        {digitalWrite (10,HIGH);
        //delay(70);
        if (111 > currentMillis - previousMillis >= 110) //instead of delay(70);
                {digitalWrite (10,LOW);
                 //delay(40);
                if (151 > currentMillis - previousMillis >= 150) //instead of delay(40);
                        {digitalWrite (10,HIGH);
                        //delay(200);
                        if (351 > currentMillis - previousMillis >= 350) //instead of delay(200);
                                {digitalWrite (11,LOW);
                                //delay(40);
                                if (391 > currentMillis - previousMillis >= 390) //instead of delay(40);
                                       {digitalWrite (11,HIGH);
                                        //delay(70);
                                        if (461 > currentMillis - previousMillis >= 460) //instead of delay(70);
                                                {digitalWrite (11,LOW);
                                                //delay(40);
                                                if (501 > currentMillis - previousMillis >= 500) //instead of delay(40);
                                                        {digitalWrite (11,HIGH);
                                                        //delay(600);
                                                        if (1101 > currentMillis - previousMillis >= 1100) //instead of delay(600);
                                                        {previousMillis = currentMillis;}
                                                        }
                                                }
                                       }
                                }
                        }
                }
        }

                                        
  oku();
  if (tus1analogdeger > 1020)
    {      pr1();    }
   
  if (tus3analogdeger > 1020)
    {      pr3();    }
  
  if (tus4analogdeger > 1020)
    {      pr4();    }
        
        tus2analogdeger=1021;
}

 void pr3()
 {
  digitalWrite (9,HIGH);
  digitalWrite (10,HIGH);
  digitalWrite (11,LOW);
  digitalWrite (12,HIGH);
oku();
if (tus1analogdeger > 1020)
    {      pr1();    }
   
if (tus2analogdeger > 1020)
    {      pr2();    }
  
if (tus4analogdeger > 1020)
    {      pr4();    }
        
tus3analogdeger=1021;
 }
 void pr4()
 {
  tus4analogdeger=1021;
  digitalWrite (9,HIGH);
  digitalWrite (10,HIGH);
  digitalWrite (11,HIGH);
  digitalWrite (12,LOW);
  oku();
if (tus1analogdeger > 1020)
    {      pr1();    }
   
if (tus2analogdeger > 1020)
    {      pr3();    }
  
if (tus3analogdeger > 1020)
    {      pr3();    }
        
tus4analogdeger=1021;
 }

 void loop()
{
currentMillis = millis();
Serial.print("Tus 1 = ");
Serial.print(tus1analogdeger);
Serial.print(" - Tus 2 = ");
Serial.print(tus2analogdeger);
Serial.print(" - Tus 3 = ");
Serial.print(tus3analogdeger);
Serial.print(" - Tus 4 = ");
Serial.println(tus4analogdeger);

 if (tus1analogdeger > 1020)
    {      pr1();    }
  
  if (tus2analogdeger > 1020)
    {      pr2();    }
   
  if (tus3analogdeger > 1020)
    {      pr3();    }
  
  if (tus4analogdeger > 1020)
    {      pr4();    } 
    }

I know it never enters inside of if loop.

There is no such thing as an if loop. There is an if statement, but it does not loop.

You have WAY too much code at this point. loop() should call oku() first. Then, it should print the values read from the 4 analog pins. loop() should do NOTHING more, until you KNOW that you are getting good data from the pins (which I doubt).

You've used this syntax quite a few times:

if (111 > currentMillis - previousMillis >= 110)

You can't compound comparisons like this in C.

Well, you can, the compiler will allow it just fine. But it doesn't do you're trying to do.

Put your timing values for flashes and gaps into an array. Store the time a flash or gap starts in a variable. Use millis() and a loop to monitor when each flash/gap must end, then read the next flash/gap time from the array.

The only equation you need is:

if (current_millis - previous_millis >= flash_gap_time){

}

PaulS: There is no such thing as an if loop. There is an if statement, but it does not loop.

You have WAY too much code at this point. loop() should call oku() first. Then, it should print the values read from the 4 analog pins. loop() should do NOTHING more, until you KNOW that you are getting good data from the pins (which I doubt).

Dear PaulS ,

sketch succesfully executes pr1() or pr2 () or pr3() or pr4 () based on the data comming from analog pins. There is no problem here. Problem is, on pr2 (). I start arduino and millis counts. I press button2 ( Analog pin reads data and executes pr(2). At this point, millis is too high so all if functions inside of pr(2) returns true. chronometer

Beedoo: Put your timing values for flashes and gaps into an array. Store the time a flash or gap starts in a variable. Use millis() and a loop to monitor when each flash/gap must end, then read the next flash/gap time from the array.

The only equation you need is:

if (current_millis - previous_millis >= flash_gap_time){

}

Dear Beedoo ; the loop switch to other programs ( pr(1) , pr(3) or pr (4) when received some data from analog pins. It shopuld do this WHILE executing pr(2) .

Have you fixed the incorrect comparisons already pointed out?

Probably not what you want to hear, but I'd start again with something simple, such as flashing an LED without using the delay() function. Doing these kinds of tasks using millis() rather than delay() is a very useful technique to learn.

Beedoo: Probably not what you want to hear, but I'd start again with something simple, such as flashing an LED without using the delay() function. Doing these kinds of tasks using millis() rather than delay() is a very useful technique to learn.

Dear Beedoo ,

I will do as you offered. Step by step.

Thanks.