Hi. first post so here goes.
I bought this DFrobot SEN0313 UART ultrasonic sensor to measure water levels. The demo program from the DFRobot site works but has a sampling frequency of approx 100ms. I want to stretch it out to approx 5 mins using a Arduino Uno. I then plan to upload the distance via Lora in JSON.
I need some help to mod this code below to read the sensor every 5 mins.
ANy help with it would be greatly appreciated.
It’s been a while since I’ve done anything in Arduino (Micropython spoils you ).
It’s a very quick any dirty implementation, to get more accurate timings and if you want to make use of any low power modes I’d consider using an RTC and an interrupt on the Arduino!
#include <SoftwareSerial.h>
SoftwareSerial mySerial(11,10); // RX, TX
unsigned char data[4]={};
float distance;
uint16_t counter = 0; // Create a counter
void setup()
{
Serial.begin(57600);
mySerial.begin(9600);
}
void loop()
{
do{
for(int i=0;i<4;i++)
{
data[i]=mySerial.read();
}
}while(mySerial.read()==0xff);
mySerial.flush();
if(counter >= 600){ // Counts up to 500 seconds
if(data[0]==0xff)
{
int sum;
sum=(data[0]+data[1]+data[2])&0x00FF;
if(sum==data[3])
{
distance=(data[1]<<8)+data[2];
if(distance>280)
{
Serial.print("distance=");
Serial.print(distance/10);
Serial.println("cm");
}else
{
Serial.println("Below the lower limit");
}
}else Serial.println("ERROR");
}
counter = 0
}
delay(1000); // Delays the loop by 1 second
counter = counter + 1 //Iterates the counter
}
PS: if you put ``` ``` around your code you can make it look like the language it was written in.
Thanks for your feedback and tip about the "’ "’ Liam.
I gave it a try but now im just getting ERROR in the serial monitor.
I think there is something inherent in the UART operation of this sensor. Its specs state is should responds in 100-150ms. I suspect the instruction to fill the array in the code is not timed correctly when a delay is used.
I tried the code in a sub-routine program instruction which was called once every minute and it looked like it never got any data from the sensor.
Im at a bit of a loss which is a shame, it seems to be a good, accurate and repeatable sensor suited for outdoor IOT projects like water level sensing.
If you change count to 6000 and the delay to 100 it should work, the serial buffer might not roll over and just fill up then the Arduino doesn’t know what to do.There’s definitely a way to do what you’re after