Arduino Controlled dual axis Solar Tracker VB Code

Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class main
    Dim timestr As String
    Dim datestr As String
    Dim myday As Int32
    'Dim index, j As Int16
    Dim hourser, minser, dayser, monthser, yearser, tzser As Int16
    Dim latser, longser As Decimal
    Dim latserint, latserdec, longserint, longserdec As Int16
    Dim hourstr, minstr, daystr, monthstr, yearstr, latintstr, latdecstr, longintstr, longdecstr As String
    Dim stringtosend As String
    Dim buffer(10) As Byte
    Private Sub main_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        If SerialPort1.IsOpen Then SerialPort1.Close()
    End Sub


    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        If SerialPort1.IsOpen Then SerialPort1.Close()
        SerialPort1.PortName = "com18" '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!
        Timer1.Interval = 10
        Timer1.Start()
        TextBox1.Text = "STOP"
        TextBox1.BackColor = Color.Red
        TextBox1.ForeColor = Color.Black
        TextBox2.Text = "STOP"
        TextBox2.BackColor = Color.Red
        TextBox2.ForeColor = Color.Black
        RectangleShape1.FillColor = Color.Red

    End Sub
    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        Dim inval, result As String
        Dim elevint, azimint, move, mode, limitaz, limitel, minstomove As Int16
        Dim sunset As Boolean = False
    

        inval = ""
        result = ""
       
        If Not SerialPort1.IsOpen Then SerialPort1.Open()

        If SerialPort1.BytesToRead > 0 Then
            inval = SerialPort1.ReadLine()
            If inval = "ACK" & vbCr Then
                RectangleShape1.FillColor = Color.Green

                'TextBox7.Text = ""
                'TextBox8.Text = ""
                inval = SerialPort1.ReadLine()
                TextBox7.Text = inval

                inval = SerialPort1.ReadLine()
                elevint = CInt(inval)
                If elevint > 0 Then
                    result = "Elevation = " & inval
                    ' inval = SerialPort1.ReadLine()
                    ' result = result & "      Zenith = " & inval
                    inval = SerialPort1.ReadLine()
                    azimint = CInt(inval)
                    result = result & "      Azimuth = " & inval
                    TextBox8.Text = result
                    Me.CreateGraphics.FillRectangle(Brushes.DarkRed, 25 + ((azimint - 50) * 2), 380 - (elevint * 2), 4, 4)

                End If
                If (elevint <= 0) Then
                    TextBox8.Text = "The sun is set......go to sleep"
                    inval = SerialPort1.ReadLine()
                    'inval = SerialPort1.ReadLine()
                End If

                inval = SerialPort1.ReadLine()
                'angleelev = CInt(inval)
                TextBox6.Text = inval 'angleelev
                inval = SerialPort1.ReadLine()
                'angleazim = CInt(inval)
                TextBox9.Text = inval 'angleazim
                inval = SerialPort1.ReadLine()
                limitel = CInt(inval)
                Select Case limitel
                    Case 2
                        TextBox5.Text = "Limit UP"
                        TextBox5.BackColor = Color.Red
                        TextBox5.ForeColor = Color.Black
                    Case 1
                        TextBox5.Text = "Limit DOWN"
                        TextBox5.BackColor = Color.Red
                        TextBox5.ForeColor = Color.Black
                    Case 0
                        TextBox5.Text = "OK"
                        TextBox5.BackColor = Color.Green
                        TextBox5.ForeColor = Color.Yellow
                End Select
                inval = SerialPort1.ReadLine()
                limitaz = CInt(inval)
                Select Case limitaz
                    Case 2
                        TextBox4.Text = "Limit EAST"
                        TextBox4.BackColor = Color.Red
                        TextBox4.ForeColor = Color.Black
                    Case 1
                        TextBox4.Text = "Limit WEST"
                        TextBox4.BackColor = Color.Red
                        TextBox4.ForeColor = Color.Black
                    Case 0
                        TextBox4.Text = "OK"
                        TextBox4.BackColor = Color.Green
                        TextBox4.ForeColor = Color.Yellow
                End Select
                inval = SerialPort1.ReadLine()
                mode = CInt(inval)
                Select Case mode
                    Case 0
                        TextBox3.Text = "Manual Mode"
                    Case 1
                        TextBox3.Text = "Tracking Sun"
                    Case 2
                        TextBox3.Text = "Calibrating"
                    Case 3
                        TextBox3.Text = "Sleeping..."
                End Select
                inval = SerialPort1.ReadLine()
                move = CInt(inval)
                Select Case move
                    Case 8
                        TextBox1.Text = "UP"
                        TextBox1.BackColor = Color.Green
                        TextBox1.ForeColor = Color.Yellow
                    Case 4
                        TextBox1.Text = "DOWN"
                        TextBox1.BackColor = Color.Blue
                        TextBox1.ForeColor = Color.Yellow
                    Case 2
                        TextBox2.Text = "EAST"
                        TextBox2.BackColor = Color.Green
                        TextBox2.ForeColor = Color.Yellow
                    Case 1
                        TextBox2.Text = "WEST"
                        TextBox2.BackColor = Color.Blue
                        TextBox2.ForeColor = Color.Yellow
                    Case 0
                        TextBox1.Text = "STOP"
                        TextBox1.BackColor = Color.Red
                        TextBox1.ForeColor = Color.Black
                        TextBox2.Text = "STOP"
                        TextBox2.BackColor = Color.Red
                        TextBox2.ForeColor = Color.Black
                End Select
                inval = SerialPort1.ReadLine()
                minstomove = CInt(inval)
                TextBox10.Text = minstomove
            End If
            RectangleShape1.FillColor = Color.Red
            SerialPort1.Close()
        End If

    End Sub

    Private Sub main_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        e.Graphics.DrawRectangle(Pens.Black, 25, 200, 520, 180)

    End Sub

End Class

No comments:

Post a Comment