Author Topic: Scaling for Bar level  (Read 1535 times)

chunt

  • Newbie
  • *
  • Posts: 14
    • View Profile
Scaling for Bar level
« on: June 17, 2014, 10:26:30 AM »
Hey everyone, here is my question/Issue I am using a USB enabled load cell to measure the amount of liquid in a small reservoir. I am able to read the value just fine inside the textbox.  What I want to do is use a bar level as a level indicator for the reservoir using the textbox value. I will be using the load cell data inside the PLC but I don't wan to send the data to the PLC and then send it right back for the bar level value. I would like to do this with code. Is this easily done via VB code or should I send the value to the PLC and then send it back to the bar level?  Any help would be great. Thanks.

chunt

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Scaling for Bar level
« Reply #1 on: June 17, 2014, 03:33:34 PM »
I worked it out. Had to do some some math functions and conversions. Thanks.

Noe

  • Full Member
  • ***
  • Posts: 205
    • View Profile
Re: Scaling for Bar level
« Reply #2 on: June 18, 2014, 05:13:59 PM »
If you could share your solution it would be a nice thing.

chunt

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Scaling for Bar level
« Reply #3 on: June 19, 2014, 09:08:31 AM »
Here is the code that I used for my USB load cell application. What I am doing is using a load cell to measure the amount of liquid in a reservoir. The basic program has a Text box that displays the raw value of the load cell. The first button starts the calibration, then when the load cell is at the minimum weight or empty the "set minimum weight" button is pressed. This then sets the bar level value to minimum or 0%. The maximum weight is then placed onto the load cell and the "set maximum weight" button is pressed and this will set the bar level to maximum or 100%. After the max button is pressed the math is completed and you have the converted value of the raw value to properly fill the bar graph. I also created settings so that when the form loads the last calibration information is loaded and the user does not have to re-calibrate at each form load.


Public Class Form1

    Dim WithEvents LoadCell1 As Phidgets.Bridge
    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.

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

        value1 = My.Settings.Value1
        value1Coor = My.Settings.Value1Coor
        value2 = My.Settings.Value2
        value2Coor = My.Settings.Value2Coor

       
            slope = (value2Coor - value1Coor) / (value2 - value1)
            ' slope = (100 - 0) / (value2 - value1)
            yInt = value1Coor - (value1 * slope)


        Try
            LoadCell1 = New Phidgets.Bridge
            LoadCell1.open()
            Threading.Thread.Sleep(1000)
            Timer1.Start()
            LoadCell1.bridges(0).Gain = Phidgets.BridgeInput.Gains.GAIN_128
            LoadCell1.DataRate = 350
            LoadCell1.bridges(0).Enabled = True



        Catch ex As Phidgets.PhidgetException
            MessageBox.Show(ex.ToString())
        End Try

        LoadCell1Data.Text = LoadCell1.bridges(0).BridgeValue.ToString

    End Sub

   

    Private Sub LoadCell1_BridgeData(ByVal sender As Object, ByVal e As Phidgets.Events.BridgeDataEventArgs) Handles LoadCell1.BridgeData

        TextBox4.Text = Math.Round((slope * e.Value) + yInt, 2).ToString
        BarLevel1.Value = Math.Round((slope * e.Value) + yInt, 2).ToString

    End Sub
   

    Dim value1, value2, value1Coor, value2Coor, slope, yInt As Double

    Private Sub Start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Start.Click

        MinimumValue.ReadOnly = True
        SetMinButton.Enabled = True
        SetMaxButton.Enabled = True
        MaximumValue.ReadOnly = True
        MinimumValue.Text = 0
        MaximumValue.Text = 100

    End Sub

    Private Sub SetMinButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetMinButton.Click

        Try
            value1 = CType(LoadCell1Data.Text.ToString, Double)
            value1Coor = CType(MinimumValue.Text.ToString, Double)

            MaximumValue.ReadOnly = True
            MinimumValue.ReadOnly = True
            SetMinButton.Enabled = False
            SetMaxButton.Enabled = True
        Catch ex As Exception
            MessageBox.Show("Please enter a valid Number", "Invalid Number")
        End Try

        My.Settings.Value1 = value1
        My.Settings.Value1Coor = value1Coor
        My.Settings.Save()

    End Sub

    Private Sub SetMaxButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SetMaxButton.Click
        Try
            value2 = CType(LoadCell1Data.Text, Double)
            value2Coor = CType(MaximumValue.Text, Double)

            'Calculate formula
            slope = (value2Coor - value1Coor) / (value2 - value1)
            ' slope = (100 - 0) / (value2 - value1)
            yInt = value1Coor - (value1 * slope)
            TextBox3.Text = "y = " + slope.ToString("F4") + "x + " + yInt.ToString("F4")

            MaximumValue.ReadOnly = True
            SetMaxButton.Enabled = False
        Catch ex As Exception
            MessageBox.Show("Please enter a valid Number", "Invalid Number")
        End Try

        My.Settings.Value2 = value2
        My.Settings.Value2Coor = value2Coor
        My.Settings.Save()
    End Sub
End Class