MSOE EE2905 / Mbed 2 deprecated switchCase

Dependencies:   mbed

Fork of switchCase by Sheila Ross

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002   Switch statement
00003  
00004  Demonstrates the use of a switch statement.  The switch
00005  statement allows you to choose from among a set of discrete values
00006  of a variable.  It's like a series of if statements.
00007  
00008  To see this sketch in action, but the board and sensor in a well-lit
00009  room, open the serial monitor, and and move your hand gradually
00010  down over the sensor.
00011  
00012  The circuit:
00013  * photoresistor from analog in 0 to +3.3V
00014  * 10K resistor from analog in 0 to ground
00015  
00016  created 1 Jul 2009
00017  modified 9 Apr 2012
00018  by Tom Igoe 
00019  
00020  modified for Nucleo / mbed 12 Aug 2017
00021  by Sheila Ross
00022  
00023  This example code is in the public domain.
00024  
00025  http://www.arduino.cc/en/Tutorial/SwitchCase
00026  */
00027 
00028 
00029 #include "mbed.h"
00030 
00031 // Define the analog input we are using for the sensor
00032 AnalogIn sensorVoltage(A0);
00033 
00034 // Create a serial connection over our USB
00035 Serial pc(USBTX, USBRX);
00036 
00037 int main() {
00038   
00039     pc.baud(9600);  // Set serial communication speed
00040 
00041     while(1) {
00042 
00043         // Read the analog sensor
00044         // Rescale the reading to obtain a number between 0 and 3.999
00045         // which will be stored as 0 through 3 in the integer range
00046         
00047         int range = sensorVoltage*4;
00048 
00049         // do something different depending on the
00050         // range value:
00051         
00052         switch (range) {
00053         
00054             case 0:    // sensor is covered
00055         
00056                 pc.printf("dark\n");
00057                 break;
00058         
00059             case 1:    // sensor in dim light
00060         
00061                 pc.printf("dim\n");
00062                 break;
00063         
00064             case 2:    // sensor in medium light
00065         
00066                 pc.printf("medium\n");
00067                 break;
00068         
00069             case 3:    // bright light shining on sensor
00070          
00071                 pc.printf("bright\n");
00072                 break;
00073                 
00074             default:   // error situation
00075             
00076                 pc.printf("error\n");
00077 
00078         }
00079         
00080         wait(0.5);        // delay in between reads for stability
00081 
00082     }
00083 }
00084 
00085 
00086 
00087