ese519 / Mbed 2 deprecated aUtO_volume_v3

Dependencies:   mbed pixy pixy_test

Fork of aUtO_volume_v2 by ese519

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Pixy.h"
00003 
00004 // init board
00005 Pixy pixy(Pixy::SPI, p11, p12, p13);
00006 Serial pc(USBTX, USBRX);
00007 DigitalOut pin1(p24);
00008 DigitalOut pin2(p25);
00009 
00010 #if   defined(TARGET_LPC1768)
00011 Serial blue(p9, p10);          // TX, RX
00012 //Serial blue(p13, p14);         // TX, RX
00013 #elif defined(TARGET_LPC4330_M4)
00014 Serial blue(P6_4, P6_5);         // UART0_TX, UART0_RX
00015 //Serial blue(P2_3, P2_4);         // UART3_TX, UART3_RX
00016 #endif
00017 
00018 // global vars
00019 int num_readings = 0;
00020 int total_y = 0;
00021 float result_level = 0; // mL
00022 float result_hourly = 0; // mL/hr
00023 float prev_level = 0;
00024 
00025 // funcs and threads
00026 void get_volume(int i);
00027 void get_hourly(void const *args);
00028 RtosTimer get_hourly_thread(get_hourly, osTimerPeriodic);
00029 
00030 int main() {
00031     
00032     // init bluetooth
00033     blue.baud(9600);
00034     
00035     // init pc
00036     pc.baud(9600);
00037     pc.printf("Bluetooth Start\r\n");
00038     pc.printf("ready\n\r");
00039     
00040     // init pixy
00041     pixy.setSerialOutput(&pc);
00042     
00043     // start entry pump
00044     pin1 = 1;
00045     pin2 = 0;
00046     
00047     // start hourly thread
00048     get_hourly_thread.start(10000);
00049 
00050     while (1) {
00051        
00052         // get pixy data
00053         uint16_t blocks;
00054         blocks = pixy.getBlocks();
00055         
00056         // store data
00057         if (blocks) {
00058             for (int j = 0; j < blocks; j++) {
00059                 get_volume(pixy.blocks[j].y);
00060             }
00061         }
00062     }
00063 }
00064 
00065 void update_display(void const *args) {
00066     result_hourly = (result_level-prev_level)*6
00067     prev_level = result_level;
00068 }
00069 
00070 void get_volume(int y) {
00071     
00072     // update data
00073     total_y += y;
00074     num_readings++;
00075     
00076     // output results
00077     if (num_readings >= 10) {
00078         float average_y = (float)total_y/num_readings;
00079         float result_level = -0.2642*average_y + 38.453;
00080         
00081         // to pc
00082         pc.printf("y = %d, num_readings = %d, average = %.2f, mL = %.2f, rate = %.2f\r\n", y, num_readings, average_y, result_level, result_hourly);
00083         pc.printf("%.2f %.2f\r\n", result_level, result_hourly);
00084         
00085         // to bluetooth
00086         //blue.printf("y = %d, num_readings = %d, average = %.2f, mL = %.2f\r\n", y, num_readings, average_y, result);
00087         blue.printf("%.2f\r\n", result);
00088 
00089         // reset vars
00090         num_readings = 0;
00091         total_y = 0;
00092     }
00093 }