Sari la conținut
ELFORUM - Forumul electronistilor

timer too long and not read the button


Postări Recomandate

Hello, I want to help me with a problem, I have the code but not work in a correct mode (code is below). If I press the button ZS_1 and after 1 second I press the another button RB4_bit Relay_back doesn't work because the timer is too long in cycle. Please help me with another expression. Best regards !

 

 do{

                 if ((SZ_1 == 1)  ||  (RB1_bit == 1) || (RB2_bit == 1) || (RB3_bit == 1))


   {  Delay_ms(80);
                 Relay_front=1;
                  Delay_ms(10000);
                   Relay_front=0;
   }


      else if ((RB4_bit == 1)  ||  (RB5_bit == 1))
             {

                 Delay_ms(80);
                 Relay_back=1;
                  Delay_ms(10000);
                 Relay_back=0;
                               }

    } while(1);
    }

Link spre comentariu
  • Răspunsuri 8
  • Creat
  • Ultimul Răspuns

Top autori în acest subiect

Top autori în acest subiect

Imagini postate

Maybe  "Delay_ms(80)" must be biger than 10000.

Try  

 

 do{

                 if ((SZ_1 == 1)  ||  (RB1_bit == 1) || (RB2_bit == 1) || (RB3_bit == 1))


   {  Delay_ms(200000);
                 Relay_front=1;
                  Delay_ms(10000);
                   Relay_front=0;
   }


      else if ((RB4_bit == 1)  ||  (RB5_bit == 1))
             {

                 Delay_ms(200000);
                 Relay_back=1;
                  Delay_ms(10000);
                 Relay_back=0;
                               }

    } while(1);
    }

 
Editat de Vizitator
Link spre comentariu

https://randomnerdtutorials.com/why-you-shouldnt-always-use-the-arduino-delay-function/

 

Do not use delay() if your program handles user input, because delay() blocks the system (as far as your program logic is concerned), i.e. nothing happens during that time, not even checking that the user pressed some button.

What you should do instead is use state machine logic, i.e. something like:

int state = 0;
ulong stateStart, stateDuration;
while (1)
{
    ulong crtTime = millis();
    switch (state)
    {
        case 0: // relay off, button not pressed
            if (button_pressed)
            {
                state = 1;
                stateStart = crtTime;
                stateDuration = 80;
            }
            break;
            
        case 1: // delay between button press and activating relay
            if (crtTime - stateDuration >= stateStart) // testing this way avoids the overflow error
            {
                state = 2;
                stateStart = crtTime;
                stateDuration = 10000;
                relayState = 1; // relay on
            }
            break;

        case 2: // relay on time
            if (crtTime - stateDuration >= stateStart)
            {
                state = 0;
                relayState = 0; // relay off
            }
            break;
    }
}

 

Link spre comentariu
14 hours ago, bcristian said:

https://randomnerdtutorials.com/why-you-shouldnt-always-use-the-arduino-delay-function/

 

Do not use delay() if your program handles user input, because delay() blocks the system (as far as your program logic is concerned), i.e. nothing happens during that time, not even checking that the user pressed some button.

What you should do instead is use state machine logic, i.e. something like:


int state = 0;
ulong stateStart, stateDuration;
while (1)
{
    ulong crtTime = millis();
    switch (state)
    {
        case 0: // relay off, button not pressed
            if (button_pressed)
            {
                state = 1;
                stateStart = crtTime;
                stateDuration = 80;
            }
            break;
            
        case 1: // delay between button press and activating relay
            if (crtTime - stateDuration >= stateStart) // testing this way avoids the overflow error
            {
                state = 2;
                stateStart = crtTime;
                stateDuration = 10000;
                relayState = 1; // relay on
            }
            break;

        case 2: // relay on time
            if (crtTime - stateDuration >= stateStart)
            {
                state = 0;
                relayState = 0; // relay off
            }
            break;
    }
}

 

I understood, thank you very much !

Link spre comentariu

Creează un cont sau autentifică-te pentru a adăuga comentariu

Trebuie să fi un membru pentru a putea lăsa un comentariu.

Creează un cont

Înregistrează-te pentru un nou cont în comunitatea nostră. Este simplu!

Înregistrează un nou cont

Autentificare

Ai deja un cont? Autentifică-te aici.

Autentifică-te acum



×
×
  • Creează nouă...

Informații Importante

Am plasat cookie-uri pe dispozitivul tău pentru a îmbunătății navigarea pe acest site. Poți modifica setările cookie, altfel considerăm că ești de acord să continui.Termeni de Utilizare si Ghidări