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 Acclerometer_node by
main.cpp@10:149ded218037, 2015-08-11 (annotated)
- Committer:
- jaehughes
- Date:
- Tue Aug 11 17:28:38 2015 +0000
- Revision:
- 10:149ded218037
- Parent:
- 9:934cae7f3290
With Xbee comms
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Owen | 0:a51a6e7da590 | 1 | #include "mbed.h" |
| henriwts | 6:7198985d3442 | 2 | #include "math.h" |
| jaehughes | 5:7c53f5f2af8d | 3 | |
| henriwts | 7:46da94ccfa67 | 4 | Serial pc (USBTX, USBRX); |
| jaehughes | 8:412307ba78b0 | 5 | Serial xbee (PTE0, PTE1); |
| henriwts | 7:46da94ccfa67 | 6 | |
| jaehughes | 9:934cae7f3290 | 7 | const int ARRAY_LENGTH = 50; |
| henriwts | 6:7198985d3442 | 8 | float temperature_celsius, temperature_voltage, light_voltage, raw_pir_reading, noise_voltage, noise_maximum, noise_average, noise_total, noise_variance, noise_sd, presence_percentage, num_consecutive_runs; |
| henriwts | 6:7198985d3442 | 9 | char publishString[40]; |
| henriwts | 6:7198985d3442 | 10 | float noise_array[ARRAY_LENGTH]; |
| henriwts | 6:7198985d3442 | 11 | int pir_array[ARRAY_LENGTH]; |
| jaehughes | 5:7c53f5f2af8d | 12 | |
| henriwts | 6:7198985d3442 | 13 | //bool DEBUG_MODE = false; |
| jaehughes | 5:7c53f5f2af8d | 14 | |
| jaehughes | 5:7c53f5f2af8d | 15 | AnalogIn light_ain(A0); |
| jaehughes | 5:7c53f5f2af8d | 16 | AnalogIn temp_ain(A1); |
| henriwts | 6:7198985d3442 | 17 | AnalogIn pir_ain(A2); |
| henriwts | 6:7198985d3442 | 18 | AnalogIn noise_ain(A3); |
| jaehughes | 5:7c53f5f2af8d | 19 | |
| henriwts | 6:7198985d3442 | 20 | void publish_measurements() { |
| henriwts | 6:7198985d3442 | 21 | temperature_voltage = temp_ain.read_u16(); |
| henriwts | 6:7198985d3442 | 22 | // Convert DAC reading to millivolts |
| henriwts | 6:7198985d3442 | 23 | temperature_voltage = (temperature_voltage * 3.3 * 1000) / 4095; |
| henriwts | 6:7198985d3442 | 24 | // Convert millivolts to Celsius using datasheet equation |
| henriwts | 6:7198985d3442 | 25 | temperature_celsius = (2103 - temperature_voltage) / 10.9; |
| henriwts | 6:7198985d3442 | 26 | //if(DEBUG_MODE) { |
| henriwts | 6:7198985d3442 | 27 | //Serial.println("Temperature (Celsius): " + String(temperature_celsius)); |
| henriwts | 6:7198985d3442 | 28 | //} |
| jaehughes | 5:7c53f5f2af8d | 29 | |
| henriwts | 6:7198985d3442 | 30 | light_voltage = light_ain.read_u16(); |
| jaehughes | 5:7c53f5f2af8d | 31 | |
| henriwts | 6:7198985d3442 | 32 | sprintf(publishString,"%.1f, %.1f, %.1f, %.1f, %.1f", temperature_celsius, light_voltage, noise_maximum, noise_average, noise_variance); |
| jaehughes | 8:412307ba78b0 | 33 | xbee.printf(publishString); |
| jaehughes | 9:934cae7f3290 | 34 | pc.printf("publish"); |
| jaehughes | 9:934cae7f3290 | 35 | pc.printf(publishString); |
| henriwts | 6:7198985d3442 | 36 | sprintf(publishString, "%.1f, %.1f", presence_percentage, num_consecutive_runs); |
| jaehughes | 10:149ded218037 | 37 | xbee.printf(publishString); |
| henriwts | 6:7198985d3442 | 38 | } |
| henriwts | 6:7198985d3442 | 39 | |
| henriwts | 6:7198985d3442 | 40 | |
| henriwts | 6:7198985d3442 | 41 | void measure_pir_and_noise() { |
| henriwts | 6:7198985d3442 | 42 | noise_voltage = 0; |
| jaehughes | 5:7c53f5f2af8d | 43 | |
| henriwts | 6:7198985d3442 | 44 | for (int i = 0; i < ARRAY_LENGTH; i++) { |
| henriwts | 6:7198985d3442 | 45 | raw_pir_reading = pir_ain.read_u16(); |
| henriwts | 6:7198985d3442 | 46 | noise_voltage = noise_ain.read_u16(); |
| henriwts | 6:7198985d3442 | 47 | noise_array[i] = noise_voltage; |
| henriwts | 6:7198985d3442 | 48 | if (raw_pir_reading > 3000) { |
| henriwts | 6:7198985d3442 | 49 | pir_array[i] = 1; |
| henriwts | 6:7198985d3442 | 50 | } else { |
| henriwts | 6:7198985d3442 | 51 | pir_array[i] = 0; |
| henriwts | 6:7198985d3442 | 52 | } |
| henriwts | 6:7198985d3442 | 53 | //if (DEBUG_MODE) { |
| henriwts | 6:7198985d3442 | 54 | //Serial.println("PIR: " + String(raw_pir_reading)); |
| henriwts | 6:7198985d3442 | 55 | //Serial.println("Noise: " + String(noise_voltage)); |
| henriwts | 6:7198985d3442 | 56 | //} |
| henriwts | 6:7198985d3442 | 57 | wait(0.1); |
| henriwts | 6:7198985d3442 | 58 | } |
| jaehughes | 5:7c53f5f2af8d | 59 | } |
| jaehughes | 5:7c53f5f2af8d | 60 | |
| jaehughes | 5:7c53f5f2af8d | 61 | |
| henriwts | 6:7198985d3442 | 62 | void noise_analysis() { |
| henriwts | 6:7198985d3442 | 63 | float residuals[ARRAY_LENGTH]; |
| henriwts | 6:7198985d3442 | 64 | noise_maximum = 0; |
| henriwts | 6:7198985d3442 | 65 | noise_average = 0; |
| henriwts | 6:7198985d3442 | 66 | noise_variance = 0; |
| henriwts | 6:7198985d3442 | 67 | noise_total = 0; |
| jaehughes | 5:7c53f5f2af8d | 68 | |
| henriwts | 6:7198985d3442 | 69 | for (int i = 0; i < ARRAY_LENGTH; i++) { |
| henriwts | 6:7198985d3442 | 70 | if (noise_array[i] > noise_maximum) noise_maximum = noise_array[i]; |
| henriwts | 6:7198985d3442 | 71 | noise_total += noise_array[i]; |
| henriwts | 6:7198985d3442 | 72 | } |
| henriwts | 6:7198985d3442 | 73 | noise_average = noise_total / (ARRAY_LENGTH * 1.0); |
| jaehughes | 5:7c53f5f2af8d | 74 | |
| henriwts | 6:7198985d3442 | 75 | for (int i = 0; i < ARRAY_LENGTH; i++) { |
| henriwts | 6:7198985d3442 | 76 | residuals[i] = noise_array[i] - noise_average; |
| henriwts | 6:7198985d3442 | 77 | noise_variance += residuals[i] * residuals[i]; |
| Owen | 0:a51a6e7da590 | 78 | } |
| henriwts | 6:7198985d3442 | 79 | noise_variance = noise_variance / (ARRAY_LENGTH * 1.0); |
| jaehughes | 5:7c53f5f2af8d | 80 | } |
| jaehughes | 5:7c53f5f2af8d | 81 | |
| jaehughes | 5:7c53f5f2af8d | 82 | |
| henriwts | 6:7198985d3442 | 83 | void pir_analysis() { |
| henriwts | 6:7198985d3442 | 84 | int counts_over_1s = 0; |
| henriwts | 6:7198985d3442 | 85 | int longest_consecutive_run = 0; |
| henriwts | 6:7198985d3442 | 86 | int curr_num_consecutive = 0; |
| henriwts | 6:7198985d3442 | 87 | |
| henriwts | 6:7198985d3442 | 88 | counts_over_1s = pir_array[0] == 1 ? 1 : 0; |
| henriwts | 6:7198985d3442 | 89 | for (int i = 1; i < ARRAY_LENGTH; i++) { |
| henriwts | 6:7198985d3442 | 90 | if (pir_array[i] == 1) { |
| henriwts | 6:7198985d3442 | 91 | ++counts_over_1s; |
| henriwts | 6:7198985d3442 | 92 | } |
| henriwts | 6:7198985d3442 | 93 | if (pir_array[i] == 1 && pir_array[i-1] == 1) { |
| henriwts | 6:7198985d3442 | 94 | ++curr_num_consecutive; |
| henriwts | 6:7198985d3442 | 95 | } else { |
| henriwts | 6:7198985d3442 | 96 | if (curr_num_consecutive > longest_consecutive_run) { |
| henriwts | 6:7198985d3442 | 97 | longest_consecutive_run = curr_num_consecutive; |
| henriwts | 6:7198985d3442 | 98 | curr_num_consecutive = 0; |
| henriwts | 6:7198985d3442 | 99 | } |
| henriwts | 6:7198985d3442 | 100 | } |
| henriwts | 6:7198985d3442 | 101 | } |
| henriwts | 6:7198985d3442 | 102 | |
| henriwts | 6:7198985d3442 | 103 | presence_percentage = 100 * counts_over_1s * 1.0 / ARRAY_LENGTH; |
| henriwts | 6:7198985d3442 | 104 | num_consecutive_runs = longest_consecutive_run; |
| henriwts | 6:7198985d3442 | 105 | } |
| henriwts | 6:7198985d3442 | 106 | |
| henriwts | 6:7198985d3442 | 107 | |
| henriwts | 6:7198985d3442 | 108 | int main() { |
| jaehughes | 8:412307ba78b0 | 109 | while (1) { |
| jaehughes | 8:412307ba78b0 | 110 | measure_pir_and_noise(); |
| jaehughes | 8:412307ba78b0 | 111 | noise_analysis(); |
| jaehughes | 8:412307ba78b0 | 112 | pir_analysis(); |
| jaehughes | 8:412307ba78b0 | 113 | publish_measurements(); |
| jaehughes | 8:412307ba78b0 | 114 | |
| henriwts | 6:7198985d3442 | 115 | } |
| henriwts | 6:7198985d3442 | 116 | } |
