10 years, 1 month 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

10 years, 1 month 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.

I can do the communication by a serial terminal like minicom. But the thing is I want to communicate by a C code. For example by running a C code on the computer if I enter 1 then led 1 should be turned on.

posted by Astha Jada 18 Mar 2014