Michael Z Michael Z - 12 days ago 6
Vb.net Question

How to wait for a value until it performs the next syntax in VB.NET?

I am obtaining height data from my sensor and here is my code:

If Sensor1 = 5 Or Sensor1 = 6 Or Sensor1 = 7 Then
If Sensor1 >= 13 Then
flagCtr1 = 1
End If
ElseIf Sensor1 = 8 Or Sensor1 = 9 Then
If Sensor1 >= 13 Then
flagCtr1 = 2
EndIf
ElseIf Sensor1 = 10 Then
If Sensor1 >= 13 Then
flagCtr1 = 3
End If
End If


Basically what I want to happen is, for example, when my sensor detected the height 7, it will wait for the value greater than or equal 13 before it performs the flagCtr = 1. I tested this code and nothing happens.

This is my dilemma. This is an example of how I read the height where 15 is the height where there is no object placed below the sensor

15

15

15

7 - object was placed

7

7

7

7

8 - object is being removed

15

15

15


everytime I will remove the object it goes to a certain number that is also in one of the conditions. My problem is the latest height data is being read not the data that was read at a time interval. So what I want to do is that I will wait for the sensor to go back to the value greater than or equal 13 before it performs the next instruction.

It's like this, if I detected 7 I want to disregard the other values I read and wait for value that is >=13

Sorry for a long post, I really need help, this is my thesis. Thank you so much

EDIT1:


how i read my data from my sensors

AxWinsock1.GetData(strData)
for ctr = 0 To UBound(strData)
tstring = tstring + Chr(strData(ctr))
If DisconnectF1=0 Then
If tstring Like "*DATA_*_OK*" Then
Dim parsedStr() As String = Split(tsring,"_")
Sensor1 = parsedSTR(1) : Sensor2 = parsedSTR(2)
Sensor3 = parsedSTR(3) : Sensor4 = parsedSTR(4)


this what my arduino sends me:
DATA_15_15_15_15_OK

EDIT2:


For those who still have problems with the correct solution just make previousState global.

Answer

Your current if statement doesn't make sense since this

If Sensor1 = 5 Or Sensor1 = 6 Or Sensor1 = 7 Then
   If Sensor1 >= 13 Then

Assume that the variable Sensor1 can be both below and above 12 at the same time.

What you need to do is keep track of the previous sensor.

If PreviousSensor1 = 5 Or PreviousSensor1 = 6 Or PreviousSensor1 = 7 Then
   If Sensor1 >= 13 Then
      flagCtr1 = 1
   End If
ElseIf PreviousSensor1 = 8 Or PreviousSensor1 = 9 Then
   If Sensor1 >= 13 Then
      flagCtr1 = 2
   EndIf
ElseIf PreviousSensor1 = 10 Then
   If Sensor1 >= 13 Then
      flagCtr1 = 3
   End If
End If

PreviousSensor1 = Sensor1

EDIT

Since you want to ignore values until you hit 13. You could keep the flag value and set it when you hit >= 13. PreviousState would be set to 0 as default and kept in between calls.

If PreviousState = 0 Then
   If Sensor1 = 5 Or Sensor1 = 6 Or Sensor1 = 7 Then
      PreviousState = 1
   ElseIf Sensor1 = 8 Or Sensor1 = 9 Then
      PreviousState = 2
   ElseIf Sensor1 = 10 Then
      PreviousState = 3
   End If
Else
   If Sensor1 >= 13 Then
      flagCtr1 = PreviousState
      PreviousState = 0
   End If
End If

Here's an example.

Module Module1

    Sub Main()

        Dim state As Integer = 0
        Dim sensorData() As Integer = {15, 15, 15, 7, 7, 7, 7, 7, 8, 15, 15, 15}

        For Each data As Integer In sensorData
            state = ProcessSensor(state, data)
        Next

        Console.ReadLine()

    End Sub

    Public Function ProcessSensor(ByVal state As Integer, ByVal sensor1 As Integer)

        If state = 0 Then
            If sensor1 = 5 Or sensor1 = 6 Or sensor1 = 7 Then
                state = 1
            ElseIf sensor1 = 8 Or sensor1 = 9 Then
                state = 2
            ElseIf sensor1 = 10 Then
                state = 3
            End If
        Else
            If sensor1 >= 13 Then
                'flagCtr1 = previousState
                Console.WriteLine("Set flagCtr1 to {0}", state)
                state = 0
            End If
        End If

        Return state
    End Function

End Module