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
00001 #include "mbed.h" 00002 #include "math.h" 00003 00004 Serial pc (USBTX, USBRX); 00005 Serial xbee (PTE0, PTE1); 00006 00007 const int ARRAY_LENGTH = 50; 00008 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; 00009 char publishString[40]; 00010 float noise_array[ARRAY_LENGTH]; 00011 int pir_array[ARRAY_LENGTH]; 00012 00013 //bool DEBUG_MODE = false; 00014 00015 AnalogIn light_ain(A0); 00016 AnalogIn temp_ain(A1); 00017 AnalogIn pir_ain(A2); 00018 AnalogIn noise_ain(A3); 00019 00020 void publish_measurements() { 00021 temperature_voltage = temp_ain.read_u16(); 00022 // Convert DAC reading to millivolts 00023 temperature_voltage = (temperature_voltage * 3.3 * 1000) / 4095; 00024 // Convert millivolts to Celsius using datasheet equation 00025 temperature_celsius = (2103 - temperature_voltage) / 10.9; 00026 //if(DEBUG_MODE) { 00027 //Serial.println("Temperature (Celsius): " + String(temperature_celsius)); 00028 //} 00029 00030 light_voltage = light_ain.read_u16(); 00031 00032 sprintf(publishString,"%.1f, %.1f, %.1f, %.1f, %.1f", temperature_celsius, light_voltage, noise_maximum, noise_average, noise_variance); 00033 xbee.printf(publishString); 00034 pc.printf("publish"); 00035 pc.printf(publishString); 00036 sprintf(publishString, "%.1f, %.1f", presence_percentage, num_consecutive_runs); 00037 xbee.printf(publishString); 00038 } 00039 00040 00041 void measure_pir_and_noise() { 00042 noise_voltage = 0; 00043 00044 for (int i = 0; i < ARRAY_LENGTH; i++) { 00045 raw_pir_reading = pir_ain.read_u16(); 00046 noise_voltage = noise_ain.read_u16(); 00047 noise_array[i] = noise_voltage; 00048 if (raw_pir_reading > 3000) { 00049 pir_array[i] = 1; 00050 } else { 00051 pir_array[i] = 0; 00052 } 00053 //if (DEBUG_MODE) { 00054 //Serial.println("PIR: " + String(raw_pir_reading)); 00055 //Serial.println("Noise: " + String(noise_voltage)); 00056 //} 00057 wait(0.1); 00058 } 00059 } 00060 00061 00062 void noise_analysis() { 00063 float residuals[ARRAY_LENGTH]; 00064 noise_maximum = 0; 00065 noise_average = 0; 00066 noise_variance = 0; 00067 noise_total = 0; 00068 00069 for (int i = 0; i < ARRAY_LENGTH; i++) { 00070 if (noise_array[i] > noise_maximum) noise_maximum = noise_array[i]; 00071 noise_total += noise_array[i]; 00072 } 00073 noise_average = noise_total / (ARRAY_LENGTH * 1.0); 00074 00075 for (int i = 0; i < ARRAY_LENGTH; i++) { 00076 residuals[i] = noise_array[i] - noise_average; 00077 noise_variance += residuals[i] * residuals[i]; 00078 } 00079 noise_variance = noise_variance / (ARRAY_LENGTH * 1.0); 00080 } 00081 00082 00083 void pir_analysis() { 00084 int counts_over_1s = 0; 00085 int longest_consecutive_run = 0; 00086 int curr_num_consecutive = 0; 00087 00088 counts_over_1s = pir_array[0] == 1 ? 1 : 0; 00089 for (int i = 1; i < ARRAY_LENGTH; i++) { 00090 if (pir_array[i] == 1) { 00091 ++counts_over_1s; 00092 } 00093 if (pir_array[i] == 1 && pir_array[i-1] == 1) { 00094 ++curr_num_consecutive; 00095 } else { 00096 if (curr_num_consecutive > longest_consecutive_run) { 00097 longest_consecutive_run = curr_num_consecutive; 00098 curr_num_consecutive = 0; 00099 } 00100 } 00101 } 00102 00103 presence_percentage = 100 * counts_over_1s * 1.0 / ARRAY_LENGTH; 00104 num_consecutive_runs = longest_consecutive_run; 00105 } 00106 00107 00108 int main() { 00109 while (1) { 00110 measure_pir_and_noise(); 00111 noise_analysis(); 00112 pir_analysis(); 00113 publish_measurements(); 00114 00115 } 00116 }
Generated on Mon Aug 8 2022 02:46:02 by
1.7.2
