Hi all! I'm kinda new to Arduino and a total noob on serial data. I bought a MaxSonar(MB7369) and im trying to use its Pin 5 (RS232 output) to measure the distance. I used a RS232 to TTL converter (Max232) to be able to read the data. When I connect it to an Arduino and upload a blank code, the serial monitor shows the R-Distance Values continuously. However, if I input a code, the serial monitor outputs no data (at random instances it does but just once) :
/* test function to Parse data from MaxSonar serial data
and return integer */
#include <SoftwareSerial.h>
#define txPin 0 //define pins used for software serial for sonar
#define rxPin 1
SoftwareSerial sonarSerial(rxPin, txPin, false); //define serial port for recieving data, output from maxSonar is inverted requiring true to be set.
boolean stringComplete = false;
void setup()
{
Serial.begin(9600); //start serial port for display
sonarSerial.begin(9600); //start serial port for maxSonar
//delay(500); //wait for everything to initialize
}
void loop()
{
int range = EZread();
if(stringComplete)
{
stringComplete = false; //reset sringComplete ready for next reading
//delay(500); //delay for debugging
}
}
int EZread()
{
int result;
char inData[6]; //char array to read data into
int index = 0;
sonarSerial.flush(); // Clear cache ready for next reading
while (stringComplete == false) {
//Serial.print("reading "); //debug line
if (sonarSerial.available())
{
char rByte = sonarSerial.read(); //read serial input for "R" to mark start of data
if(rByte == 'R')
{
Serial.println("rByte set");
while (index < 5) //read next 4 characters for range from sensor
{
if (sonarSerial.available())
{
inData[index] = sonarSerial.read();
Serial.println(inData[index]); //Debug line
index++; // Increment where to write next
}
}
inData[index] = 0x00; //add a padding byte at end for atoi() function
}
rByte = 0; //reset the rByte ready for next reading
index = 0; // Reset index ready for next reading
stringComplete = true; // Set completion of read to true
result = atoi(inData); // Changes string data into an integer for use
}
}
return result;
}
(code from Piano_Man)How is this possible? If anyone could help me with the code to read the data It would be very helpful!
Thanks in advance!