This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/EK-TM4C123GXL: SW1 Button and LED

Part Number: EK-TM4C123GXL


Tool/software: Code Composer Studio

how do i use the button to turn on the led then turn it off when pressed again? 

currently, my program only turns on my led if i hold the button and it turns off if i let go

  • Germaine Tan said:
    How do i use the button to turn on the led  ... then turn it off  when pressed again? 

    That goal describes a, 'Toggle Function' - one in which 2 unique behaviors occur in sequence - following each button (switch, really) operation.    Your current program appears to simply, 'Monitor' the switch (via the MCU's GPIO pin)  which receives such switch input.    That's much easier - yet not so valuable - just as you've noted.

    That 'Live Switch Monitoring' is far simpler - and avoids a common 'gotcha' ... the known tendency of  inexpensive switches to suffer, 'Contact Bounce.'   (Bounce is the generation of multiple contact 'Makes/Breaks' (closures/opens) - which persist often for milliseconds.      Also known as 'Contact Chatter' - these are produced when the switch's mechanical electrical contacts join (or separate.)

    With that as (necessary) background - now to your requested programming requirements:

    • Your program must 'know' the current state of that switch
    • Your program must (properly) detect the 'change of state' of that switch
    • That 'change of state' detection - must not be 'fooled' by the 'switch bounce'
    • When - and only when - the switch state has become adequately 'stable' (i.e. bounce free) should the desired program action occur

    You must buy into (accept) the need for all four of those functions.    Once agreed - how do 'you' propose to write code - which achieves each function?    Such IS required - is it not?    

    Should you make such effort - and present it here - I'll return to offer (further) encouragement (perhaps guidance) - but  your 'learning' is best assured by your 'dealing with these program issues' - now spelled out w/'reasonable' clarity - and brought to your attention...

  • hi cb1, 

    how do you do debouncing of the switches? 

  • How do 'you' answer (any) of the earlier - still outstanding - issues?      Anyone here would feel 'little motivated' - after creating such focused detail - and then receiving an 'answering post'  -  which (clearly) - is 'Non-Responsive!'      Beyond that - it is gracious to 'thank'  your sole - and detailed responder - prior to 'loading on' - still more questions - is that not true?

    In my (first) identification of such 'Switch BOUNCE' - and (then) description of  'bounce's'  effect (and duration) - there are (several, solid clues.)        You may recall - that such 'bounce' was noted - 'NOT - to persist forever!'     (that almost makes your 'bounce resolving job' ... too easy - does it not?)

    One way data-dump - while practiced here by (some) - does not maximize forum user 'learning' - which is my pursuit & purpose...

  • Hello Germaine,

    As cb1 is mentioning, that is more of a basic engineering question rather than a question specific to our device. There are many resources available for learning how to achieve your goal, I would suggest some web searches on the topic. Stackoverflow may have some useful articles as well.

    Once you understanding the 'how' in general, if you are running into issues with the implementation on TM4C, then post your source code and we can comment further to help from there.