![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
This code reads 5 analog inputs, converts them to string, then sends them over USART to PC. On PC LabVIEW app read and displays and logs values
Dependencies: mbed
This code reads analog inputs form A0 to A4, which then gets converted to string and sent over USART.
String are parsed in the following manner:
Start char: $ Delimiter char: # End char: ?
example: Reading A01=2.53V and A02=1.2V the resulted parsed string would be: $2.530#1.200#?
Revision 0:15f3699c39a5, committed 2019-03-29
- Comitter:
- ChrisLusetic
- Date:
- Fri Mar 29 15:49:15 2019 +0000
- Commit message:
- Initial and final commit;
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
mbed.bld | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 15f3699c39a5 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Mar 29 15:49:15 2019 +0000 @@ -0,0 +1,54 @@ +//TODO: dodati sd card logger +//TODO: dodati conversion for sensors +//TODO: Serial Read from LabView on start +//TODO: PWM output + +#include "mbed.h" +#include "stdio.h" +#include "string" + +#define SAMPLE_RATE 0.01 +Serial pc(USBTX, USBRX); // tx, rx + +Timer timer; +AnalogIn a_input[] = {A0,A1,A2,A3,A4}; +const int numberOfInputs=5; +float analog_values[numberOfInputs]; + +char buffer[100]; +char temp[30]; +void read_inputs(); +void convert_inputs(); +void send_results(); + +void flushSerialBuffer(void) { char char1 = 0; while (pc.readable()) { char1 = pc.getc(); } return; } + +int main() { + timer.start(); + flushSerialBuffer(); + while(1){ + if (timer.read() > SAMPLE_RATE) { + read_inputs(); + convert_inputs(); + send_results(); + timer.reset(); + } + } + } +void read_inputs(){ + for (int i=0;i<numberOfInputs;i++){ + analog_values[i]= a_input[i].read()*3.3; //convert to voltage + } + } +void convert_inputs(){ + for (int i=0;i<numberOfInputs;i++){ + sprintf(temp,"%.3f",analog_values[i]); //convert float to string + strcat(temp,"#"); //add delimiter + strcat(buffer,temp); //concatenate buffer and new value + } + strcat(buffer,"?"); //end char + } +void send_results(){ + pc.printf(buffer); //serial send buffer + sprintf(buffer,"$"); //start char + } \ No newline at end of file
diff -r 000000000000 -r 15f3699c39a5 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri Mar 29 15:49:15 2019 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file