Search code examples
cdo-whileavr

do while loop exiting pre-maturely


I have written code for reading switch status and exit if # is is pressed 3 times.

void allkeypadTest(void)
{
    static uint8_t modeKeyCount=0;

    do
    {
        uint8_t key=getKeyStatus();
        if(key)
        {
            if(key=='#')
            {
                modeKeyCount++;
                //pulseIODevice(LED1,1,500,200);
            }
            else
            {
                pulseIODevice(LED1,key-0x30,500,200);
            }
        }
     }while(modeKeyCount<3);
}

But as soon as I enter # key once, loop is exiting. Behavior is OK if I press other keys. However if I uncomment pulseIODevice present under if(key=='#') section, behavior is normal. pulseIODevice will toggle the LED for certain times at certain period and PWM passed to it. I am puzzled what went wrong in my code. Note that getKeyStatus will return '\0' (null) if no key is detected and return ASCII values of the keys of 1x4 keypad (ASCII values of 3, 6, 9 and # only)


Solution

  • One possible reason may be getkeyStatus() return "#" more than once when you press only one time. it is possible keypad device need some sleep time to clear its buffer memory i.e. as soon as you press a key its store into buffer which is getting multiple times.

    Try

    a. if possible printf("%c\n", key) and press only once and see how many times printed.

    b. if any function to clear key functions ? OR sleep(x ms).

    c. when you uncomment pulseIODevice () its working normal may be due to pulseIODevice () blocks few milli seconds by that time pressed key cleared from keypad buffer.