0

I want to execute couatall.text to increment but nested-if in timer ticking in every 1000 microsecond/1 second is not working. Please help me, here's my code...

I do this because my sensor read two,three times or more than that of less than 900 value in a time, which is if I only use '1-If condition' it count two,three times or more than that. so my logic(noob) is if my sensor reads less than 900 after that if my sensor reads greater than 899 it's execute 'coutall.Text = coutall.Text + 1' code to count 1 and so on.

Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
    Try
        Dim i As Single = SerialPort1.ReadExisting 'get data of my sensor
        label1.Text = i.ToString                   'display on label1.text
        If label1.Text < 900 Then
            'messageBox.show("working here")
            'I really make it comment lines don't mind it.
            If label1.Text > 899 Then   'not working when my i is <  to 900
                coutall.Text = coutall.Text + 1
            End If
        Else
            'nothing
        End If
    Catch ex As Exception

    End Try
End Sub

let me know if my explanation is not clear, I'm not good in English sorry. :)

fkeya
  • 1
  • 2
  • The incrementing line will never be reached. The first `If` will be true when the data is less than 900, but that means the second `If` will never be true. So you'll never get to the incrementing line. Other than that, it's not clear what you are intending to do with this code. – MarkL Sep 13 '19 at 06:05
  • AHA! I gets what you point, thanks! so whats approach I use than nested-if? while loop, for loop or what? hehehe sorry I'm newbie. – fkeya Sep 13 '19 at 06:10
  • 1
    Also, don't use a UI control (like a Label) instead of a variable - the UI control will be *much* slower. So it should be `If i < 900` etc. Another thing: if you use [`Option Strict On`](https://stackoverflow.com/a/29985039/1115360) it will make sure that variable types match up correctly and there will be no slowing down due to type conversions that it has to guess at. – Andrew Morton Sep 13 '19 at 08:39
  • thank you sir Andrew, how to commend here? hehe – fkeya Sep 14 '19 at 01:18
  • @MarkL Why can't i be 899.9? Certainly less than 900 but also greater than 899. We are not dealing with Integers here. i is Declared as Single. – Mary Sep 14 '19 at 06:13
  • @Mary - given the posted code (`i as Single`), you are correct, programatically. However, given the descriptions provided, and the other code, along with the OP apparently being a beginner, it seemed that they were actually dealing with integer values (despite the Single declaration), so I was avoiding the complications of describing the details of floating point variables and handling. An assumption on my part, of course, and I could be wrong on that. – MarkL Sep 14 '19 at 15:34

1 Answers1

0

Here's the answer, for future preference only (if)

Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
    Try
        Dim i As Single = SerialPort1.ReadExisting
        label1.Text = i.ToString
        If label1.Text < 900 Then
            Timer1.Enabled = True 'instead using nested here create timer again
        End If
    Catch ex As Exception

    End Try
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick 'new timer
    If label1.Text > 899 Then 'looks like nested
        coutall.Text = coutall.Text + 1 'execute count + 1
        Timer1.Enabled = False 'then shutdown timer 1
    End If
End Sub
fkeya
  • 1
  • 2
  • You still have an empty Catch so your code just swallows errors and continues processing with an undiscovered error. You still haven't turned on Option Strict which makes your code vulnerable to run time errors which should be discovered at compile time. – Mary Sep 14 '19 at 15:56