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.
Dependencies: mbed
Fork of Nucleo-Analog_read by
Diff: main.cpp
- Revision:
- 1:dc5a274adc7d
- Parent:
- 0:242976b62902
- Child:
- 2:eb5ee8191175
--- a/main.cpp Thu Mar 12 23:40:48 2015 +0000
+++ b/main.cpp Fri Mar 13 01:37:57 2015 +0000
@@ -1,37 +1,86 @@
#include "mbed.h"
#define voltage 3300
-#define mv1(x) (x*voltage)
-#define mv2(x) ((x*voltage)/0xFFFF)
-#define mv3(x) (x*voltage)
+#define mv(x) ((x*voltage)/0xFFFF)
+
AnalogIn analog_value(A0);
+Serial pc(SERIAL_TX, SERIAL_RX);
+
DigitalOut led(LED1);
+Ticker toggle_ticker;
+int voltages =0;
+
+bool start = true;
+
+void toggle()
+{
+ int meas;
+
+ meas = analog_value.read_u16(); // Converts and read the analog input value (value from 0.0 to 0xFFFF)
+ meas = mv(meas); // Change the value to be in the 0 to 3300 range
+ while(!pc.writeable());
+
+
+ led= !led;
+ if (meas > voltages ) { // If the value is greater than 2V then switch the LED on
+ start = true;
+ }
+ if(start) {
+ printf("%d\n", meas);
+ }
+}
+
+void flushSerialPort()
+{
+ while(pc.readable())
+ pc.getc();
+ return;
+}
+
int main()
{
- float meas1;
- int meas2;
- float meas3;
-
- printf("\nAnalogIn example\n");
+ int prijData=0;
+ int Data1=0;
+ float Data2=0;
+ pc.baud(115200);
+ toggle_ticker.detach();
+ toggle_ticker.attach(&toggle, 0.01);
+ printf("\nAnalogIn example, data (numbers) are in mV.\n");
while(1) {
- meas1 = analog_value.read(); // Converts and read the analog input value (value from 0.0 to 1.0)
- meas1 = mv1(meas1); // Change the value to be in the 0 to 3300 range
-
- meas2 = analog_value.read_u16(); // Converts and read the analog input value (value from 0.0 to 0xFFFF)
- meas2 = mv2(meas2); // Change the value to be in the 0 to 3300 range
-
- meas3 = analog_value; // Converts and read the analog input value (value from 0.0 to 1.0)
- meas3 = mv3(meas3); // Change the value to be in the 0 to 3300 range
-
- printf("measure = %.0f mV, %d mV, %.0f mV\n", meas1,meas2,meas3);
- if (meas1 > 2000 && meas2 > 2000 && meas3 > 2000) { // If the value is greater than 2V then switch the LED on
- led = 1;
+ prijData=pc.scanf("%d %f",&Data1,&Data2);
+ if(prijData==2) {
+ //pc.printf()
+ if(Data1==1) {
+ toggle_ticker.detach();
+ toggle_ticker.attach(&toggle, Data2);
+ } else if(Data1==2) {
+ toggle_ticker.detach();
+ } else if(Data1==3) {
+ voltages=(int)Data2;
+ start = false;
+ }
} else {
- led = 0;
+ while(!pc.writeable());
+ printf("HELP - MENU\n");
+ while(!pc.writeable());
+ printf("Data send to PC (numbers) are in mV.\n");
+ while(!pc.writeable());
+ printf("Set data exactly.\n");
+ while(!pc.writeable());
+ printf("Write to console: \"xx yy\", where xx is a code of seting and yy his value.\n");
+ while(!pc.writeable());
+ printf("01 y.yyy - set period[s] to send data to PC and start reading value, example:01 0.01\n");
+ while(!pc.writeable());
+ printf("02 0 - stop reading value example:02 0\n");
+ while(!pc.writeable());
+ printf("03 yyyy - Wait for minimum voltage[mV] (to 3299) and send data example:03 2000\n");
+ while(!pc.writeable());
+ printf("end HELP\n");
+ flushSerialPort();
}
- wait(0.2); // 200 ms
+ // wait(0.2); // 200 ms
}
}
