Components and supplies
Arduino Nano RP2040 Connect
Apps and platforms
Arduino IoT Cloud
ARDUINO IOT REMOTE
Project description
Code
FallDetection sketch
c
The sketch associated to the Nano RP2040 thing
1/* 2 Sketch generated by the Arduino IoT Cloud Thing "Untitled 3" 3 https://create.arduino.cc/cloud/things/43234da5-a323-46ad-9081-e284710d6b93 4 5 Arduino IoT Cloud Variables description 6 7 The following variables are automatically generated and updated when changes are made to the Thing 8 9 String message; 10 float accel; 11 float fall_intensity; 12 float threshold; 13 CloudLocation fall_location; 14 CloudLocation gps; 15 bool fall; 16 bool fall_clear; 17 CloudTime fall_detection_time; 18 19 Variables which are marked as READ/WRITE in the Cloud Thing will also have functions 20 which are called when their values are changed from the Dashboard. 21 These functions are generated with the Thing and added at the end of this sketch. 22*/ 23 24#include "thingProperties.h" 25 26#define THRESHOLD_DEFAULT 6 27 28float last_accel = 0; 29unsigned int fall_detected_time = 0; 30 31void setup() { 32 // Initialize serial and wait for port to open: 33 Serial.begin(9600); 34 // This delay gives the chance to wait for a Serial Monitor without blocking if none is found 35 delay(1500); 36 37 // Defined in thingProperties.h 38 initProperties(); 39 message = ""; 40 if (threshold == 0) 41 threshold = THRESHOLD_DEFAULT; 42 pinMode(LED_BUILTIN, OUTPUT); 43 digitalWrite(LED_BUILTIN, LOW); 44 45 // Connect to Arduino IoT Cloud 46 ArduinoCloud.begin(ArduinoIoTPreferredConnection); 47 48 /* 49 The following function allows you to obtain more information 50 related to the state of network and IoT Cloud connection and errors 51 the higher number the more granular information you’ll get. 52 The default is 0 (only errors). 53 Maximum is 4 54 */ 55 setDebugMessageLevel(2); 56 ArduinoCloud.printDebugInfo(); 57} 58 59void loop() { 60 ArduinoCloud.update(); 61 62 // Your code here 63 if (message == "") 64 message = "Starting!"; 65} 66 67 68 69 70 71/* 72 Since AccelX is READ_WRITE variable, onAccelXChange() is 73 executed every time a new value is received from IoT Cloud. 74*/ 75void onAccelXChange() { 76 // Add your code here to act upon AccelX change 77} 78 79/* 80 Since AccelY is READ_WRITE variable, onAccelYChange() is 81 executed every time a new value is received from IoT Cloud. 82*/ 83void onAccelYChange() { 84 // Add your code here to act upon AccelY change 85} 86 87/* 88 Since AccelZ is READ_WRITE variable, onAccelZChange() is 89 executed every time a new value is received from IoT Cloud. 90*/ 91void onAccelZChange() { 92 // Add your code here to act upon AccelZ change 93} 94 95/* 96 Since Accel is READ_WRITE variable, onAccelChange() is 97 executed every time a new value is received from IoT Cloud. 98*/ 99void onAccelChange() { 100 // Add your code here to act upon Accel change 101 if (accel - last_accel > threshold) { 102 fall = true; 103 fall_detection_time = millis() / 1000; 104 fall_intensity = accel - last_accel; 105 fall_location = gps; 106 digitalWrite(LED_BUILTIN, HIGH); 107 108 Location coordinates = gps.getValue(); 109 Location coordinates2 = fall_location.getValue(); 110 message = "Fall detected. Intensity:" + String(fall_intensity) + 111 " Time: " + String(fall_detection_time) + 112 " Latitude: " + String(coordinates.lat) + 113 " Longitude: " + String(coordinates.lon) + 114 " Lat: " + String(coordinates2.lat) + 115 " Long: " + String(coordinates2.lon); 116 Serial.println(message); 117 } 118 last_accel = accel; 119} 120 121/* 122 Since FallDetectionTime is READ_WRITE variable, onFallDetectionTimeChange() is 123 executed every time a new value is received from IoT Cloud. 124*/ 125void onFallDetectionTimeChange() { 126 // Add your code here to act upon FallDetectionTime change 127} 128 129/* 130 Since FallClear is READ_WRITE variable, onFallClearChange() is 131 executed every time a new value is received from IoT Cloud. 132*/ 133void onFallClearChange() { 134 // Add your code here to act upon FallClear change 135 fall = false; 136 fall_clear = false; 137 digitalWrite(LED_BUILTIN, LOW); 138 139 message = "Clearing changes. Time: " + String(millis() / 1000); 140 Serial.println(message); 141} 142 143/* 144 Since Threshold is READ_WRITE variable, onThresholdChange() is 145 executed every time a new value is received from IoT Cloud. 146*/ 147void onThresholdChange() { 148 // Add your code here to act upon Threshold change 149 message = " Threshold changed. Thr: " + String(threshold); 150 Serial.println(message); 151} 152 153/* 154 Since Gps is READ_WRITE variable, onGpsChange() is 155 executed every time a new value is received from IoT Cloud. 156*/ 157void onGpsChange() { 158 // Add your code here to act upon Gps change 159}
Comments
Only logged in users can leave comments