Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Button click performs all function without pressing second press, third press?

Status
Not open for further replies.

buzzed

Newbie level 6
Newbie level 6
Joined
Oct 8, 2021
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
50
I have connected a button to the 8085 microcontrollers and I want that button to perform different functions when pressed again and again.
But now when I pressed it executes all the functions pressing only once.

Code:
void switich_1(void)
{    
      count = 0;
      if (modef == 1){                                             // button is pressed       
                if (count == 0) {                                                     
                count++;                                             //   increment count 
                add();                                                  //   perform  one function             
        }  else if (count == 1) {                              // on second press
               count++;                                           //   increment count 
               subtract();                                         //   perform another function,
        } else if (count == 2) {                             // on second press
              count =0;
              divide();                                             //perform another function
        } 
    } 
}
 

Hi,

If all "IF" are processed, then obviously COUNT will run 0, 1, 2

But we don't see how the values of COUNT are generated.
--> You need to show full code.
Maybe wrong code, maybe caused by contact bouncing
--> we need to see your schematic

Klaus
 

As shown in the program above I have just intilialize the count and use it for each button to press second and third times.
 

Your code looks for modef to = 1 (switch pressed) but never looks for modef = 0 (key released).
Therefore it does'nt really count keypresses. As pointed out above, if modef = 1, your code will
see all the "else if " statements as true. To actually count keypresses, test for modef to be both 1 and 0.
The program will need to "debounce" the switch so that one keypress is not seen as multiple keypresses.
Only increment count when the key is released (modef = 0), following a keypress. Then it will know that the next modef = 1 is a new keypress and what it's count is.
 
Last edited:

Hello!

As shown in the program above I have just intilialize the count and use it for each
button to press second and third times.

As said above, you have to show the full code, in particular the part which is calling
your switich_1(void) function.

What I can guess is that you are checking button press in a loop, and if a button
is pressed, then you call this fuction. But since the loop can run many times within
1 button press, your function will be called several times, therefore it will look
like it processes everything.

One thing you might consider is to add a few leds to your hardware.
Then at the beginning of one particular function, you ad a set_led(led_id, ON) and
at the end you add set_led(led_id, OFF). Doing this and hooking a scope on the LED,
you can count how many times the function is called. And beside this it will give
you the duration of your function.
There are plenty of cheap scopes around.
If it's out of your budget, you can also setup a breakpoint at the beginning of the
function.
Press the button
-> The function is called, and the debugger will stop.
Don't release the button and press continue
-> The function will be likely to be called again.
etc...

Dora.
 

@FenTrac I have tried using this also, but it executes all the code:

Code:
void switich_1(void)
{  
      count = 0;
      if (modef == 1){                                             // button is pressed     
                if (count == 0) {                                                   
                count++;                                             //   increment count
                add();                                                  //   perform  one function
                modef ==0;          
        }  else if (count == 1) {                              // on second press
               count++;                                           //   increment count
               subtract();                                         //   perform another function,
               modef ==0;          
        } else if (count == 2) {                             // on second press
              count ==0;
              divide();                                             //perform another function
        }
    }
}

Also, I have tried incrementing the count when the button is released as:
Code:
void switich_1(void)
{  
      count = 0;
      if (modef == 1){                                             // button is pressed     
                if (count == 0) {                                        
                
                add();                                                  //   perform  one function
                modef ==0;       
               delay_msec(100);
               count++;                                             //   increment count   
        }  else if (count == 1) {                              // on second press
               
                subtract();                                         //   perform another function,
                modef ==0; 
                delay_msec(100);
               count++;                                             //   increment count  
         
        } else if (count == 2) {                             // on second press
              count ==0;
              divide();                                             //perform another function
        }
    }
}
 
Last edited:

@doraemon, at the present situation I cannot try with LED, and I have tried using the breakpoint but also I am not being able to stop the button press. I have called the function in the main function as :

Code:
void main_loop(void)
    {        
        while (1)
          {
                /* Panel proccessing */
                panel();

                /* Watchdog timer reset start */
                WDT_Reset();
        
                //**** count ****
               dispset_count();
        
                switich_1();
        }
 

@Ric I wrote this modef ==0; to off the switch, so that new press can be read by the button.
 

You didn't take the hint.
"==" is NOT an assignment operator, it is a comparison operator, so that does not change the value in modef.
Try just a single "=", i.e.
modef = 0;
 

    buzzed

    Points: 2
    Helpful Answer Positive Rating
Are you sure this isn't a debouncing problem? Before I learned about debouncing I always thought I was either not pressing the button or pressing it too many times. So many code examples skip over it, but it is super important!!

This debouncing tutorial is pretty good. He uses a scope to illustrate the bounce itself and shows a couple of different ways you can solve for it.


If it is not a bounce problem, do you think you could post the rest of your code, or at least some more of it so I can better tell what is going on?
 

I have connected a button to the 8085 microcontrollers and I want that button to perform different functions when pressed again and again.
But now when I pressed it executes all the functions pressing only once.

Code:
void switich_1(void)
{    
      count = 0;
      if (modef == 1){                                             // button is pressed       
                if (count == 0) {                                                     
                count++;                                             //   increment count 
                add();                                                  //   perform  one function             
        }  else if (count == 1) {                              // on second press
               count++;                                           //   increment count 
               subtract();                                         //   perform another function,
        } else if (count == 2) {                             // on second press
              count =0;
              divide();                                             //perform another function
        } 
    } 
}

Try This
it should work
Code:
void switich_1(void)
{    
      count = 0;
      if (modef == 1){                                             // button is pressed       
                if (count == 0) {                                                     
                count++;                                             //   increment count 
                add();                                                  //   perform  one function             
        }  else if (count == 1) {                              // on second press
               count++;                                           //   increment count 
               subtract();                                         //   perform another function,
        } else if (count == 2) {                             // on second press
              count =0;
              divide();                                             //perform another function
        } 
    } 
while(modef==1);
delay(100);                    //  add a 100mSec delay
}
 

    buzzed

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top