Reads Analog and sends to the computer every 0.01 seconds.

Dependencies:   mbed

main.cpp

Committer:
Jamess
Date:
2015-02-24
Revision:
0:1b42934fe1a1

File content as of revision 0:1b42934fe1a1:

// Sending Serial Values Read from AnalogIN
// by Thiago Rech
//
//   When receiving the number '2', starts reading and sending the analog voltage in a 100Hz frequency*
//
// This is the first mbed project that I publish =D I would be thankfull if anyne give tips and point mistakes if any.


#include "mbed.h"

AnalogIn ain(PTB3);
DigitalOut led2(LED2);  // Led red
Ticker t1;              // Timer to send data
Serial pc(USBTX, USBRX);

void HandlerT1(void);
void rx_Handler(void);

volatile bool STATUS = true;
char test = 0;

int main()
{

    //Setup
    pc.baud(9600);
    pc.printf("Press 2 to Start");
    pc.attach(&rx_Handler, pc.RxIrq);
    led2=0;

    while(1) {

        wait(0.5);                                  // Debug
        led2 = 0;
        pc.putc(test);                             // Debug

        if (teste == '2') {
            t1.attach(&HandlerT1, 0.01);
        }

        while(test == '2') {

            if (STATUS == true) {

                STATUS = false;
                led2 = 1;
                pc.putc(analogValue);
                pc.printf("0x%04X",ain.read_u16());
            }


        } //end of while(test=='2'){}

        t1.detach();    // Detaches timer interruption when not needed
    }//end of while(1)
}//end of main

void HandlerT1(void)
{
    STATUS = true;
}

void rx_Handler(void)
{
    test = pc.getc();
    pc.putc(test);
}