0

I am intergrating inputs and uart in PIC MCU. My program is if I press a switch, this info is send to terminal but the problem is it keep on displaying until I release the switch. I want it to be displayed just for once regardless of how much time switch is being pressed.

For ex:

while(1)
{
  if(switch == pressed)
  putsUART("switch 1 pressed");

 }

Its just a pseudo code. This keep on displaying as it is in while(1) but is there any logic or some function which displays it for one time, and if some presses the switch again, it prints again for one time.

anna carolina
  • 366
  • 1
  • 5
  • 17
  • First learn language syntax and possibilites, then practice and improve your logic all the time. You'll learn the most first by giving your best, and then when you feel really stucked, ask some expert here to help you. All the best Anna. – Junior Aug 25 '15 at 10:23

2 Answers2

4

If I understand correctly you want to print it only once for each time the button is pressed, but as the controller will loop quite fast, currently for a single press you will have multiple prints.

So to get around that, you will have to save that you printed it, and as long as that is saved you don't print it. If the button is released you clear your saved variable, so that it gets printed again on the next button press.

Something like this:

bool printedOut = false;
while (true)
{
    if (switch == pressed)
    {
        if (!printedOut)
        {
            putsUART("switch 1 pressed");
            printedOut = true;
        }
    }
    else
    {
        printedOut = false;
    }
}

Note that this is prone to bouncing of the switch and you'll have to debounce the switch either in software or in hardware to really only get one print per press.

Arsenal
  • 17,674
  • 1
  • 34
  • 60
0

You can "hold" the process with another 'while'. The putsUART will be executed after user release the switch.

while(1)
{
  if(switch == pressed)
  {
     while(switch == pressed); //this semicolon is important to hold the process
    putsUART("switch 1 pressed");
  }
}

Alternatively, we can place a holding after putsUART executed. With this code, putsUART will be executed after user press the switch.

while(1)
{
  if(switch == pressed)
  {
  putsUART("switch 1 pressed");
  while(switch == pressed); //this semicolon is important to hold the process
  }
}

You may add delay to eleminate the bouncing effect by replacing

while(switch == pressed);

with

while(switch == pressed) delay_ms(100);
Oka
  • 961
  • 4
  • 10
  • adding while(switch == pressed); is not working because it keeps on displaying switch 1 pressed whether i press the switch or not. If i remove ; from the while then it behaves exactly like if condtion – anna carolina Aug 25 '15 at 08:05
  • @annacarolina, sorry I forgot to place 'if' in the first code. I edited my answer – Oka Aug 25 '15 at 08:07
  • no it is still keep on displaying – anna carolina Aug 25 '15 at 08:11
  • so, in your system, the varible "switch" always has "pressed" value, even after the user release the switch? – Oka Aug 25 '15 at 08:12
  • no it will be 0 if not pressed – anna carolina Aug 25 '15 at 08:27
  • If my code and Arsenal code are not working, try to remove bouncing by adding delay first. If it is still not working, check your hardware configurations. A digital switch usually is pulled up to VCC with 10k resistor, so it has value 0 when pressed (connected to ground) and 1 if not pressed. When floating without pull-up, a microcontroller pin can have any random values (0/1) – Oka Aug 25 '15 at 08:50
  • Can you post your actual code in pastebin.com, so we can take a look at it? – ricardomenzer Aug 25 '15 at 12:28