Forum Électro-Bidouilleur
Sections => Vos projets et Montages d'Électronique => Discussion démarrée par: Manu le Mai 19, 2021, 08:12:12 pm
-
Bonjour,
Conçu pour mesurer un réservoir de 50 cm de pronfondeur et compter le temps écoulé depuis le dernier démarrage du moteur sur la dernière v2.
Sur la v1 Voici le code (modif du code d'origine du vendeur) que j'ai testé avec "ToF050F Laser Distance Sensor" pour éviter les glitchs et erreurs de lecture.
v1.0 -mesurer la distance du bluetooth
// http://forum.bidouilleur.ca/
// ToF050F Laser Sensor Distance v1.0 ManuModif
#include <MsTimer2.h>
#include <SoftwareSerial.h>
#include <Wire.h>
SoftwareSerial DT(3, 2); //[ToF.RX-->PIN PD2 Arduino Nano] [ToF.TX-->PIN PD3 Arduino Nano]
// HC-05 --> PIN TX RX shared of the USB Port PD1 PD0 ( to upload the code to the Arduino needs to disconnect the bluetooth HC-05 )
long distance = 0; // New Distance Value hex to mm
float A = 0; // New Distance Value for show display & compare B
float B = 0; // Old Distance Value for compare A
float D; // for mm (A) to %
byte Buf[6]; // Store data hex value
typedef unsigned char uint8; // for set MsTimer
typedef unsigned long uint32; // for set MsTimer
// part of the source-inspired code "TOFxxx_TEST_UNO.ino" Wavgat Store
uint32 timCount = 0; // set MsTimer2 - WAVGAT Official Store in Aliexpress parte of code extract
void tim_isr(void)
{
timCount++;
timCount++;
}
uint32 tim_get_count(void)
{
uint32 count;;
do{
count = timCount;
}while(count != timCount);
return count;
}
uint8 tim_check_timeout(uint32 start, uint32 now, uint32 invt)
{
if((uint32)(start + invt) >= start)
{
if((now >= (uint32)(start + invt)) || (now < start))
{
return 1;
}
}
else
{
if((now < start) && (now >= (uint32)(start + invt)))
{
return 1;
}
}
return 0;
}
void setup() {
Serial.begin(9600);
DT.begin(115200);
MsTimer2::set(2 ,tim_isr);
MsTimer2::start();
}
void loop() {
START:
if(DT.read() != 0x01){ // data check
return;
}
DT.readBytes(Buf, 6); // store data
if (Buf[2] == 0xFF){
return;
}
distance = Buf[2] * 256 + Buf[3]; // hex to mm
A = distance; // New Set Value A
if ( A > 499 || A < 1){ // Filter only possible New Values A from 500mm to 1mm for TOF050
return;
}
if ( A > (B+35) || A < (B-35) ){ // Compare old value B & new value A should be about 35mm top or down (to avoid wrong values glitch)
goto DATANOK;
}
goto DATAOK;
DATANOK: // Reset old value B ( to compare with new value )
if(DT.read()!= 0x01){
goto DATANOK;
}
DT.readBytes(Buf, 6);
if (Buf[2] == 0xFF){
goto DATANOK;
}
distance = Buf[2] * 256 + Buf[3];
A = distance;
if ( A > 500 || A < 1){
goto DATANOK;
}
B = A;
goto START; //old value B now set back to start to compare and validate
DATAOK: //Set old value B for next comparison & Show info A on display
B = A;
D = map(A,10,500,100,0); // A mm to xx % ( D = map(A , mmMIN , mmMAX , %MAX , %MIN )
if (D>95){
Serial.print("[]]]]]]]]]]]]]]]]]]]]]");
}
else if (D>90){
Serial.print("[]]]]]]]]]]]]]]]]]]] ]");
}
else if (D>85){
Serial.print("[]]]]]]]]]]]]]]]]]] ]");
}
else if (D>80){
Serial.print("[]]]]]]]]]]]]]]]] ]");
}
else if (D>75){
Serial.print("[]]]]]]]]]]]]]]] ]");
}
else if (D>70){
Serial.print("[]]]]]]]]]]]]]] ]");
}
else if (D>65){
Serial.print("[]]]]]]]]]]]]] ]");
}
else if (D>60){
Serial.print("[]]]]]]]]]]]] ]");
}
else if (D>55){
Serial.print("[]]]]]]]]]]] ]");
}
else if (D>50){
Serial.print("[]]]]]]]]]] ]");
}
else if (D>45){
Serial.print("[]]]]]]]]] ]");
}
else if (D>40){
Serial.print("[]]]]]]]] ]");
}
else if (D>35){
Serial.print("[]]]]]]] ]");
}
else if (D>30){
Serial.print("[]]]]]] ]");
}
else if (D>25){
Serial.print("[]]]]] ]");
}
else if (D>20){
Serial.print("[]]]] ]");
}
else if (D>15){
Serial.print("[]]] ]");
}
else if (D>10){
Serial.print("[]] ]");
}
else if (D>5){
Serial.print("[] ]");
}
else{
Serial.print(" VACIO "); // tank is empty
}
Serial.print(" ");
Serial.print(D,0);
Serial.println(" % "); // send Serial text "[]]]]] ] 50%" -> UART (USB) for Bluetooth
//delay(10);
}
(https://i.ibb.co/HnJ2RNm/Screenshot-20210519-214228.jpg)
Interrupteur sur le "5v" du HC05 pour le déconnecter et ainsi pouvoir télécharger le code du PC vers l'arduino.
(L'écran oled 128x32 pin A4 A5 a l'arrière de l'arduino non utilisé)
(https://i.ibb.co/KKbMSNx/IMG-20210519-212914-01.jpg)
(https://i.ibb.co/5kCbMJ8/Screenshot-20210519-213118.jpg)
(https://i.ibb.co/MnqDmFh/To-F-L-ser-de-rango.png)
V1.1: 2021-05-26
-Réinitialiser l’arduino à partir de l’app command "reset"
Résumé:
String cmd;
String cmdRst ="reset";
String cmd = Serial.readStringUntil('\r'); // ('\r') for HC05 & app, ('\n') for command send from serial monitor arduino ide
if(cmd == cmdRst ){ // if receive reset command
// http://forum.bidouilleur.ca/
// ToF050F Laser Sensor Distance v1.1 ManuModif
#include <MsTimer2.h>
#include <SoftwareSerial.h>
#include <Wire.h>
SoftwareSerial DT(3, 2); //[ToF.RX-->PIN 2 Arduino Nano] [ToF.TX-->PIN 3 Arduino Nano]
long distance = 0; // New Distance Value hex to mm
float A = 0; // New Distance Value for show display & compare B
float B = 0; // Old Distance Value for compare A
float D; // for mm (A) to %
byte Buf[6]; // Store data hex value
typedef unsigned char uint8; // for set MsTimer
typedef unsigned long uint32; // for set MsTimer
String cmd ="0"; // for command rx
String cmdRst ="reset"; // reset command
uint32 timCount = 0; // set MsTimer2 - WAVGAT Official Store in Aliexpress parte of code extract
void tim_isr(void)
{
timCount++;
timCount++;
}
uint32 tim_get_count(void)
{
uint32 count;;
do{
count = timCount;
}while(count != timCount);
return count;
}
uint8 tim_check_timeout(uint32 start, uint32 now, uint32 invt)
{
if((uint32)(start + invt) >= start)
{
if((now >= (uint32)(start + invt)) || (now < start))
{
return 1;
}
}
else
{
if((now < start) && (now >= (uint32)(start + invt)))
{
return 1;
}
}
return 0;
}
void setup() {
Serial.begin(9600);
DT.begin(115200);
MsTimer2::set(2 ,tim_isr);
MsTimer2::start();
delay(500);
Serial.println("");
Serial.println("Démarrage...");
delay(5000);
}
void(* resetFunc) (void) = 0; // For reset (addrees 0)
void loop() {
START:
if(Serial.available()){
String cmd = Serial.readStringUntil('\r'); // ('\r') for HC05 & app, ('\n') for command send from serial monitor arduino ide
if(cmd == cmdRst ){ // if receive reset command
Serial.println("Redémarrage..."); // restart msg
delay(5000); // send by 5seg
resetFunc(); // reset arduino
}
}
if(DT.available()){
if(DT.read() != 0x01){ // data check
return;
}
DT.readBytes(Buf, 6); // store data
if (Buf[2] == 0xFF){
return;
}
distance = Buf[2] * 256 + Buf[3]; // hex to mm
A = distance; // New Set Value A
if ( A > 499 || A < 1){ // Filter only possible New Values A from 500mm to 1mm for TOF050
return;
}
if ( A > (B+35) || A < (B-35) ){ // Compare old value B & new value A should be about 35mm top or down (to avoid wrong values glitch)
goto DATANOK;
}
goto DATAOK;
}
goto START;
DATANOK: // Reset old value B ( to compare with new value )
if(DT.read()!= 0x01){
goto DATANOK;
}
DT.readBytes(Buf, 6);
if (Buf[2] == 0xFF){
goto DATANOK;
}
distance = Buf[2] * 256 + Buf[3];
A = distance;
if ( A > 500 || A < 1){
goto DATANOK;
}
B = A;
goto START; //old value B now set back to start to compare and validate
DATAOK: //Set old value B for next comparison & Show info A on display
B = A;
D = map(A,10,500,100,0); // A mm to xx % ( D = map(A , mmMIN , mmMAX , %MAX , %MIN )
if (D>95){
Serial.print("[]]]]]]]]]]]]]]]]]]]]]");
}
else if (D>90){
Serial.print("[]]]]]]]]]]]]]]]]]]] ]");
}
else if (D>85){
Serial.print("[]]]]]]]]]]]]]]]]]] ]");
}
else if (D>80){
Serial.print("[]]]]]]]]]]]]]]]] ]");
}
else if (D>75){
Serial.print("[]]]]]]]]]]]]]]] ]");
}
else if (D>70){
Serial.print("[]]]]]]]]]]]]]] ]");
}
else if (D>65){
Serial.print("[]]]]]]]]]]]]] ]");
}
else if (D>60){
Serial.print("[]]]]]]]]]]]] ]");
}
else if (D>55){
Serial.print("[]]]]]]]]]]] ]");
}
else if (D>50){
Serial.print("[]]]]]]]]]] ]");
}
else if (D>45){
Serial.print("[]]]]]]]]] ]");
}
else if (D>40){
Serial.print("[]]]]]]]] ]");
}
else if (D>35){
Serial.print("[]]]]]]] ]");
}
else if (D>30){
Serial.print("[]]]]]] ]");
}
else if (D>25){
Serial.print("[]]]]] ]");
}
else if (D>20){
Serial.print("[]]]] ]");
}
else if (D>15){
Serial.print("[]]] ]");
}
else if (D>10){
Serial.print("[]] ]");
}
else if (D>5){
Serial.print("[] ]");
}
else{
Serial.print(" VIDE "); // tank is empty
}
Serial.print(" ");
Serial.print(D,0);
Serial.println(" % "); // send Serial text "[]]]]] ] 50%" -> UART (USB) for Bluetooth
//delay(10);
}
Test du "reset":
(https://i.ibb.co/8xRyG1S/temp.jpg)
v2.0 -2021-06-21 Ajout d'une sonde de courant pour compter le temps écoulé depuis le dernier démarrage du moteur
/*
ToF050F Laser Sensor Distance v2 ManuModif
designed to measure a 50cm tank and count the time since the engine was last started.
v1.0 -measure distance from bluetooth
v1.1 -added reset command from bluetooth
v2.0 -Added current meter to count time since the last time the engine was started
http://forum.bidouilleur.ca/
thanks:
-papyblue
-kamill
PIN:
-Current Sensor HCS-LSP 20A PIN: A0 (no need resistance)
-HC05 Bluetooth PIN: TX-->RX & RX-->TX . shared of the USB Port PD1 PD0 (to upload the code to the Arduino needs to turn off the bluetooth HC-05)
-TOF050F PIN: ToF.RX-->PD2 ToF.TX-->PD3
*/
#include <MsTimer2.h>
#include <SoftwareSerial.h>
#include <Wire.h>
SoftwareSerial DT(3, 2); //[ToF.RX-->PIN 2 Arduino Nano] [ToF.TX-->PIN 3 Arduino Nano]
long distance = 0; // New Distance Value hex to mm
float A = 0; // New Distance Value for show display & compare B
float B = 0; // Old Distance Value for compare A
float D; // for mm (A) to %
byte Buf[6]; // Store data hex value
typedef unsigned char uint8; // for set MsTimer
typedef unsigned long uint32; // for set MsTimer
String cmd ="0"; // reset for command rx
String cmdRst ="reset"; // reset for command reset text
int PinCurSens = A0; // Current Sensor analog input A0
int CurSens = 0; // current sensor value
int CSZero = 535; // Current Sensor value activation for reset on timer (value depends on the sensor used)
float sec=0; // Timer sec
float min=0; // Timer minut
float hour=0; // Timer hour
float day=0; // Timer day
unsigned long previousMillis = 0; // Timer previous time measured to compare with the next
unsigned long MsMin = 60000; // Timer to add 1 min every 60000ms (60sec)
const long MinHour = 60; // Timer to add 1 hour every 60 min
const long HourDay = 24; // Timer to add 1 day every 24 hours
uint32 timCount = 0; // set MsTimer2 - WAVGAT Official Store in Aliexpress parte of code extract
void tim_isr(void)
{
timCount++;
timCount++;
}
uint32 tim_get_count(void)
{
uint32 count;;
do{
count = timCount;
}while(count != timCount);
return count;
}
uint8 tim_check_timeout(uint32 start, uint32 now, uint32 invt)
{
if((uint32)(start + invt) >= start)
{
if((now >= (uint32)(start + invt)) || (now < start))
{
return 1;
}
}
else
{
if((now < start) && (now >= (uint32)(start + invt)))
{
return 1;
}
}
return 0;
}
void setup() {
Serial.begin(9600); // Start serial port
DT.begin(115200); // Start port of communication for the tof
MsTimer2::set(2 ,tim_isr); // Config MsTimer
MsTimer2::start(); // Config MsTimer
delay(500); // pause 500ms
Serial.println(""); // write nothing and go to next line
Serial.println("Démarrage..."); // write start message
delay(2000); // pause 2second = 2000ms
}
void(* resetFunc) (void) = 0; // when requesting a reset this is executed (addrees 0)
void loop() {
//Timer-----------------------------------------------------
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= MsMin){ // if the result is equal to or greater than 60000ms (60second)
min++; // add 1min
previousMillis = currentMillis; // set new value for next calculation of ms
}
sec=((currentMillis - previousMillis)/1000); // for sec
if(min==MinHour){ // if the result is equal to or greater than 60min
hour++; // add 1hour
min=0; // start 0 minute of the hour
}
if(hour==HourDay){ // if the result is equal to or greater than 24hours
day++; // add 1 day
hour=0; // start 0 hour of the day
}
START:
//Command rx----------------------------------------------------------------
if(Serial.available()){
String cmd = Serial.readStringUntil('\r');// ('\r') for HC05 & app, ('\n') for command send from serial monitor arduino ide
if(cmd == cmdRst ){ // if receive reset command
Serial.println("Redémarrage..."); // restart msg
delay(5000); // send by 5seg
resetFunc(); // reset arduino
}
}
//ToF data check-----------------------------------------------------------
if(DT.available()){
if(DT.read() != 0x01){ // data check
return;
}
DT.readBytes(Buf, 6); // store data
if (Buf[2] == 0xFF){
return;
}
distance = Buf[2] * 256 + Buf[3]; // hex to mm
A = distance; // New Set Value A
if ( A > 499 || A < 1){ // Filter only possible New Values A from 500mm to 1mm for TOF050
return;
}
if ( A > (B+35) || A < (B-35) ){ // Compare old value B & new value A should be about 35mm top or down (to avoid wrong values glitch)
goto DATANOK;
}
goto DATAOK;
}
goto START;
DATANOK: // Reset old value B ( to compare with new value )
if(DT.read()!= 0x01){
goto DATANOK;
}
DT.readBytes(Buf, 6);
if (Buf[2] == 0xFF){
goto DATANOK;
}
distance = Buf[2] * 256 + Buf[3];
A = distance;
if ( A > 500 || A < 1){
goto DATANOK;
}
B = A;
goto START; //old value B now set back to start to compare and validate
DATAOK: //Set old value B for next comparison & Show info A on display
B = A;
D = map(A,10,500,100,0); // A mm to xx % ( D = map(A , mmMIN , mmMAX , %MAX , %MIN )
if (D>90){ // is greater than 90%
Serial.print("[]]]]]]]]]]]");
}
else if (D>80){ // is greater than 80%
Serial.print("[]]]]]]]]] ]");
}
else if (D>70){ // is greater than 70%
Serial.print("[]]]]]]]] ]");
}
else if (D>60){ // is greater than 60%
Serial.print("[]]]]]]] ]");
}
else if (D>50){ // is greater than 50%
Serial.print("[]]]]]] ]");
}
else if (D>40){ // is greater than 40%
Serial.print("[]]]]] ]");
}
else if (D>30){ // is greater than 30%
Serial.print("[]]]] ]");
}
else if (D>20){ // is greater than 20%
Serial.print("[]]] ]");
}
else if (D>10){ // is greater than 10%
Serial.print("[]] ]");
}
else if (D>5){ // is greater than 50%
Serial.print("[] ]");
}
else{
Serial.print("[ ]"); // tank is empty
}
CurSens = analogRead(PinCurSens); // read current analog pin and save value
//if activation current is detected ----------------------------------------------------
if (CurSens<=CSZero){ // Compares the saved current value by the trigger value
Serial.println("ON"); // show ON
previousMillis = currentMillis; // reset timer previousMilllis
sec=0; // reset timer sec
min=0; // reset timer min
hour=0; // reset timer hour
day=0; // reset timer day
}
else if (day==0&&hour==0&&min==0&&sec==0){ // Switch for Timer no started yet
Serial.println("ON"); // show ON
}
//if no activation current is detected ----------------------------------------------------
else{
Serial.print("OFF"); // write OFF
Serial.print(" "); // write a space
if(day<10){ // if it is less than 10 put a zero in front
Serial.print("0"); // write 0
}
Serial.print(day,0); // write day value
Serial.print("d"); // write d
if(hour<10){ // if it is less than 10 put a zero in front
Serial.print("0"); // write 0
}
Serial.print(hour,0); // write hour value
Serial.print(":"); // write :
if(min<10){ // if it is less than 10 put a zero in front
Serial.print("0"); // write 0
}
Serial.print(min,0); // write minut value
Serial.print(":"); // write :
if(sec<10){ // if it is less than 10 put a zero in front
Serial.print("0"); // write 0
}
Serial.println(sec,0); // write second value
}
//end loop------------------------------------------------------------------------------------
}
(https://i.ibb.co/nQJfyKS/3bw454qerg3h5jhw4thg.jpg)
(https://i.ibb.co/48XD0qh/2134c32.png)
Liens vers l'app UART sur Google Play:
https://play.google.com/store/apps/details?id=de.kai_morich.serial_bluetooth_terminal (https://play.google.com/store/apps/details?id=de.kai_morich.serial_bluetooth_terminal)
(https://i.ibb.co/McTgTJ0/gfa4.png)
Liens code d'origine du vendeur Alix:
https://drive.google.com/file/d/1NcilRHzprBWcr5PLjt6fK2ES4uE-t7hE/view?spm=a2g0o.detail.1000023.1.16fd4bccqJ0NvX&usp=sharing (https://drive.google.com/file/d/1NcilRHzprBWcr5PLjt6fK2ES4uE-t7hE/view?spm=a2g0o.detail.1000023.1.16fd4bccqJ0NvX&usp=sharing)
Vendeur:
https://es.aliexpress.com/store/1962508?spm=a2g0o.detail.100005.1.16fd4bccqJ0NvX (https://es.aliexpress.com/store/1962508?spm=a2g0o.detail.100005.1.16fd4bccqJ0NvX)
Liens EB:
EB_#232 Mini-Plaquette - Sonde de Distance aux Ultrasons HC-SR04
https://youtu.be/QgEkTRFNrEs (https://youtu.be/QgEkTRFNrEs)
EB_#233 Arduino de Vincent, partie 3 - Réparation et Finalisation.
https://youtu.be/smB8AOa1hyU (https://youtu.be/smB8AOa1hyU)
EB_#231 Découverte - La Communication par Laser
https://youtu.be/wdt9-Xy4L0c (https://youtu.be/wdt9-Xy4L0c)
Lien dont j’ai été inspiré pour faire la commande reset:
https://arduinogetstarted.com/tutorials/arduino-serial-monitor (https://arduinogetstarted.com/tutorials/arduino-serial-monitor)