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