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, 7 months ago.
How to send an integer to mbed by C code
I want to communicate with mbed by my C code.I want to send a number to mbed by C code and that number led should blink.
which function can be used in the file to get integer from the C code that we run on the mbed compiler online?
1 Answer
11 years, 7 months ago.
Create a new project and copy this code into the main.cpp file:
#include "mbed.h"
Serial pc(USBTX, USBRX); // tx, rx
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
DigitalOut leds[] = { led1, led2, led3, led4 };
int main() {
int i;
for ( i = 0; i < 4; i++ )
leds[i] = 0;
pc.printf("Press '1' through '4' to turn on an LED\n\r");
while(1) {
char c = pc.getc();
pc.printf( "Got a character: %c\n\r", c );
if ( c > '0' and c < '5' ) {
i = c - '0' - 1; // convert to a zero-based index
leds[i] = !leds[i];
}
}
}
This will continuously read a character and, if the character is '1' through '4', turn on or off that led.
Compile and download the code to your mbed, reset the mbed, and then open a serial terminal to the mbed to enter the characters.