-1

When I press a button, this button performs a tast once it was pressed and even I release button, it should keep performing the task. But when I release the button, it does not perform the task.

I assign a task to a button. And when I press this button and even if I release the button, I want the button to keep doing its task.

Here is a part of my code:

   if (!input(button3)){
    buton3_state=1;
   } else buton3_state=0,buton3_lock=0;
    if (buton3_state==1 && buton3_lock==0)
    {   
        output_low(horn),horn_state=0;
        buton3_lock=1;
    }
    if((input1_state==1 ||input2_state==1 ||input3_state==1 ||input4_state==1 ||input5_state==1 ||input6_state==1)&&(buton3_state==0))
{
    horn_state=1;
}
if(horn_state==1){output_high(horn);}
  • 3
    And please don't use the comma-expression to chain multiple expressions into a single statement. That makes the code harder to read, understand and maintain. And may sometimes lead to unexpected results. – Some programmer dude Mar 23 '22 at 08:27
  • 2
    Welcome to StackOverflow! Please take the [tour] to learn how this site works, and read "[ask]". -- Look into "finite state machines", you need a flag variable, as you already have. But think about _when_ to release the flag. Do you know when the task has to stop? -- Anyway, a [mre] is commonly a good way to get decent answers. Please note that you should [edit] your question to add new informations. Comments are the wrong place for such. – the busybee Mar 23 '22 at 08:37
  • 1
    Is there a loop or something? What is the task actually doing? Is the task going on for ever once the button has been poressed? There is a lot of informatino you're not telling us. – Jabberwocky Mar 23 '22 at 08:48
  • There is no enough information. What does input function return? – Kerim Turak Mar 23 '22 at 08:49
  • I use 6 inputs in my hardware and horn relay condition is depend on these inputs. When i see an input signal any one of the input channel,horn relay is on. If the input signal is OFF condition, horn relay is still active untill i push a button. – tensaisakuragi Mar 23 '22 at 08:55
  • Question #1: How do you debounce the buttons? – Lundin Mar 23 '22 at 10:21
  • I do not need that i think ,do i? – tensaisakuragi Mar 23 '22 at 10:24
  • @Lundin as far as I understood there no need to debounce the button here. But TBH the requirements are pretty inaccurate – Jabberwocky Mar 23 '22 at 10:50

2 Answers2

1

I guess you execute your code in a loop like "while" and your input function returns "1" when "button3 = 1". So according to this assumption. When you press the button task will work well but if you release the button in next iteration of loop your program flow will enter the else block. Simple create a global variable out of the loop "state = 0" and make "or" within "if" block after assign "1" in the "if" statement. This ways if you press one time to the button even if you release the button, your code will run continuously because the state is 1 in the first iteration. According to your limited information I understand this.

int state = 0
...
while(...)
{
    if (!input(button3) || state) {
        buton3_state=1;
        state = 1;
    } 
    else {
        buton3_state=0;
        buton3_lock=0;
    }
}
Ôrel
  • 7,044
  • 3
  • 27
  • 46
Kerim Turak
  • 127
  • 10
  • It nearly works well. With your logic, when i get another input signal after i press and release button, i can not make horn relay active. But thank you anyway. It teachs me another vision. – tensaisakuragi Mar 23 '22 at 09:12
  • @tensaisakuragi please explain in plain english what the button is supposed to do like, for example: _"When I press the button the horn should start, and when I press the button again it should stop"_. Or _"When I press the button, the horn should start and sound forever"_. [Edit](https://stackoverflow.com/posts/71583907/edit) the question and put all clarifications __there__ – Jabberwocky Mar 23 '22 at 10:52
  • Step1:When input signal comes to any of the six input channel, horn relay is active. Step2:Even though input channels are of(there is no incoming input signal),horn relay is still active.Because it takes the input signal once. Step3: Horn relay is inactive if i press the button even if the input signals keeps coming. Step 4:If new input signal occurs at any of the input channel,horn relay is active again. – tensaisakuragi Mar 23 '22 at 11:20
0

You probably want something like this:

int task = 0;   // indicates if task should run

for (;;)   // loop forever
{
  if (!input(button3))
  {
    // button is pressed
    task = 1;
    // task is set to 1 and it stays 1 even if the button is released
  }

  if (task)
  {
    // do task...
  }
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115