This example program shows how to plot sine and cosine waveforms in MegunoLink using the mbed platform. You can download this example from the link below. It includes a MegunoLink interface and mbed program.
Megunolink is fully compatible with the mbed platform and other microcontrollers capable of sending serial data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
#include "mbed.h" Serial device(p9, p10); // tx, rx Timer t; unsigned int currentTemperature = 0; void SendData(char *Series, float Data); void SetPlotTitle(char *Title); void SetXlabel(char *XLabel); void SetYlabel(char *YLabel); int main() { device.baud(9600); wait(2); //Wait a few seconds for serial to start SetPlotTitle("Sine and Cosine Function Waveforms"); SetXlabel("Time"); SetYlabel("Amplitude"); t.start(); while(1) { double dY, dY2; float seconds; float frequency = 0.5; //Hz float phase = 3.141/2; seconds = t.read(); dY = sin(2 * 3.141 * frequency * seconds); dY2 = cos(2 * 3.141 * frequency * seconds + phase); //Send Data To MegunoLink Pro SendData("Sinewave",dY); // Sinewave = series name, dY = data to plot SendData("Cosinewave",dY2); // By wrapping strings in F("") we can save ram by storing strings in program memory wait(0.01); //Wait 10ms } } void SendData(char *Series, float Data) { device.printf("{TIMEPLOT"); device.printf("|data|"); device.printf(Series); device.printf("|T|"); device.printf("%f",Data); device.printf("}"); } void SetPlotTitle(char *Title) { device.printf("{TIMEPLOT|SET|Title="); device.printf(Title); device.printf("}"); } void SetXlabel(char *XLabel) { device.printf("{TIMEPLOT|SET|X-Label="); device.printf(XLabel); device.printf("}"); } void SetYlabel(char *YLabel) { device.printf("{TIMEPLOT|SET|Y-Label="); device.printf(YLabel); device.printf("}"); } |