Forum >Replies by Stan.Howe
userhead Stan.Howe
Replies (6)
  • You Reply:

    had to paste the working code in three separate comments;

     

    /**************************************************************************************************************

    *This code was originally written to test the range finder function of the URM08-RS485 Waterproof Sonar Range Finder

    the original author : roker.wang@dfrobot.com, date   : 11.09.2017;

     

    Code was Modified by SFH 8/15/23 to work on an arduino UNO R3 , using a RS485 shield and DFRobot SEN0492 laser sensor being

    used to detect water level and calculate distance to water(gap). The readings are evaluated statistically averaging data and the result is

    calculated and displayed in waterlevel in inches and gallons. simular code was succesfully used with SEN0246 ultrasonic sensor

     

    RS-485 shield https://www.dfrobot.com/product-1024.html

    LCD shield https://www.robotshop.com/products/cytron-lcd-keypad-shield-arduino

    SEN0492- laser sensor https://www.dfrobot.com/search-sen0492.html

    external wiring via Screw shield https://www.robotshop.com/products/dfrobot-proto-screw-shield-assembled

    library quickstats that is used to evaluate the data https://github.com/dndubins/QuickStats

     

    **************************************************************************************************************/

    //parameters to communicate via RS485 to the sensor

    #define address     0x50 //Slave Address

    #define function    0x03 //funtion Code

    #define register_H  0x00 //Register Address High bit

    #define register_L  0x34 //Register Address Low bit

    #define length_H    0x00 //Read Length high bit

    #define length_L    0x01 //Read Length low bit

    #define CRC_H       0xC8 //CRC check High bit

    #define CRC_L       0x45 //CRC check low bit

    unsigned char CMD[8]={address,function,register_H,register_L,length_H,length_L,CRC_H,CRC_L}; //declare an array with 8 elements(plus null) to store the Distance measurement commands

     

    //varible and array to recieve data from sensor

    unsigned int  Distance=0;  // delare and set varible Distance to 0 mm at start

    unsigned char Rx_DATA[8]; //declare char Rx_Data array with 8 elements (plus one for the null)

     

    //programming delays

    int dt=30; //a delay period before taking the next measurement

    int setTime=100; // a delay used to allow serial coms to iniyialize

    int wait = 200; // a delay to allow serial data to be returned

     

    //median sorting parameters

    #include "QuickStats.h" //the library needed for statistical calculations

    int const numReadings = 5; //set the number of readings to include for statistical analysis

    float med [numReadings]; //create an array to store 'numReadings' measurements that can then be sorted and use only the median data

    QuickStats stats; //initialize an instance of this class

  • You Reply:

    //varibles for water calulations

    float convert = 25.4; //conversion factor from mm to inches

    float measure;  //a float varible to store the calculated distance

    int calibrate = 1.0;// a caibration factor that can be used to fine tune the readings for accuracy

    long gallons; //declares the float varible gallons to store calculated gallons in the tank

    long waterLevel; //declares waterLevel varible to store the calculated water level

    int tankDepth = 104; //declares total tank depth varible

    float gallonsPerInch = 202.38;  //declares gallons per inch varible

     

    #include <LiquidCrystal.h> //invokes the liquidCrystal library needed for the LCD display

    int d4=4; //declares varible d4 (LCD d4 pin) to Arduino pin 4

    int d5=5; //declares varible d5 (LCD d5 pin) to Arduino pin 5

    int d6=6; //declares varible d6 (LCD d6 pin) to Arduino pin 6

    int d7=7; //declares varible d7 (LCD d7 pin) to Arduino pin 7

    int rs=8; //declares varible rs (LCD rs pin) to Arduino pin 8

    int en=9; //declares varible en (LCD en pin) to Arduino pin 9

    LiquidCrystal lcd (rs,en,d4,d5,d6,d7); //sets up LCD pins using the varibles declared above

  • You Reply:

    void setup() {

     

     Serial.begin(115200);  //Communicate with module via Serial, set baud rate to 115200

     delay(setTime); //delay to allow serial coms to establish

     lcd.begin(16,2); // starts the LCD, specifies columns and rows on the LCD

    } //end of setup

     

    void loop() {

     int x = 0;  // Declare and set varible x to 0

     int i = 0;  // Declare and set varible i to 0 at start(changed from unsigned char)

     

     for (x=0;x<numReadings;x++){ //cycles through the "for" loop "numReading" times to store data in the median array for averaging

        delay (wait); //wait for silent interval before sending request

     

       //TX sub-"for" loop

       for(i=0;i<8;i++){ // cycles though the for loop 8 times to send all 8 portions of the command string

        Serial.write(CMD[i]); //sends each portion of the command string from the array to the sensor to return distance

        }//end of transmit "for" loop

     

       // RX data

       delay(wait);  //Wait for the end of distance measurement

       i=0; //resets i to 0

     

       while (Serial.available()){  //Read return data package

         Rx_DATA[i++]=(Serial.read()); //reads each of the 8 packets and stores them in the Rx_Data array

        } // end of recieve while loop

  • You Reply:

       Distance=((Rx_DATA[3]<<8)|Rx_DATA[4]); //Get the distance data,(reads element 4(3), then shifts it left by 8(multiplies by 256),

        // then does a bitwise OR (it is really addition) with element 5(4))

     

       med[x] = Distance; //store each distance measuement in the 'med' array

     

      } //end of median array "for" loop  

     

     Distance = (stats.average(med,numReadings)); //takes the average of 'numReading' in 'med' array and stores it in the distance varible

     

     //calculations of water level gallons etc.

     measure = (Distance/convert)+calibrate; //convert mm distance to inches and calibrate the probe

     int inch = round(measure); //convert Float to integer and round it off for display on LCD

     waterLevel = tankDepth-inch; //calcualtes water level

     gallons= waterLevel*gallonsPerInch; //calculates the total gallons

     int level = int(waterLevel); //convert long to integer

     int gal = int(gallons); //convert Float to integer

     

     //print results to LCD screen

     lcd.clear(); //clears the LCD screen

     lcd.setCursor(0,0); // sets cursor to top left of the screen

     lcd.print(level); //prints the calculated inches of water

     lcd.print(" In of "); // prints text listed

     lcd.print(tankDepth); //prints the total tank depth

     

     lcd.setCursor(0,1); // sets cursor to bottom left of the screen

     lcd.print (gal);// prints the calculated gallons varible

     lcd.print(" G [");  // prints text listed

     lcd.print(inch); //prints the measured distance to water

     lcd.print("]gap");  // prints text listed

     

     delay(dt);  //a delay period to allow last items displayed on LCD to be viewed without flicker

     

    }

  • You Reply:

    SOLVED

    I had the wrong baud rate for this sensor. Have also calibrated my sensor based on testing (your mileage may vary). it wouldnt let me upload the new code- too many characters, ill try under a comment or two

     

     

     

    /

  • You Reply:

    I had avoided those libraries in my sketch because they say they are not compatible with the Uno. But I gave your sketch a try anyway, and just a blank screen on the LCD. 

     

    thank you for trying