Node sonde HC-SR04
Le but de ce node est de mesurer la distance par ultrason en utilisant un module HC-SR04. La doc de ce composant est disponible ici : http://www.micropik.com/PDF/HCSR04.pdf
Utilisation, bah nombreuses : - niveau de sel d'adoucisseur - avec ajout d'un affichage ou chenillard de led, aide au garage pour rentrer la voiture - distance d'un objet - capture d'ouverture / fermeture - etc…
Le schémas :
/* Author : Fabrice Scheider AKA Denia * Modifications: Xylerk, EricDele * Description : Node for distance mesure using a HC-SR04 * Licence : CC-BY-SA */ #include <TimerOne.h> #include "Ydle.h" #define RX_PIN 12 #define TX_PIN 10 #define BT_PIN 3 #define DELAY_SEND 15000 // PIN for the HC-SR04 #define TRIG_PIN 2 // Trig #define ECHO_PIN 4 // Echo long echo_pulse; // pulse from the echo pin long calculated_cm; // result in cm after calculation const int DELAY = 1000; // Delay between sending a frame unsigned long last_send, cur_time; // Following the time ydle y(RX_PIN, TX_PIN, BT_PIN); void setup() { // setting the HC-SR04 output/input on arduino pinMode(TRIG_PIN, OUTPUT); digitalWrite(TRIG_PIN, LOW); pinMode(ECHO_PIN, INPUT); Serial.begin(9600); Serial.println("Node type : <complete here>"); // Initialisation of the Read Interrupt y.init_timer(); // Set the first time cur_time = millis(); last_send = cur_time; } void loop() { // Read a frame y.receive(); // Test if the Node was Initialized if(y.initialized()) { // Store current time cur_time = millis(); // Test if the delay between the last send is ok if(cur_time - last_send >= DELAY_SEND) { // Store the current time as last send time last_send = cur_time; // Trig on the HC-SR04 digitalWrite(TRIG_PIN, HIGH); // wait 10 micor seconds delayMicroseconds(10); // Trig off the HC-SR04 digitalWrite(TRIG_PIN, LOW); // Read the pulse from the ECHO pin echo_pulse = pulseIn(ECHO_PIN, HIGH); // Do the calcul to obtain in centimeter calculated_cm = echo_pulse / 58; // Show it Serial.print("Distance en cm : "); Serial.println(calculated_cm); // check if returns is valid if (isnan(calculated_cm) || calculated_cm > 4095) { Serial.println("Failed to use the sensor"); } else { // Make the frame Frame_t frame; // Set the frame with a ACKnowledge y.dataToFrame(&frame, YDLE_TYPE_STATE_ACK); // Add in the frame the data, choosing type in this list // YDLE_DATA_STATE On - OFF (4bits) // YDLE_DATA_DEGREEC Degrée Celsius ** -204.7 à 204.7 (12bits) // YDLE_DATA_DEGREEF Degrée Fahrenheit ** -204.7 à 204.7 (12bits) // YDLE_DATA_PERCENT Pourcentage ** -100% à 100% (12bits) // YDLE_DATA_DISTANCE Distance en Cm ** 0 à 4095 (12 bits) // YDLE_DATA_WATT Watt ** 0 à 1048575 (20bits) // YDLE_DATA_HUMIDITY Pourcentage humidité ** 0 à 100% (12bits) // YDLE_DATA_PRESSION Pression en hpa 0 à 4095 (12bits) y.addData(&frame, YDLE_DATA_DISTANCE, calculated_cm); Serial.println(" -- Sending the frame"); y.send(&frame); delay (DELAY); } } } else { // Node not initialized Serial.println("Node not initialized, use the lightning icon on the web site"); } }