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