hello everyone
so, i'm making a project using 2 ultrasonic sensors and each sensor detects an object within a range 4cm and i want to display it on visual basic. if i hit the button then the first ultrasonic sensor detects an object and the label1 is displaying "There is an Object" and vice versa/"there is nothing". if the second ultrasonic sensor detects an object then the label2 is displaying"There is an Object" and so on. here's my arduino code,but it only works for the first sensor(the serial monitor only read first ultrasonic),please help me to correct this source code:
#define trigPin1 7
#define echoPin1 6
#define trigPin2 2
#define echoPin2 1
void setup() {
Serial.begin (9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
;
}
void loop() {
long duration1,duration2, distance1,distance2;
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(2);
digitalWrite(trigPin2,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
digitalWrite(trigPin2, LOW);
duration1 = pulseIn(echoPin1, HIGH);
duration2 = pulseIn(echoPin2 ,HIGH);
distance1 = (duration1/2) / 29.1;
distance2 = (duration2/2) /29.1;
if (distance1 < 4)
{
Serial.println("There is an object");
}
else {
Serial.println("There is Nothing");
}
delay(500);
if (distance2 < 4) {
{
Serial.println("There is an Object");
}
else {
Serial.println("There is nothing");
}
delay(500);
}
and my visual basic code:
Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
Shared _continue As Boolean
Shared _serialPort As SerialPort
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.Close()
SerialPort1.PortName = "com7"
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SerialPort1.Open()
Dim ArduinoText As String
If SerialPort1.IsOpen Then
Try
ArduinoText = SerialPort1.ReadLine()
Label1.Text = ArduinoText
Catch ex As Exception
MessageBox.Show("Error has occured")
End Try
End If
SerialPort1.Close()
End Sub
End Class
i dont know how to program the label2 and how do i write it? ReadLine(2) or ReadLine2() because i'm a begineer. so what i expected when i hit the button1 the label1 will displaying the data from ultrasonic1 when it detects an object or not and label2 displaying the data from ultrasonic2 as well, i hope you understand me , i know my english is bad but please help me ![]()