Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MMA8451Q mbed nRF24L01P
Fork of Sensor_Box by
main.cpp@6:fb76109304df, 2015-07-01 (annotated)
- Committer:
- jaehughes
- Date:
- Wed Jul 01 22:13:56 2015 +0000
- Revision:
- 6:fb76109304df
- Parent:
- 5:7c53f5f2af8d
- Child:
- 7:03334e89478d
added stuff
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Owen | 0:a51a6e7da590 | 1 | #include "mbed.h" |
| Owen | 0:a51a6e7da590 | 2 | #include "nRF24L01P.h" |
| trisjph | 3:5c7f4e5a7605 | 3 | #include "MMA8451Q.h" |
| jaehughes | 5:7c53f5f2af8d | 4 | #include <vector> |
| jaehughes | 5:7c53f5f2af8d | 5 | #include <math.h> |
| jaehughes | 5:7c53f5f2af8d | 6 | |
| trisjph | 3:5c7f4e5a7605 | 7 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
| jaehughes | 5:7c53f5f2af8d | 8 | #define TRANSFER_SIZE 24 |
| jaehughes | 6:fb76109304df | 9 | #define ARRAY_LENGTH 20 |
| jaehughes | 5:7c53f5f2af8d | 10 | |
| Owen | 0:a51a6e7da590 | 11 | Serial pc(USBTX, USBRX); // tx, rx |
| jaehughes | 5:7c53f5f2af8d | 12 | |
| trisjph | 3:5c7f4e5a7605 | 13 | PinName const SDA = PTE25; |
| trisjph | 3:5c7f4e5a7605 | 14 | PinName const SCL = PTE24; |
| jaehughes | 5:7c53f5f2af8d | 15 | |
| jaehughes | 5:7c53f5f2af8d | 16 | AnalogIn light_ain(A0); |
| jaehughes | 5:7c53f5f2af8d | 17 | AnalogIn temp_ain(A1); |
| jaehughes | 5:7c53f5f2af8d | 18 | AnalogIn pir_ain(A3); |
| jaehughes | 6:fb76109304df | 19 | AnalogIn sound_ain(A4); |
| jaehughes | 6:fb76109304df | 20 | AnalogIn noise_ain(A5); |
| jaehughes | 5:7c53f5f2af8d | 21 | |
| jaehughes | 4:7d3a1dfe5454 | 22 | nRF24L01P my_nrf24l01p(PTD2, PTD3, PTD1, PTE1, PTE0, PTD0); // mosi, miso, sck, csn, ce, irq |
| jaehughes | 5:7c53f5f2af8d | 23 | |
| Owen | 0:a51a6e7da590 | 24 | DigitalOut myled1(LED1); |
| Owen | 0:a51a6e7da590 | 25 | DigitalOut myled2(LED2); |
| jaehughes | 5:7c53f5f2af8d | 26 | |
| jaehughes | 5:7c53f5f2af8d | 27 | uint16_t light; |
| jaehughes | 5:7c53f5f2af8d | 28 | uint16_t temp; |
| jaehughes | 5:7c53f5f2af8d | 29 | uint16_t pir; |
| jaehughes | 6:fb76109304df | 30 | uint16_t noise; |
| jaehughes | 5:7c53f5f2af8d | 31 | |
| jaehughes | 5:7c53f5f2af8d | 32 | std::vector<uint16_t> light_data; |
| jaehughes | 5:7c53f5f2af8d | 33 | std::vector<uint16_t> temp_data; |
| jaehughes | 5:7c53f5f2af8d | 34 | std::vector<uint16_t> pir_data; |
| jaehughes | 6:fb76109304df | 35 | std::vector<uint16_t> noise_data; |
| jaehughes | 5:7c53f5f2af8d | 36 | |
| jaehughes | 5:7c53f5f2af8d | 37 | void getdata() |
| jaehughes | 5:7c53f5f2af8d | 38 | { |
| jaehughes | 5:7c53f5f2af8d | 39 | light = light_ain.read_u16(); |
| jaehughes | 5:7c53f5f2af8d | 40 | temp = temp_ain.read_u16(); |
| jaehughes | 5:7c53f5f2af8d | 41 | pir = pir_ain.read_u16(); |
| jaehughes | 6:fb76109304df | 42 | noise = noise_ain.read_u16(); |
| jaehughes | 5:7c53f5f2af8d | 43 | |
| jaehughes | 5:7c53f5f2af8d | 44 | light_data.push_back (light); |
| jaehughes | 5:7c53f5f2af8d | 45 | temp_data.push_back (temp); |
| jaehughes | 5:7c53f5f2af8d | 46 | pir_data.push_back (pir); |
| jaehughes | 6:fb76109304df | 47 | noise_data.push_back (noise); |
| jaehughes | 5:7c53f5f2af8d | 48 | |
| jaehughes | 5:7c53f5f2af8d | 49 | light_data.pop_back(); |
| jaehughes | 5:7c53f5f2af8d | 50 | temp_data.pop_back(); |
| jaehughes | 5:7c53f5f2af8d | 51 | pir_data.pop_back(); |
| jaehughes | 6:fb76109304df | 52 | noise_data.pop_back(); |
| jaehughes | 6:fb76109304df | 53 | |
| jaehughes | 5:7c53f5f2af8d | 54 | printf("%f\n", light_data); |
| jaehughes | 5:7c53f5f2af8d | 55 | } |
| jaehughes | 5:7c53f5f2af8d | 56 | |
| jaehughes | 6:fb76109304df | 57 | void average() |
| jaehughes | 6:fb76109304df | 58 | { |
| jaehughes | 6:fb76109304df | 59 | int light_sum = 0; |
| jaehughes | 6:fb76109304df | 60 | int temp_sum = 0; |
| jaehughes | 6:fb76109304df | 61 | int noise_sum = 0; |
| jaehughes | 6:fb76109304df | 62 | float light_avg, temp_avg, noise_avg, light_var, temp_var, noise_var; |
| jaehughes | 6:fb76109304df | 63 | float light_residuals =0; |
| jaehughes | 6:fb76109304df | 64 | float temp_residuals = 0 ; |
| jaehughes | 6:fb76109304df | 65 | float noise_residuals = 0; |
| jaehughes | 6:fb76109304df | 66 | |
| jaehughes | 6:fb76109304df | 67 | for (int i = 0; i < ARRAY_LENGTH; i++) { |
| jaehughes | 6:fb76109304df | 68 | light_sum += light_data[i]; |
| jaehughes | 6:fb76109304df | 69 | temp_sum += temp_data[i]; |
| jaehughes | 6:fb76109304df | 70 | noise_sum += noise_data[i]; |
| jaehughes | 6:fb76109304df | 71 | } |
| jaehughes | 6:fb76109304df | 72 | light_avg = light_sum / (float)ARRAY_LENGTH; |
| jaehughes | 6:fb76109304df | 73 | temp_avg = temp_sum / (float)ARRAY_LENGTH; |
| jaehughes | 6:fb76109304df | 74 | noise_avg = noise_sum / (float)ARRAY_LENGTH; |
| jaehughes | 6:fb76109304df | 75 | |
| jaehughes | 6:fb76109304df | 76 | /* Compute variance and standard deviation */ |
| jaehughes | 6:fb76109304df | 77 | for (int i = 0; i < ARRAY_LENGTH; i++) { |
| jaehughes | 6:fb76109304df | 78 | light_residuals += pow((light_data[i] - light_avg), 2); |
| jaehughes | 6:fb76109304df | 79 | temp_residuals += pow((temp_data[i] - temp_avg), 2); |
| jaehughes | 6:fb76109304df | 80 | noise_residuals += pow((noise_data[i] - noise_avg), 2); |
| jaehughes | 6:fb76109304df | 81 | } |
| jaehughes | 6:fb76109304df | 82 | light_var = light_residuals/(float)ARRAY_LENGTH; |
| jaehughes | 6:fb76109304df | 83 | temp_var = temp_residuals/(float)ARRAY_LENGTH; |
| jaehughes | 6:fb76109304df | 84 | noise_var = noise_residuals/(float)ARRAY_LENGTH; |
| jaehughes | 6:fb76109304df | 85 | |
| jaehughes | 6:fb76109304df | 86 | printf("Light Average = %.2f\n", light_avg); |
| jaehughes | 6:fb76109304df | 87 | printf("Temp Average = %.2f\n", temp_avg); |
| jaehughes | 6:fb76109304df | 88 | printf("Noise Average = %.2f\n", noise_avg); |
| jaehughes | 6:fb76109304df | 89 | } |
| jaehughes | 6:fb76109304df | 90 | |
| jaehughes | 6:fb76109304df | 91 | |
| jaehughes | 5:7c53f5f2af8d | 92 | |
| trisjph | 3:5c7f4e5a7605 | 93 | int main() |
| trisjph | 3:5c7f4e5a7605 | 94 | { |
| jaehughes | 5:7c53f5f2af8d | 95 | |
| trisjph | 3:5c7f4e5a7605 | 96 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
| jaehughes | 5:7c53f5f2af8d | 97 | |
| jaehughes | 6:fb76109304df | 98 | light_data.assign (ARRAY_LENGTH,0); |
| jaehughes | 6:fb76109304df | 99 | temp_data.assign (ARRAY_LENGTH,0); |
| jaehughes | 6:fb76109304df | 100 | pir_data.assign (ARRAY_LENGTH,0); |
| jaehughes | 5:7c53f5f2af8d | 101 | |
| jaehughes | 5:7c53f5f2af8d | 102 | |
| Owen | 0:a51a6e7da590 | 103 | char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE]; |
| Owen | 0:a51a6e7da590 | 104 | int txDataCnt = 0; |
| Owen | 0:a51a6e7da590 | 105 | int rxDataCnt = 0; |
| jaehughes | 5:7c53f5f2af8d | 106 | |
| Owen | 0:a51a6e7da590 | 107 | my_nrf24l01p.powerUp(); |
| jaehughes | 5:7c53f5f2af8d | 108 | |
| Owen | 0:a51a6e7da590 | 109 | // Display the (default) setup of the nRF24L01+ chip |
| jaehughes | 4:7d3a1dfe5454 | 110 | //pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); |
| jaehughes | 4:7d3a1dfe5454 | 111 | //pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); |
| jaehughes | 4:7d3a1dfe5454 | 112 | //pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); |
| jaehughes | 4:7d3a1dfe5454 | 113 | //pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); |
| jaehughes | 4:7d3a1dfe5454 | 114 | //pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); |
| jaehughes | 5:7c53f5f2af8d | 115 | |
| jaehughes | 4:7d3a1dfe5454 | 116 | //pc.printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE ); |
| jaehughes | 5:7c53f5f2af8d | 117 | |
| Owen | 0:a51a6e7da590 | 118 | my_nrf24l01p.setTransferSize( TRANSFER_SIZE ); |
| jaehughes | 5:7c53f5f2af8d | 119 | |
| Owen | 0:a51a6e7da590 | 120 | my_nrf24l01p.setReceiveMode(); |
| Owen | 0:a51a6e7da590 | 121 | my_nrf24l01p.enable(); |
| jaehughes | 5:7c53f5f2af8d | 122 | |
| trisjph | 3:5c7f4e5a7605 | 123 | printf("MMA8451 ID: %d\n", acc.getWhoAmI()); |
| jaehughes | 5:7c53f5f2af8d | 124 | |
| Owen | 0:a51a6e7da590 | 125 | while (1) { |
| jaehughes | 5:7c53f5f2af8d | 126 | getdata(); |
| jaehughes | 6:fb76109304df | 127 | average(); |
| jaehughes | 5:7c53f5f2af8d | 128 | // txDataCnt = sprintf(txData, " %1.3f %1.3f %1.3f\n", x,y,z); |
| jaehughes | 5:7c53f5f2af8d | 129 | //my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt ); |
| trisjph | 3:5c7f4e5a7605 | 130 | myled1 = !myled1; |
| jaehughes | 5:7c53f5f2af8d | 131 | wait(0.5); |
| Owen | 0:a51a6e7da590 | 132 | } |
| jaehughes | 5:7c53f5f2af8d | 133 | } |
| jaehughes | 5:7c53f5f2af8d | 134 | |
| jaehughes | 5:7c53f5f2af8d | 135 |
