Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
11 years, 10 months ago.
using getchar() to print a set of values
hi, i am using an accelerometer and after it's test connection i want to press a 'particular' character on the keyboard to display my values. the code i used can accept 'any' character on the keyboard.
char a;
printf("Enter character:"); a = getchar(); printf("Character entered:"); putchar(a);
1 Answer
11 years, 9 months ago.
I didn't compile the following, but it might be about what you want to do (embellished a bit):
#include "mbed.h"
Serial pc(USBTX, USBRX);
int main() {
pc.printf("\r\n\r\nMy Program - Build " __DATE__ " - " __TIME__ "\r\n");
while (1) {
if (pc.readable()) {
switch(pc.getc()) {
case 'a':
// do what you want for 'a'
break;
case '3':
// do what you want for '3'
break;
default:
pc.printf(" - unexpected character\r\n");
break;
}
}
// do other things here, if anything, that run all the time.
}
}
The "pc." prefix to the printf isn't really necessary, just my practice.