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.

Push button to toggle led issue

Status
Not open for further replies.

ansh11

Member level 4
Joined
Feb 27, 2018
Messages
71
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
659
I need to write code for following but I clearly don't have any idea so first I am trying to write pseudo code

When button pressed and released, Turn On led. When button pressed and released again, Turn Off led. Continue process

My work : I check button every 10 ms (timer interrupt) When I see a button change, I start counting interrupts. If the button is the same in the next interrupt, count will increment .
Otherwise, count will clear back to zero if the button has changed.
Once i reach a certain 10 counts. i can say that is a new stable state for the button, so I can say that as one press and turn on led

How to turn off led when second time button is pressed and released
 

Best to start and draw a decision flow diagram -

1630006321736.png


Then coding becomes a lot easier. You coding in C or ASM or ?



Regards, Dana.
 
There are three action
1) Button pressed
2) Button Held down
3) button release

check button action every 10ms

if button pressed count up
if Button Held down count up

accept button state valid when get same states 10 times

if count == 10, turn on led

when next pressed happen turn off led
 

Hi

In post 1 your wrote "I need to write code".
Yes. Do this. You've already got all help.
Please don't expect that we write the code for you.

But we will be there to rectify mistakes in your code.

Klaus
 
Hello!

The problem with what you wrote is that you are mixing all technical issues.
In particular, you are mixing the functional issues and the artefact fixing issues.

In this program, there are 2 separate things you have to solve:
1. Debouncing your button
2. Process the button event.

Beside this, I think you chose the worst possible method. You shouldn't use
delays to do that, but to use interrupts. One button interrupt to process the
button event, and one timer interrupt to debounce the button.

By counting up to 10 as you do, what would you do if the button is read as 0
on the second time? Reset your counter? Continue to wait just to see if there
are other button down states?
-> If you reset the counter, that's bad, because you had one push of the button,
but you don't count it.
-> If you count up to 10 and if the button down is 80 ms only, what would happen?
You will just loose a button event.

Here is a pseudo-code sample of what I usually do:
Code:
main() {
    setup_button_interrupt();
}

// This function will be called everytime your button is pressed if the timer
// is not counting
void button_interrupt_routine() {
    toggle_led(); // I hope you can find how to do this.
    disable_button_interrupt();
    setup_timer_interrupt(100ms);
}

//    This function will be called when the timer finished after 100 ms, which
//    is equivalent to your 10 times count of 10 ms.

void timer_interrupt_routine() {
    enable_button_interrupt();
}

By this method:
1. You can avoid any bounce, even with large delays like 250 ms.
2. Even if your button push is less than the bounce delay you set, you can catch the
interrupt.

Dora.
 
The problem with what you wrote is that you are mixing all technical issues.
In particular, you are mixing the functional issues and the artefact fixing issues.
I drawn flowchart but there is problem with this. I don't have any idea how to add logic to toggle led with button

1630760036403.png


PIC MCU & c language
 

I drawn flowchart but there is problem with this. I don't have any idea how to add logic to toggle led with button

View attachment 171752

PIC MCU & c language


Something like this

LEDflg = 0; its off
Count = 0; Time keeper
Buttpin = 0; no butt pressed

The below is main loop -

Test Buttpin = 1 ? //Assuming button pulls pin up to "1" when closed
No, start over, test Buttpin again
Yes, inc counter
Cntr > 10
No, loop to Test Buttpin again
Yes cntr is > 10, Buttpin = 1

Now we have to loop on reading pin until it is released. Simple while loop
while butt pressed do nothing. This then is blocking code. So if you want non blocking
then you want another flag and mod the above loop to be decrementing count (because it got
> 10) and add a test for Count <=0 before you proceed on to executing the fact butt was pressed and released. I would also if butt pressed flg = "1" then force count to 10 to start working on release.

Now we execute on butt has been pressed and released.

LEDflg = 1 then turn off led, set LEDflg to 0, set Count to 0, loop to Test Buttpin and start over
LEDflg = 0, then turn on led, set LEDflg to 1, set Count to 0, loop to Test Buttpin and start over

The command to turn on LED or off is write to the pin register the bit value you want the output
to be.
If LED connected pin to ground then "1" turns it on. If connected to Vdd then "0" turns it on.
Same logic applies to button, if pulling up pin then its a "1" as active, if pulling down pin its
a "0" as active.

Convert above to flow chart, you will see its simpler.

Regards, Dana.
 
Last edited:

Take this for starters -

1630835262453.png


The right hand decision test has no "NO" exit.

You test for key press after Flag1 set, why ?

The right hand decision test, you test for count > 0
and then immediately after test for count = 10, but
it will never get to 10 for that path since to get into
the upper right decision you can only get to it because
count = 10 and then decrement it so it never passes the
count = 10 test.

And a few more. You never do anything with the flag states....

Sequentially you work on getting Flag1 set, then move on to
the release problem. Your diagram is testing for press after
its established its pressed.

Logically describe to yourself step by step the sequence starting
with a press, considering bounce (means restart count), etc..

In your ISR for button positive edge and negative edge test
the flag to determine if you are in press routine or release to
determine whether to inc or dec count. All variables in ISR have
to be declared volatile.


Regards, Dana.
 

I think it looks something like this -

1630940356920.png


Regards, Dana.
 

Attachments

  • 1630938552019.png
    1630938552019.png
    319.8 KB · Views: 125
  • 1630939412222.png
    1630939412222.png
    397.9 KB · Views: 115
  • 1630939743793.png
    1630939743793.png
    457 KB · Views: 118
  • 1630939966339.png
    1630939966339.png
    459.4 KB · Views: 125
  • 1630940325635.png
    1630940325635.png
    460.5 KB · Views: 127
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top