ตอนนี้ผมกำลังศึกษาโปรเจ็คเกี่ยวกับ โปรแกรมตรวจสอบลานจอดรถอยู่ครับ
ผมใช้ Arduino Mega 2560 กับ Ultrasonic Sensor Module HY-SRF05 ผมได้ทำการติดต่อผ่าน Serial Port มาเรียบร้อยแล้ว ได้ค่า cm ตามภาพ
ผมอยากสอบถามว่า จะเอาค่า cm ที่ได้มาใช้ยังไงครับ ผมอยากให้กำหนดประมาณว่า ถ้าระยะน้อยกว่า 50cm จาก P1 เป็นภาพ P1 ที่มีรถมาจอด
ตอนนี้ผมลองใช้เซ็นเซอร์ 1 ตัวก่อนนะครับ
Code ฝั่ง arduino
int pingPin = 13;
int inPin = 12;
void setup() {
Serial.begin(9600);
}
void loop()
{
long duration, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(inPin, INPUT);
duration = pulseIn(inPin, HIGH);
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(50);
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
Code ฝั่ง VB
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False
Dim lists As String() = IO.Ports.SerialPort.GetPortNames
For i = 0 To lists.Count - 1
ComboBox1.Items.Add(lists(i))
Next
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If ComboBox1.SelectedItem = "" Then Return
If SerialPort1.IsOpen Then SerialPort1.Close()
If Button1.Text = "Connect" Then
SerialPort1.BaudRate = 9600
SerialPort1.PortName = ComboBox1.SelectedItem
Try
SerialPort1.Open()
Button1.Text = "Disconnect"
ComboBox1.Enabled = False
Catch ex As Exception
MsgBox(ex.Message)
End Try
Else
Button1.Text = "Connect"
ComboBox1.Enabled = True
End If
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim line As String = SerialPort1.ReadLine()
Label1.Text = line
End Sub
Private Sub PictureBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
End Sub
End Class
ขอแนวทางจากพี่ๆช่วยแนะนำทีครับ ว่าผมควรจะทำยังไงบ้าง ขอบคุณครับ
แก้แค่โคดใน VB ใช้ if เช็ค แล้วเปลี่ยนรูปครับ ตัวอย่างโคดนี้
Picturebox1.Image = Image.FromFile("C:\mypicture.jpg")
ใส่ใน event เมื่อมีการรับข้อมูลจาก Serial
SerialPort1_DataReceived ตรงนี้ครับ
ขา OUT ให้สัญญาณ 1 เมื่อมีวิตตถุ น่าจะต่อกับ LED ได้ ขอมูลอ้างอิงตาม datasheet นี้ครับ
http://www.energiazero.org/arduino_sensori/Arduino%20ultrasonic%20sensor%20(HC-SR04%20or%20HY-SRF05).pdf
ส่งเป็น 1 ข้อความ 4 ตัว แล้วแยกด้วยเครื่องหมายจุลคา ','
เช่น 1,2,3,4
Dim s As String s = "Chilkat Mail, ActiveX Component, $99, free upgrades, 1-Year Support" Dim fields() As String ' Split the string at the comma characters and add each field to a ListBox fields() = Split(s, ",") For i = 0 To UBound(fields) List1.AddItem Trim$(fields(i)) Next
ตามที่แนะนำไปแล้ว ส่งไป 1 ข้อความแล้วตัดให้เป็น 4 ตัวแปร
ดูวิธีการตัด string ตามตัวอย่าง อีกครั้งก่อนนะครับ