d
Dependencies: xtoff RF24Network mbed
main.cpp@0:a1bd5b12a602, 2018-07-12 (annotated)
- Committer:
- gimohd
- Date:
- Thu Jul 12 12:02:35 2018 +0000
- Revision:
- 0:a1bd5b12a602
hoi
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gimohd | 0:a1bd5b12a602 | 1 | #include "mbed.h" |
gimohd | 0:a1bd5b12a602 | 2 | |
gimohd | 0:a1bd5b12a602 | 3 | Serial pc(USBTX, USBRX); |
gimohd | 0:a1bd5b12a602 | 4 | |
gimohd | 0:a1bd5b12a602 | 5 | // Read the voltage |
gimohd | 0:a1bd5b12a602 | 6 | AnalogIn ain(A2); |
gimohd | 0:a1bd5b12a602 | 7 | |
gimohd | 0:a1bd5b12a602 | 8 | /*I2C |
gimohd | 0:a1bd5b12a602 | 9 | I2C i2c(D4, D5); |
gimohd | 0:a1bd5b12a602 | 10 | //Slave address |
gimohd | 0:a1bd5b12a602 | 11 | //char SLAVE_ADDRESS = 0b11110000; // 7 bits => 0b1111000 = 0x78 |
gimohd | 0:a1bd5b12a602 | 12 | |
gimohd | 0:a1bd5b12a602 | 13 | |
gimohd | 0:a1bd5b12a602 | 14 | char * i2cRead(){ |
gimohd | 0:a1bd5b12a602 | 15 | char data[2]; |
gimohd | 0:a1bd5b12a602 | 16 | i2c.read(SLAVE_ADDRESS, data, 2); |
gimohd | 0:a1bd5b12a602 | 17 | pc.printf("%s\r\n",data); |
gimohd | 0:a1bd5b12a602 | 18 | return data; |
gimohd | 0:a1bd5b12a602 | 19 | } |
gimohd | 0:a1bd5b12a602 | 20 | */ |
gimohd | 0:a1bd5b12a602 | 21 | |
gimohd | 0:a1bd5b12a602 | 22 | |
gimohd | 0:a1bd5b12a602 | 23 | |
gimohd | 0:a1bd5b12a602 | 24 | float TARE_VALUE = 0 ; |
gimohd | 0:a1bd5b12a602 | 25 | float CALIBRATION_VALUE = 0 ; |
gimohd | 0:a1bd5b12a602 | 26 | int CALIBRATION_MASS = 1131; |
gimohd | 0:a1bd5b12a602 | 27 | int SAMPLE_AMOUNT = 2000; |
gimohd | 0:a1bd5b12a602 | 28 | |
gimohd | 0:a1bd5b12a602 | 29 | |
gimohd | 0:a1bd5b12a602 | 30 | float analogRead() |
gimohd | 0:a1bd5b12a602 | 31 | { |
gimohd | 0:a1bd5b12a602 | 32 | float beginval = 0; |
gimohd | 0:a1bd5b12a602 | 33 | for(int i = 0; i < 100; i++) { |
gimohd | 0:a1bd5b12a602 | 34 | beginval += ain.read(); |
gimohd | 0:a1bd5b12a602 | 35 | |
gimohd | 0:a1bd5b12a602 | 36 | } |
gimohd | 0:a1bd5b12a602 | 37 | float gemiddeld = beginval/100; |
gimohd | 0:a1bd5b12a602 | 38 | pc.printf("Gemiddelde %f\r\n", gemiddeld); |
gimohd | 0:a1bd5b12a602 | 39 | |
gimohd | 0:a1bd5b12a602 | 40 | |
gimohd | 0:a1bd5b12a602 | 41 | float curval = 0; |
gimohd | 0:a1bd5b12a602 | 42 | int del = 0 ; |
gimohd | 0:a1bd5b12a602 | 43 | for(int i = 0; i < SAMPLE_AMOUNT; i++) { |
gimohd | 0:a1bd5b12a602 | 44 | if ( ain.read()< gemiddeld +0.001012*2 && ain.read()> gemiddeld -0.001012*2){ |
gimohd | 0:a1bd5b12a602 | 45 | curval += ain.read(); |
gimohd | 0:a1bd5b12a602 | 46 | }else { |
gimohd | 0:a1bd5b12a602 | 47 | del ++; |
gimohd | 0:a1bd5b12a602 | 48 | |
gimohd | 0:a1bd5b12a602 | 49 | } |
gimohd | 0:a1bd5b12a602 | 50 | |
gimohd | 0:a1bd5b12a602 | 51 | } |
gimohd | 0:a1bd5b12a602 | 52 | pc.printf("Deleted %d\r\n", del); |
gimohd | 0:a1bd5b12a602 | 53 | |
gimohd | 0:a1bd5b12a602 | 54 | return (curval + beginval) /(SAMPLE_AMOUNT-del + 100); |
gimohd | 0:a1bd5b12a602 | 55 | } |
gimohd | 0:a1bd5b12a602 | 56 | |
gimohd | 0:a1bd5b12a602 | 57 | float calculateMass(float value) |
gimohd | 0:a1bd5b12a602 | 58 | { |
gimohd | 0:a1bd5b12a602 | 59 | return ((TARE_VALUE - value)*(CALIBRATION_MASS))/(TARE_VALUE - CALIBRATION_VALUE); |
gimohd | 0:a1bd5b12a602 | 60 | } |
gimohd | 0:a1bd5b12a602 | 61 | |
gimohd | 0:a1bd5b12a602 | 62 | void log(char c[]) |
gimohd | 0:a1bd5b12a602 | 63 | { |
gimohd | 0:a1bd5b12a602 | 64 | pc.printf("%s\r\n", c); |
gimohd | 0:a1bd5b12a602 | 65 | } |
gimohd | 0:a1bd5b12a602 | 66 | |
gimohd | 0:a1bd5b12a602 | 67 | void tare() |
gimohd | 0:a1bd5b12a602 | 68 | { |
gimohd | 0:a1bd5b12a602 | 69 | TARE_VALUE = analogRead(); |
gimohd | 0:a1bd5b12a602 | 70 | pc.printf("Tare %f\r\n", TARE_VALUE); |
gimohd | 0:a1bd5b12a602 | 71 | } |
gimohd | 0:a1bd5b12a602 | 72 | |
gimohd | 0:a1bd5b12a602 | 73 | void init() |
gimohd | 0:a1bd5b12a602 | 74 | { |
gimohd | 0:a1bd5b12a602 | 75 | pc.printf("### INITIALISING ###\r\n"); |
gimohd | 0:a1bd5b12a602 | 76 | tare(); |
gimohd | 0:a1bd5b12a602 | 77 | pc.printf("### INIT COMPLETE ###\r\n"); |
gimohd | 0:a1bd5b12a602 | 78 | } |
gimohd | 0:a1bd5b12a602 | 79 | |
gimohd | 0:a1bd5b12a602 | 80 | |
gimohd | 0:a1bd5b12a602 | 81 | int main() |
gimohd | 0:a1bd5b12a602 | 82 | { |
gimohd | 0:a1bd5b12a602 | 83 | init(); |
gimohd | 0:a1bd5b12a602 | 84 | while (1) { |
gimohd | 0:a1bd5b12a602 | 85 | if(pc.readable()) { |
gimohd | 0:a1bd5b12a602 | 86 | switch (pc.getc()) { |
gimohd | 0:a1bd5b12a602 | 87 | case 't': |
gimohd | 0:a1bd5b12a602 | 88 | tare(); |
gimohd | 0:a1bd5b12a602 | 89 | break; |
gimohd | 0:a1bd5b12a602 | 90 | case 'c': |
gimohd | 0:a1bd5b12a602 | 91 | CALIBRATION_VALUE = analogRead(); |
gimohd | 0:a1bd5b12a602 | 92 | pc.printf("Callibrate %f\r\n", CALIBRATION_VALUE); |
gimohd | 0:a1bd5b12a602 | 93 | |
gimohd | 0:a1bd5b12a602 | 94 | break; |
gimohd | 0:a1bd5b12a602 | 95 | case 'y': |
gimohd | 0:a1bd5b12a602 | 96 | float mass = calculateMass(analogRead()); |
gimohd | 0:a1bd5b12a602 | 97 | pc.printf("MASS %f\r\n", mass); |
gimohd | 0:a1bd5b12a602 | 98 | |
gimohd | 0:a1bd5b12a602 | 99 | break; |
gimohd | 0:a1bd5b12a602 | 100 | |
gimohd | 0:a1bd5b12a602 | 101 | }; |
gimohd | 0:a1bd5b12a602 | 102 | |
gimohd | 0:a1bd5b12a602 | 103 | } |
gimohd | 0:a1bd5b12a602 | 104 | wait_ms(500); |
gimohd | 0:a1bd5b12a602 | 105 | |
gimohd | 0:a1bd5b12a602 | 106 | |
gimohd | 0:a1bd5b12a602 | 107 | } |
gimohd | 0:a1bd5b12a602 | 108 | return 0; |
gimohd | 0:a1bd5b12a602 | 109 | } |
gimohd | 0:a1bd5b12a602 | 110 | |
gimohd | 0:a1bd5b12a602 | 111 |