/* Link between Arduino Nano 33 BLE Sense and phyphox app v1 * Allows to choose which experiment the Arduino Nano 33 BLE Sense should execute, using Phyphox. * by Gautier Creutzer and Frédéric Bouquet, La Physique Autrement / Physics Reimagined, Laboratoire de Physique des Solides / Laboratory of Solid state Physics, Université Paris-Saclay / Paris-Saclay University * Check our other projects in English and French: www.physicsreimagined.com * * This work is based on the phyphox Arduino library developed by the phyphox team at the RWTH Aachen University, which is released under the GNU Lesser General Public Licence v3.0 (or newer). * This work is released under the GNU Lesser General Public Licence v3.0 (or newer). * * C'est la suite de v1 (tous les capteurs sont activés - ca peut poser des problèmes sur la lecture de la pression) * * */ #include #include char board_name[] = "nano_33_ble_sense_rev2"; // to change the name displayed by the board using BLE: no space and no special character float temperature, humidity; unsigned long initial_time, first_time, fourth_time; float first_difference_float, fourth_difference_float; unsigned int period = 100; const int ledPin = 22; const int ledPin2 = 23; const int ledPin3 = 24; //distance sensor #include #define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the distance sensor. #define ECHO_PIN 11 // Arduino pin tied to echo pin on the distance sensor. #define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. float distance; float duration; void receivedData(); // see Phyphox Arduino Library example void setup() { PhyphoxBLE::minConInterval = 6; //6 = 7.5ms PhyphoxBLE::maxConInterval = 6; //6 = 7.5ms PhyphoxBLE::slaveLatency = 0; // PhyphoxBLE::timeout = 10; //10 = 100ms pinMode(22, OUTPUT); pinMode(23, OUTPUT); pinMode(24, OUTPUT); digitalWrite(ledPin, LOW); digitalWrite(ledPin2, HIGH); digitalWrite(ledPin3, HIGH); if (!HS300x.begin()) { while (1); } PhyphoxBLE::start(board_name); // the name of the board can be changed at the beginning of the program PhyphoxBLE::configHandler=&receivedData; // see Phyphox Arduino library example } void loop() // depending on the config parameter sent by Phyphox, an experiment is chosen { if (PhyphoxBLE::currentConnections > 0){ digitalWrite(ledPin, HIGH); digitalWrite(ledPin2, LOW); digitalWrite(ledPin3, HIGH); }else{ digitalWrite(ledPin, LOW); digitalWrite(ledPin2, HIGH); digitalWrite(ledPin3, HIGH); } speedofsound(); } void receivedData(){ // see Phyphox Arduino Library example if (PhyphoxBLE::currentConnections == 1){ //PhyphoxBLE::read(choice); // the "choice" variable is written by our Phyphox experiments initial_time = millis(); } } void speedofsound() { if ((unsigned long)(millis() - first_time) > period) { // to make sure that the board does not send too many times the same value first_time = millis(); delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings. unsigned int uS = sonar.ping_median(1); // Median: Send ping, get ping time in microseconds (uS). Default for median is 5 iterations. duration = 1*uS; temperature = HS300x.readTemperature(); humidity = HS300x.readHumidity(); first_difference_float = ((float)first_time-(float)initial_time)/1000; PhyphoxBLE::write(first_difference_float,duration,temperature,humidity); } }