abc
Dependencies: mbed pixy pixy_test
Fork of pixy_test by
Diff: main.cpp
- Revision:
- 1:5302a23c97c4
- Parent:
- 0:6618ecb9567e
- Child:
- 2:1354ce60c0b9
diff -r 6618ecb9567e -r 5302a23c97c4 main.cpp --- a/main.cpp Wed Nov 18 21:03:06 2015 +0000 +++ b/main.cpp Tue Nov 24 01:06:04 2015 +0000 @@ -1,29 +1,52 @@ #include "mbed.h" #include "Pixy.h" +// init board Pixy pixy(Pixy::SPI, p11, p12, p13); Serial pc(USBTX, USBRX); +// global vars +int num_readings = 0; +int total_y = 0; + +// funcs +void get_volume(int i); + int main() { + + // init pixy pc.printf("ready\n\r"); pixy.setSerialOutput(&pc); + while (1) { - static int i = 0; - int j; + + // get pixy data uint16_t blocks; - blocks = pixy.getBlocks(); - + + // store data if (blocks) { - i++; - - if (i % 50 == 0) { - pc.printf("Detected %d:\n\r", blocks); - for (j = 0; j < blocks; j++) { - pc.printf(" block %d: ", j); - pixy.blocks[j].print(pc); - } + for (int j = 0; j < blocks; j++) { + get_volume(pixy.blocks[j].y); } } - } + } } + +void get_volume(int y) { + + // update data + total_y += y; + num_readings++; + + // output results + if (num_readings > 30) { + double average_y = total_y/num_readings; + double result = -42*average_y + 327; + pc.printf("y = %d, num_readings = %d, average = %d, mL = %d\r\n", y, num_readings, average_y, result); + + // reset vars + num_readings = 0; + total_y = 0; + } +} \ No newline at end of file