Hai There,
Recently I bought this A01NYUB Waterproof Ultrasonic Sensor.
I got the details of the sensor from the same website itself. Actually, I wanted to print the distance in the serial monitor of Arduino (In inch). So I modified the code a little bit to print in inches and it is working without any problem.
But my problem is that I want to add one more sensor (The same sensor) but measuring a different distance. And display both the distance in the serial monitor like Sensor1-__ , Sensor2-__ . I tried it by myself but there was a lot of error and both the distances were wrong... Can somebody modify the code for me, please... I only know the basics...
The original code can be found on the website which I mentioned before. The below code is the one that I modified.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(11,10);
unsigned char S1[4]={};// I am not sure what is the use of the array in this code.
int i,SensorMM,sum,SensorCM,Inch,Approx;
void setup()
{
Serial.begin(57600);
mySerial.begin(9600);
}
void loop()
{
do{
for(i=0;i<4;i++)
{
S1[i]= mySerial.read();
}
}while(mySerial.read()==0xff);
mySerial.flush();
if(S1[0]==0xff)
{
sum=(S1[0]+S1[1]+S1[2])&0x00FF;
if(sum==S1[3])
{
SensorMM=(S1[1]<<8)+S1[2];
if(SensorMM>280)
{
SensorCM=SensorMM/10;
Inch=SensorCM/2.54;//I want the output in Inch in the serial monitor.
Approx = (int)(Inch+0.5);// I just want a single-digit value (without and decimal) in the serial monitor. Here I am rounding the value to the nearest one.
Serial.print(Approx);
Serial.print("Inch");
delay(100);
}else
{
Serial.println("Too Close");
}
}else Serial.println("ERROR");
}
delay(150);
}
Thanks for the reply.
This is my new account. For this long, I've been using my parent's email address and all. So I wanted to create an account using my credentials.
I am not a programmer. I just know the basics. I can understand simple codes which are not too complicated but I cannot write any programs. I am just learning to program.
The original demo code is in the link that I mentioned.
There is nothing much in the serial monitor when running the demo code. It just displays the distance (distance=___cm).
When running the modified program, I get the distance in Inch (___Inch).
My problem I that, I don't know how to add one more sensor to the code. ie: How to define the pins of the second sensor?
How to differentiate between the two sensors in the code.
I tried to add the second sensor in the code but there are lots of errors and I completely messed up with the curly brackets which I think is the reason for the errors.
Also can you explain why are we using array in the program? This sensor is the same as HC-SR04 Ultrasonic Sensor. But the program is entirely different.
You are talking to your sensor using SoftwareSerial on pins 11 and 10. You will need to create another instance of SoftwareSerial using different pins and a different name.
You should also google how to use two instances of SoftwareSerial since you can not just simply use both of them. You have to select 1, read sensor 1 and then select the other one and read that.
No. As blh64 said there is a different technique for using two software serial ports. You could have easily found examples on the same sight this forum is on.
I haven't developed the sensor. This sensor could behave in thousands of ways. So I don't know nothing about the behaviour of the sensor.
Posting the original serial output 1 : 1 that you get from the original demo-code - even if it is only very very few characters what gets printed gives information about the behaviour of the sensor. And this is important.