Hi Oliver,
Great code! We’ve tested this on an Arduino Mega, and it worked beautifully after changing the software RX and TX pins to 10 and 11 respectively:
Everyone else should also keep in mind that software serial only works on certain pins depending on your board.
If your board has hardware serial, this sketch should work as well:
char setAddress[5] = {0xFA,0x04,0x01,0x80,0x81};
char measureCont[5] = {0x80,0x06,0x03,0x77};
char bin[4] = {};
void setup(){
Serial.begin(115200); //Open hardware serial connected via USB
while(!Serial){
delay(50);
}
Serial.println("USB Serial opened"); //Announce yourself to PC
Serial1.begin(9600);
Serial1.write(setAddress);
while(Serial1.available()<4){
delay(50);
}
Serial1.readBytes(bin, 4)
Serial1.write(measureCont);
}
void loop(){
if (Serial1.available()>=11){
char response[11] = {};
Serial1.readBytes(response, 11);
for(int i = 3; i <= 9; i++){
Serial.print(response[i]);
}
Serial.println(" Metres");
}
}
Please note that The RX pin of your microcontroller should be plugged into the yellow line of the sensor, and TX to white. We’ll update the product page for this as soon as we can, as the documented opposite wiring will leave you very frustrated!
If you need help deciphering the serial message reference, let us know, as we had to work with it a lot during testing.
Keen to see what you make with this once you’ve got it going!
James