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#?
main.cpp
- Committer:
- ChrisLusetic
- Date:
- 2019-03-29
- Revision:
- 0:15f3699c39a5
File content as of revision 0:15f3699c39a5:
//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 }