Take sensor readings after pressing button and reading

Supporting Visual Studio on Windows
Schmidtmotorworks
Phidgetsian
Posts: 6
Joined: Sun Feb 24, 2013 6:32 pm
Contact:

Take sensor readings after pressing button and reading

Postby Schmidtmotorworks » Tue Feb 26, 2013 12:49 am

This is my 1st Fidget project.

I am making a machine that measures the coefficient of friction on a clutch disc used in drag racing. The machine will put load on a cell from friction applied to the disc.

I want to record 10 samples in 1 second intervals after pressing a button and I want the intervals to begin after the load goes above a predefined value so that it will be easy to compare the graphs.

I tried this code below but have two problems:
1. It returns the 1st value measured for each cycle of the for loop
2. Much of the time I get a some packets were lost message.

If you have any suggestions, to improve this thanks in advance.

Code: Select all

Private Sub ifKit_SensorChange(ByVal sender As Object, ByVal e As Phidgets.Events.SensorChangeEventArgs) Handles phidgetIFK.SensorChange
        Dim i As Integer = 0
        Dim stopwatch As Stopwatch = stopwatch.StartNew
        Thread.Sleep(0)
        stopwatch.Stop()

        If buttonState = 1 Then
            For i = 0 To 10
                TextBoxData.AppendText(Environment.NewLine & phidgetIFK.sensors(0).Value)
                stopwatch.StartNew()
                Thread.Sleep(1000)
                stopwatch.Stop()
            Next
        End If
        buttonState = 0
    End Sub

User avatar
mparadis
Site Admin
Posts: 625
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Take sensor readings after pressing button and reading

Postby mparadis » Tue Feb 26, 2013 9:11 am

The lost packets message usually occurs when you're spending too much time inside an event handler, thus blocking additional events from being caught. The sleep command inside the loop is the main issue. If you want 10 readings a second apart each, you should handle the timing outside of the event handler, and use a variable to select sensor values that are one second apart.

Schmidtmotorworks
Phidgetsian
Posts: 6
Joined: Sun Feb 24, 2013 6:32 pm
Contact:

Re: Take sensor readings after pressing button and reading

Postby Schmidtmotorworks » Tue Feb 26, 2013 10:12 am

Thanks for the advice, I am trying to imagine how to do that.

Are you thinking that I should figure out some kind of do/while loop that cycles for 10 seconds and then within that loop have an if/then that takes a reading if the time passes some increment?

Another possible way might be to fill an array of all times and values recorded and go back and find the values at the desired times?

If you have something better in mind, I would really appreciate some additional detail of your idea.

I am modifying the Interfacekit-full example which has code to write the analog_in text boxes, I am thinking maybe I should disable that to free up resources.

User avatar
mparadis
Site Admin
Posts: 625
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Take sensor readings after pressing button and reading

Postby mparadis » Tue Feb 26, 2013 12:38 pm

One easy way to do it would be to change the data rate of the analog input that the sensor is attached to. Something like "phidgetIFK.sensors(0).DataRate = 1000" would make it so that sensor change triggers only occur every second, and each value would be the average of all samples taken within that one second period. Then you could remove most of the code inside your event handler, leaving just an if statement that checks if the load is high enough to start sampling, and the text box update.

Schmidtmotorworks
Phidgetsian
Posts: 6
Joined: Sun Feb 24, 2013 6:32 pm
Contact:

Re: Take sensor readings after pressing button and reading

Postby Schmidtmotorworks » Tue Feb 26, 2013 2:42 pm

Brilliant, great idea!

I'll try that tonight, thanks very much for your help!

Schmidtmotorworks
Phidgetsian
Posts: 6
Joined: Sun Feb 24, 2013 6:32 pm
Contact:

Re: Take sensor readings after pressing button and reading

Postby Schmidtmotorworks » Tue Feb 26, 2013 11:39 pm

I have been trying everything I can think of to put
"phidgetIFK.sensors(0).DataRate = 1000" in the code but everything I try raises errors.

Could you please point me to where it should go?

I am concerned though that if the value stays constant it will not record that, I really need to correlate it to time intervals.

The problem I am running into though is if I write the value to a variable and use any other loop to write it to the text-box it doesn't update the value of the variable, it just writes the first value over and over again and never updates.

Any ideas?

Schmidtmotorworks
Phidgetsian
Posts: 6
Joined: Sun Feb 24, 2013 6:32 pm
Contact:

Re: Take sensor readings after pressing button and reading

Postby Schmidtmotorworks » Wed Feb 27, 2013 1:03 am

I came up with a new way to do it.

1st I had to comment out the code that wrote data any time the value changed

Then Added this code:

Code: Select all

    Public Sub PrintData()
        TextBoxData.AppendText(Environment.NewLine & phidgetIFK.sensors(0).Value)
    End Sub
    Public TenthSecondCount As Integer = 0
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        phidgetIFK.sensors(0).DataRate.ToString(100)
        TenthSecondCount = 0
        Me.Timer_Clock.Interval = 100

        While phidgetIFK.sensors(0).Value < 110
        End While

        Me.Timer_Clock.Enabled = True

    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer_Clock.Tick
        Me.Lbl_Clock.Text = TenthSecondCount
        TenthSecondCount += 1
        PrintData()
        If TenthSecondCount = 10 Then
            Me.Timer_Clock.Enabled = False
        End If
    End Sub


Return to “VB .NET”

Who is online

Users browsing this forum: No registered users and 1 guest