Kristof T'Jonck / Mbed 2 deprecated CYS_Transmitter

Dependencies:   xtoff RF24Network mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 Serial pc(USBTX, USBRX);
00004 
00005 // Read the voltage
00006 AnalogIn ain(A2);
00007 
00008 /*I2C
00009 I2C i2c(D4, D5);
00010 //Slave address
00011 //char SLAVE_ADDRESS = 0b11110000; // 7 bits => 0b1111000 = 0x78
00012 
00013 
00014 char * i2cRead(){
00015      char data[2];
00016      i2c.read(SLAVE_ADDRESS, data, 2);
00017      pc.printf("%s\r\n",data);
00018      return data;
00019 }
00020 */
00021 
00022 
00023 
00024 float TARE_VALUE = 0 ;
00025 float CALIBRATION_VALUE = 0 ;
00026 int CALIBRATION_MASS = 1131;
00027 int SAMPLE_AMOUNT = 2000;
00028 
00029 
00030 float analogRead()
00031 {
00032     float beginval = 0;
00033      for(int i = 0; i < 100; i++) {
00034         beginval += ain.read();
00035 
00036     } 
00037     float gemiddeld = beginval/100;
00038     pc.printf("Gemiddelde %f\r\n", gemiddeld);
00039     
00040     
00041     float curval = 0;
00042     int del = 0 ;
00043     for(int i = 0; i < SAMPLE_AMOUNT; i++) {
00044         if ( ain.read()< gemiddeld +0.001012*2 && ain.read()> gemiddeld -0.001012*2){            
00045             curval += ain.read();
00046         }else {
00047             del ++;
00048             
00049         }
00050 
00051     }
00052         pc.printf("Deleted %d\r\n", del);
00053 
00054     return (curval + beginval) /(SAMPLE_AMOUNT-del + 100);
00055 }
00056 
00057 float calculateMass(float value)
00058 {
00059     return ((TARE_VALUE - value)*(CALIBRATION_MASS))/(TARE_VALUE - CALIBRATION_VALUE);
00060 }
00061 
00062 void log(char c[])
00063 {
00064     pc.printf("%s\r\n", c);
00065 }
00066 
00067 void tare()
00068 {
00069     TARE_VALUE = analogRead();
00070     pc.printf("Tare %f\r\n", TARE_VALUE);
00071 }
00072 
00073 void init()
00074 {
00075     pc.printf("### INITIALISING ###\r\n");
00076     tare();
00077     pc.printf("### INIT COMPLETE ###\r\n");
00078 }
00079 
00080 
00081 int main()
00082 {
00083     init();
00084     while (1) {
00085         if(pc.readable()) {
00086             switch (pc.getc()) {
00087                 case 't':
00088                     tare();
00089                     break;
00090                 case 'c':
00091                     CALIBRATION_VALUE = analogRead();
00092                     pc.printf("Callibrate %f\r\n", CALIBRATION_VALUE);
00093 
00094                     break;
00095                 case 'y':
00096                     float mass = calculateMass(analogRead());
00097                     pc.printf("MASS %f\r\n", mass);
00098 
00099                     break;
00100 
00101             };
00102 
00103         }
00104         wait_ms(500);
00105 
00106 
00107     }
00108     return 0;
00109 }
00110 
00111