11 years ago.

Skip getchar() if no input? using interrupts?

Hi i'm completely stuck now i've tryed some ways of doing this but have yet to get a solution.

In short i would like to read a value from a sensor display the value and keep doing this until a user presses 'e' to exit the loop and go back to a menu.

So i'm trying to display an anaglo input using printf() but at the same time i want to be able to exit this loop using a character input, in this case e. Which i'm trying to do using c = getchat();. However as soon as the program reaches c = getchar(); it stops and waits for a users input. I want the program to look for character but if no character is entered then carry on with the loop.

I was thinking of using an interrupt like timeout or ticker but not sure how to use them. Below is my code in question.

Repository: getchar_timeout

If i'm not stating what my question is clearly i'm looking for a way to skip past c = getchar; if there is NO user input. i.e. a timeout so that if say 100mS passes and no character has been entered then carry on with the while loop.

2 Answers

11 years ago.

#include "mbed.h"
#include "stdio.h"
 
AnalogIn Ac1(p15);  //Accelerometer #1 Y axis
Serial pc(USBTX, USBRX);     // Setting up serial to pc connection
 
float while1 = 0;
 
float maxTemp;     //Maximum axis value
float minTemp;     //Minimum axis value
 
float yAxis;   
float zAxis;
 
char c;
 
int Menu = 1;
int temp = 0;
 
 while (temp == 1)
        {
            yAxis = Ac1;            //yAxis = the analog input pin of AC1
                
            if(yAxis > maxTemp)     //Find the maximum value of yAxis
               maxTemp = yAxis;
          
            if(minTemp == 0)        //if yAsix is too low (i.e zero) then reset to the currnt value. (yaxis should never hit 0;)
                minTemp = yAxis;
                                
            if(yAxis < minTemp)     //Find the minimum value of yAxis
                minTemp = yAxis;
                    
            printf("| %0.3f | %0.5f | %0.3f |  %c \r", minTemp, yAxis, maxTemp, c);
                
            if(pc.readable()){    // Check if data is available on serial port.
                c = pcgetc();  //Exit this loop if the user presses 'e'
                if(c == 'e')
                    temp = 0;
            }
         }
            

Try it out, return if any troubles.

Lerche

Accepted Answer

Ah awesome cheers for this. Could you explain more on the pc.readable() function please :)

posted by Kai Longshaw 05 Apr 2013

pc.readable simply returns true if a char has been received on the serial port. So it only reads the char if there is something to read.

posted by Erik - 06 Apr 2013
11 years ago.

I would use a real Serial object, and then use the readable function. So define:

Serial pc(USBTX,USBRX);

Then in your code you can do:

if (pc.readable()) {
  c = pc.getc();  //Same as getchar();
  //Your if statements
}

There are also other nice options with generating an interrupt when a new char arrives, but this is probably the easiest.

Edit: Looks like I was too late ;)

Ahh, the magic of multiple minds, with the same thought :D

posted by Christian Lerche 05 Apr 2013