MAXBOTIX 7066_SERIAL comunication_MEGA2560

Hi folks!
I’m working for about 1day around with the MAXBOTIX 7066, to get an effective serial communication with Arduino(uno/mega) but so far I lost… with it. Does anyone get this done yet?

/*

  • Aim
  • Connect(wiring) and read maxbotix ultrasonic sensor, via serial RS232
  • Set-up
  • Mega 2560
  • MAXbotix 7066
  • Blue wire connected on pin 18 of aduino mega
  • Red wire connected on 5V of aduino mega
  • Blackwire connected on GND of aduino mega
 *References
 *Main code based on: https://forum.arduino.cc/?topic=130842#msg3856789
 *
 *other references
https://www.maxbotix.com/articles/mb7389-x-arduino-tutorial-with-code-examples.htm
https://www.maxbotix.com/Arduino-Ultrasonic-Sensors-085/
https://www.arduino.cc/en/Tutorial/LibraryExamples/TwoPortReceive
https://arduino.stackexchange.com/questions/7976/decoding-a-rs232-message-in-arduino
http://www.arduino.cc/en/Tutorial/DigitalReadSerial

 */


 int current_range_reading = -99;
#include <SoftwareSerial.h>
SoftwareSerial mySerial1(19, 18, true); // RX, TX

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(57600);
  mySerial1.begin(9600); //2nd port conected to the US
  }

// the loop routine runs over and over again forever:
void loop() {
 if (Serial.available()) {      // If anything comes in Serial (USB),
    mySerial1.write(Serial.read());   // read it and send it out Serial #1(pins 0 & 1)
  }

  if (mySerial1.available()) {     // If anything comes in Serial #1 (pins 0 & 1)
    Serial.write(Serial1.read());   // read it and send it out Serial (USB)
  }



  
  // read the input pin:
  // print out the state of the button:
  Serial.println();
  delay(1);        // delay in between reads for stability
  int inByte = mySerial1.read(); // Examine the first stored character and decide what to do.
  if (inByte == 'R')
  {
    int thousands = (mySerial1.read() - '0') * 1000; // Take and convert each range digit to human-readable integer format.
    int hundreds = (mySerial1.read() - '0') * 100;
    int tens = (mySerial1.read() - '0') * 10;
    int units = (mySerial1.read() - '0') * 1;
    int cr = mySerial1.read(); // Don't do anything with this, just clear it out of the buffer with the rest.

    // Assemble the digits into the range integer.
    int current_range_reading = thousands + hundreds + tens + units;
    if (current_range_reading <= 300) //This is the minimum reading for the HRXL MaxSonar WR 7380, not the actual distance
      Serial.println("too close!");
    else if (current_range_reading >= 5000) //This is the max reading for the HRXL MaxSonar WR 7380, not the actual distance
      Serial.println("too far!");
    else
    {
      Serial.print("Range (mm): ");
      Serial.println(current_range_reading);
    }
  }
  else if ( inByte == "\r" ) // Carriage Return  character, oops!
  {
    //Serial.println();
  }
  else if ( inByte == -1 ) // Just in case!  This shouldn't happen if Serial1.available() returns true.
  {
    Serial.println("RX buffer empty, wth?");
    return;
  }
}

Hi folks, finally I found the solution!

Using SoftwareSerial. h in the UNO or MEGA
on the line SoftwareSerial mySerial(10, 11, true); it must be set to true:

Where it goes the code:

/*
  Software serial code for parsing serial data from a MaxBotix sensor.
  Based heavily on examples by Tom Igoe and Mikal Hart.

  Receives from the hardware serial, sends to software serial.
  Receives from software serial, sends to hardware serial.

  The circuit:
   V+ (connect to sensor pin 6, may require external power supply)
   GND (connect to sensor pin 7, may require external power supply)
   RX is digital pin 10 on Arduino (connect to sensor pin 5, TX)

  If triggered ranging is desired:
  TX is digital pin 11 on Arduino (connect to sensor pin 4, RX)

  Note:
  We recommend a common ground if using external power supply.

  Triggered ranging requires the code to be edited to send a trigger
  pulse to the RX pin of the sensor. However, this can be done
  more easily by setting a digital pin high and then low.

  Not all pins on the Mega and Mega 2560 support change interrupts,
  so only the following can be used for RX:
  10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69

  Not all pins on the Leonardo and Micro support change interrupts,
  so only the following can be used for RX:
  8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).

  created back in the mists of time
  modified 26 August 2019
  by Cody Carlson
  based on examples by Tom Igoe and Mikal Hart's example
  Tested and written for use on the Arduino Uno
  This example code is in the public domain.
*/
//da leitura R0022 o sensor deixa de fazer boas leituras!

#include <SoftwareSerial.h>
// RX, TX, Invert Levels (true = RS232, false = TTL)
SoftwareSerial mySerial(10, 11, true); //IT MUST BE SET AT TRUE
char c;
void setup() {
  // Open serial communications and wait for port to open:
  // Set the data rate for the serial terminal
  Serial.begin(9600);
  while (!Serial) {
    ; // Wait to connect serial port. For native USB port only
  }
  // set the data rate for the serial data from the sensor
  mySerial.begin(9600);
}

void loop() { // run over and over
  if (mySerial.available()) {
    c = mySerial.read();
    if (c == 13) {
      // looks for a carriage return and sends a line end
      Serial.println();
    }
    Serial.write(c);
  }
  }

software_serial_uno.ino (2.35 KB)