Closed bilbolodz closed 6 years ago
I don't know this sensor but the link I found below suggests this only supports ping/echo mode (like the basic HC-SR04).
Are you sure it supports serial mode?
According these PDF: https://www.google.pl/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0ahUKEwiu1eSxsNTWAhUCaFAKHdkkDmIQFggqMAA&url=https%3A%2F%2Fwww.jahankitshop.com%2Fgetattach.aspx%3Fid%3D4635%26Type%3DProduct&usg=AOvVaw0v_VKu_EYJoKstjsEl-ct8 it has serial mode. I've v2.0 version.
It looks like it can operate in three modes (ping/echo, constant serial, serial on request). It lacks the temperature compensation of the US-100 so you'll lose some accuracy there, but serial mode should make it far more accurate/precise by avoiding the latency issues.
However, the manufacturers of this have gone with a slightly more complex protocol - which I can imagine would be helpful in the continuous mode but is less obviously required in the on-demand mode.
It wouldn't be rocket science to add support for this, but without a JSN-SR04T it would be impossible to test. Feel free to make the changes and send a pull request though - it shouldn't be much more than adding a new Init parameter to show which protocol is being used (US-100, JSN-SR04T, etc.) and then the receive handling needs to decide whether it wants 2 or 4 bytes and we should check the start byte/checksum as they are there.
I know it but so far I'm not able to put it "into serial mode", that I've asked about any experience with these module. Tonight I will connect logic analyzer and I will try to check "raw TTL signals". I will keep you updated for any progress.
Working code (standalone code not library mode) for "Mode 2" (R27 47k resistor):
#include <SoftwareSerial.h>
#define receiveTimeout 90 //Dane wysylane są co 100ms
SoftwareSerial mySerial(2, 3); // RX, TX
static boolean dataReady = false;
static boolean dataTimeout = true;
static byte ndx = 0;
unsigned int distance;
byte buforek[4];
unsigned long lastReceiveChar;
void readDistance();
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Start!");
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
}
void loop() { // run over and over
readDistance();
if (dataReady) {
Serial.print("Distance: ");
Serial.println(distance);
}
}
void readDistance() {
if (lastReceiveChar + receiveTimeout < millis ())
{
dataTimeout = true;
}
if (mySerial.available() > 0 )
{
if (dataTimeout == true) {
ndx = 0;
dataTimeout = false;
}
buforek[ndx] = (byte) mySerial.read();
lastReceiveChar = millis();
ndx++;
if (ndx == 4) {
ndx = 0;
if (buforek[0] == 0xff && ((buforek[0] + buforek[1] + buforek[2]) & 0x00ff) == buforek[3])
{
distance = buforek[1] * 256 + buforek[2];
dataReady = true;
}
}
}
}
I'll close this for now - your code will be there if anyone wants it, but until someone has time to make a full fix and the equipment to test it we can't fix it.
Actually it's not a library issue but: I'm trying to run these sensor into serial mode but without any success. Any comments/remarks are welcome.