abc

Dependencies:   mbed-rtos mbed pixy pixy_test

Fork of aUtO_volume_v4 by ese519

main.cpp

Committer:
jfields
Date:
2015-11-24
Revision:
1:5302a23c97c4
Parent:
0:6618ecb9567e
Child:
2:1354ce60c0b9

File content as of revision 1:5302a23c97c4:

#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) {
       
        // get pixy data
        uint16_t blocks;
        blocks = pixy.getBlocks();
        
        // store data
        if (blocks) {
            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;
    }
}