Simple taining demonstration to show the use of variables, constants and arrays

Dependencies:   USBDevice mbed

Fork of mbed_blinky by Mbed

Committer:
jf1452
Date:
Tue Nov 26 08:42:02 2013 +0000
Revision:
3:0f80147842c2
Parent:
2:db81cad8cb64
Demonstration showing the use of variables, constants and arrays.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jf1452 1:d9da28105bef 1 /*******************************************************************************
jf1452 3:0f80147842c2 2 * This program demonstrates the use of constants, variables and arrays *
jf1452 1:d9da28105bef 3 * *
jf1452 1:d9da28105bef 4 * Jon Fuge *
jf1452 2:db81cad8cb64 5 * V1.0 25/11/2013 First issue of code *
jf1452 1:d9da28105bef 6 *******************************************************************************/
dan 0:7dec7e9ac085 7
jf1452 2:db81cad8cb64 8 #include "mbed.h"
jf1452 2:db81cad8cb64 9 #include "USBSerial.h"
jf1452 3:0f80147842c2 10
jf1452 3:0f80147842c2 11 USBSerial serial; // Virtual serial port over USB. Use Teraterm as the interface
dan 0:7dec7e9ac085 12
jf1452 3:0f80147842c2 13 const float fPI = 3.1415927; // Declare a value for PI
jf1452 3:0f80147842c2 14 const char cDIAMETER[4] = {15,18,14,17}; // constant array of circle diameters
jf1452 3:0f80147842c2 15
jf1452 3:0f80147842c2 16 char cMatrix[2][3] = {{2,3,5},{7,11,13}}; // matrix array of circle diameters
dan 0:7dec7e9ac085 17 int main() {
jf1452 3:0f80147842c2 18 float fCircumference; // Declare local variables
jf1452 2:db81cad8cb64 19 wait (10); // Wait 10 seconds to connect port
jf1452 3:0f80147842c2 20
jf1452 3:0f80147842c2 21 // Calculate circumference then send the result to the USB serial port.
jf1452 3:0f80147842c2 22 fCircumference = fPI * 16; // Magic numbers should be avoided!
jf1452 3:0f80147842c2 23 serial.printf("Diameter:%i Circumference:%f\n\r", 16, fCircumference);
jf1452 3:0f80147842c2 24 fCircumference = fPI * (float)cDIAMETER[2]; // accessing an array.
jf1452 3:0f80147842c2 25 serial.printf("Diameter:%i Circumference:%f\n\r", cDIAMETER[2], fCircumference);
jf1452 3:0f80147842c2 26 fCircumference = fPI * (float)cMatrix[0][1]; // accessing an array.
jf1452 3:0f80147842c2 27 serial.printf("Diameter:%i Circumference:%f\n\r", cMatrix[0][1], fCircumference);
jf1452 3:0f80147842c2 28
jf1452 2:db81cad8cb64 29 for(;;) {} // Loop forever
jf1452 3:0f80147842c2 30 }