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

Dependencies:   USBDevice mbed

Fork of mbed_blinky by Mbed

Files at this revision

API Documentation at this revision

Comitter:
jf1452
Date:
Tue Nov 26 08:42:02 2013 +0000
Parent:
2:db81cad8cb64
Commit message:
Demonstration showing the use of variables, constants and arrays.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Nov 25 15:09:03 2013 +0000
+++ b/main.cpp	Tue Nov 26 08:42:02 2013 +0000
@@ -1,6 +1,5 @@
 /*******************************************************************************
-* This program demonstrates how to read a string from the USB serial device    *
-* and write a string back to the USB serial device                             *
+* This program demonstrates the use of constants, variables and arrays         *
 *                                                                              *
 * Jon Fuge                                                                     *
 * V1.0 25/11/2013 First issue of code                                          *
@@ -8,16 +7,24 @@
 
 #include "mbed.h"
 #include "USBSerial.h"
- 
-//Virtual serial port over USB. Use Teraterm as the interface
-USBSerial serial;
+
+USBSerial serial; // Virtual serial port over USB. Use Teraterm as the interface
 
+const float fPI = 3.1415927; // Declare a value for PI
+const char cDIAMETER[4] = {15,18,14,17}; // constant array of circle diameters
+
+char cMatrix[2][3] = {{2,3,5},{7,11,13}}; // matrix array of circle diameters
 int main() {
-   char buf[128]; // A char array is also a string!
+   float fCircumference; // Declare local variables
    wait (10); // Wait 10 seconds to connect port
-   // Need to type a key, then enter to get a response.
-   serial.printf("What is your name?\n\r");
-   serial.scanf("%s", buf);
-   serial.printf("What are you doing %s?\n\r", buf);
+
+   // Calculate circumference then send the result to the USB serial port.
+   fCircumference = fPI * 16; // Magic numbers should be avoided!
+   serial.printf("Diameter:%i Circumference:%f\n\r", 16, fCircumference);
+   fCircumference = fPI * (float)cDIAMETER[2]; // accessing an array.
+   serial.printf("Diameter:%i Circumference:%f\n\r", cDIAMETER[2], fCircumference);
+   fCircumference = fPI * (float)cMatrix[0][1]; // accessing an array.
+   serial.printf("Diameter:%i Circumference:%f\n\r", cMatrix[0][1], fCircumference);
+
    for(;;) {} // Loop forever
-}
+}
\ No newline at end of file