Jump to content
ELFORUM - Forumul electronistilor

timer too long and not read the button


Recommended Posts

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 to comment
  • Replies 8
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Posted Images

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

 
Edited by Guest
Link to comment

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 to comment
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 to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.Terms of Use si Guidelines