基礎篇-使用bDesigner
課程內容
課程內容
bDesigner為桃園八德國小蔡佳倫老師的作品
VB控制項
VB控制項
- SerialPort (元件)
- Button (通用控制項)
- Textbox (通用控制項)
- Label (通用控制項)
VB SerialPort屬性設定
VB SerialPort屬性設定
SerialPort1.Close()
SerialPort1.PortName = "com4" 'change com port to match your Arduino port
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
數位輸出
數位輸出
以Pin 13為例
SerialPort1.Open()
SerialPort1.Write("pinMode=13, 1" & vbNewLine)
SerialPort1.Write("digitalWrite=13, 1" & vbNewLine)
SerialPort1.Close()
數位輸入
數位輸入
以Pin 12為例
SerialPort1.Open()
SerialPort1.Write("pinMode=12, 0" & vbNewLine)
SerialPort1.Write("digitalRead=12" & vbNewLine)
TextBox1.Text = SerialPort1.ReadLine()
SerialPort1.Close()
類比輸出(PWM)
類比輸出(PWM)
以Pin 9為例
SerialPort1.Open()
SerialPort1.Write("pinMode=9, 1" & vbNewLine)
SerialPort1.Write("analogWrite=9, 255" & vbNewLine)
SerialPort1.Close()
類比輸出
類比輸出
以Pin A0為例
SerialPort1.Open()
SerialPort1.Write("pinMode=A0, 0" & vbNewLine)
SerialPort1.Write("analogRead=A0" & vbNewLine)
TextBox2.Text = SerialPort1.ReadLine()
SerialPort1.Close()
程式碼
程式碼
Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
'digital HIGH is 1
'digital LOW is 0
'pin mode INPUT is 0
'pin mode OUTPUT is 1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.Close()
SerialPort1.PortName = "com4" 'change com port to match your Arduino port
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SerialPort1.Open()
SerialPort1.Write("pinMode=13, 1" & vbNewLine)
SerialPort1.Write("digitalWrite=13, 1" & vbNewLine)
SerialPort1.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SerialPort1.Open()
SerialPort1.Write("digitalWrite=13, 0" & vbNewLine)
SerialPort1.Close()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
SerialPort1.Open()
SerialPort1.Write("pinMode=12, 0" & vbNewLine)
SerialPort1.Write("digitalRead=12" & vbNewLine)
TextBox1.Text = SerialPort1.ReadLine()
SerialPort1.Close()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
SerialPort1.Open()
SerialPort1.Write("pinMode=9, 1" & vbNewLine)
For i = 0 To 250 Step 10
SerialPort1.Write("analogWrite=9," & i & vbNewLine)
Label1.Text = i
'Threading.Thread.Sleep(500)
delay(500)
Next i
For i = 250 To 0 Step -10
SerialPort1.Write("analogWrite=9," & i & vbNewLine)
Label1.Text = i
'Threading.Thread.Sleep(500)
delay(500)
Next i
SerialPort1.Close()
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
SerialPort1.Open()
SerialPort1.Write("pinMode=A0, 0" & vbNewLine)
SerialPort1.Write("analogRead=A0" & vbNewLine)
TextBox2.Text = SerialPort1.ReadLine()
SerialPort1.Close()
End Sub
Sub delay(ByRef TimeLast As Integer)
Dim Start As Integer = Environment.TickCount()
'Dim TimeLast As Integer = 500 ' 要延遲 t 秒,就設為 t *1000
Do
If Environment.TickCount() - Start > TimeLast Then Exit Do
Application.DoEvents() ' 要記得寫這行,不然都在跑迴圈,畫面可能會不見
Loop
End Sub
End Class
執行畫面
執行畫面