abc

Dependencies:   mbed pixy pixy_test

Fork of pixy_test by ese519

Committer:
jfields
Date:
Tue Nov 24 01:06:04 2015 +0000
Revision:
1:5302a23c97c4
Parent:
0:6618ecb9567e
Child:
2:1354ce60c0b9
a

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jfields 0:6618ecb9567e 1 #include "mbed.h"
jfields 0:6618ecb9567e 2 #include "Pixy.h"
jfields 0:6618ecb9567e 3
jfields 1:5302a23c97c4 4 // init board
jfields 0:6618ecb9567e 5 Pixy pixy(Pixy::SPI, p11, p12, p13);
jfields 0:6618ecb9567e 6 Serial pc(USBTX, USBRX);
jfields 0:6618ecb9567e 7
jfields 1:5302a23c97c4 8 // global vars
jfields 1:5302a23c97c4 9 int num_readings = 0;
jfields 1:5302a23c97c4 10 int total_y = 0;
jfields 1:5302a23c97c4 11
jfields 1:5302a23c97c4 12 // funcs
jfields 1:5302a23c97c4 13 void get_volume(int i);
jfields 1:5302a23c97c4 14
jfields 0:6618ecb9567e 15 int main() {
jfields 1:5302a23c97c4 16
jfields 1:5302a23c97c4 17 // init pixy
jfields 0:6618ecb9567e 18 pc.printf("ready\n\r");
jfields 0:6618ecb9567e 19 pixy.setSerialOutput(&pc);
jfields 1:5302a23c97c4 20
jfields 0:6618ecb9567e 21 while (1) {
jfields 1:5302a23c97c4 22
jfields 1:5302a23c97c4 23 // get pixy data
jfields 0:6618ecb9567e 24 uint16_t blocks;
jfields 0:6618ecb9567e 25 blocks = pixy.getBlocks();
jfields 1:5302a23c97c4 26
jfields 1:5302a23c97c4 27 // store data
jfields 0:6618ecb9567e 28 if (blocks) {
jfields 1:5302a23c97c4 29 for (int j = 0; j < blocks; j++) {
jfields 1:5302a23c97c4 30 get_volume(pixy.blocks[j].y);
jfields 0:6618ecb9567e 31 }
jfields 0:6618ecb9567e 32 }
jfields 1:5302a23c97c4 33 }
jfields 0:6618ecb9567e 34 }
jfields 1:5302a23c97c4 35
jfields 1:5302a23c97c4 36 void get_volume(int y) {
jfields 1:5302a23c97c4 37
jfields 1:5302a23c97c4 38 // update data
jfields 1:5302a23c97c4 39 total_y += y;
jfields 1:5302a23c97c4 40 num_readings++;
jfields 1:5302a23c97c4 41
jfields 1:5302a23c97c4 42 // output results
jfields 1:5302a23c97c4 43 if (num_readings > 30) {
jfields 1:5302a23c97c4 44 double average_y = total_y/num_readings;
jfields 1:5302a23c97c4 45 double result = -42*average_y + 327;
jfields 1:5302a23c97c4 46 pc.printf("y = %d, num_readings = %d, average = %d, mL = %d\r\n", y, num_readings, average_y, result);
jfields 1:5302a23c97c4 47
jfields 1:5302a23c97c4 48 // reset vars
jfields 1:5302a23c97c4 49 num_readings = 0;
jfields 1:5302a23c97c4 50 total_y = 0;
jfields 1:5302a23c97c4 51 }
jfields 1:5302a23c97c4 52 }