Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 //----------------------------------------------------------------------------- 00002 // EXAMPLE: ANALOG READ AND STORE IN FILE 00003 // Simple example program to read 100 analog samples at half second intervals. 00004 // The program stores all 100 values and marks each sample as either 00005 // "above" or "below" the chosen threshold of 0.6. The values are 00006 // stored in text file analog.txt in the MBED folder. The samples are 00007 // also written out to stdout (TeraTerm). 00008 // LED 1 turns ON when the sample goes above 0.6, and OFF when it 00009 // falls below 0.6. LED2 toggles ON/OFF to show program activity. 00010 // 00011 // D. Wendelboe 14 July 2010 - Submitted "as is". No guarantees implied. 00012 //----------------------------------------------------------------------------- 00013 00014 #include "mbed.h" 00015 00016 #define ON 1 00017 #define OFF 0 00018 00019 LocalFileSystem local("local"); 00020 AnalogIn myinput(p20); 00021 DigitalOut myled(p5); 00022 DigitalOut led1(LED1); 00023 DigitalOut led2(LED2); 00024 00025 float analog_value; 00026 int count = 0; 00027 00028 int main() { 00029 printf("Read and store 100 analog sample at 0.5 sec interval\r\n"); 00030 00031 FILE *fp = fopen("/local/analog.txt","w"); // Open a file to save samples 00032 printf("File analog.txt is open.\r\n"); 00033 00034 while (count < 100) { 00035 analog_value = myinput.read(); // Read analog (range 0.0 to 1.0) 00036 if (analog_value > 0.6) // If value is above 0.6, then store 00037 { // it as an "above" value. 00038 fprintf (fp, "%3i: %f [above]\r\n", count+1, analog_value); 00039 led1 = ON; // Turn LED ON if sample was stored. 00040 00041 } else { // Below 0.6, store it as a "below" value. 00042 fprintf (fp, "%3i: %f [below]\r\n", count+1, analog_value); 00043 led1 = OFF; // Turn OFF if sample was not stored. 00044 } 00045 printf ("%3i: %f\r\n", count+1, analog_value); 00046 count++; // Increment sample counter. 00047 led2 = ON; // LED2 ON. 00048 wait_ms(250); // Wait 0.25 second 00049 led2 = OFF; // LED2 OFF. 00050 wait_ms(250); // Wait 0.25 second 00051 } 00052 fclose(fp); // Close the sampe file after 100 samples read. 00053 printf("File analog.txt closed.\r\n"); 00054 printf("Finished. 100 samples taken. Bye!\r\n"); 00055 }
Generated on Mon Jul 18 2022 19:35:23 by
1.7.2