B4R Question Waiting for analog value

BaGRoS

Active Member
Licensed User
Longtime User
Please help me to solve the problem. I control the potentiometer engine speed. I do not want to turn on the engine when the rpm is maximum. I can not stop the main thread, how to wait until the user will reduce the speed to the minimum?
 

BaGRoS

Active Member
Licensed User
Longtime User
I understand, I wanted to do that. If in the timer I wait for the right value and then run the proper procedure then it will lock this timer forever. How to call the procedure simultaneously while exiting the timer? Using CallSubPlus will be fine?

B4X:
Sub SpeedMotorTimer_Tick

    If SpeedMotor.AnalogRead > 5 And Not(SpeedMotorStatus) Then
        'blink fast
        Start_Blink(100)
        'already send message
        SpeedMotorStatus = True
    'if speed is ok then START
    Else If SpeedMotor.AnalogRead < 5 Then
        'maybe for future
        SpeedMotorStatus =False
        'timer not needed
        SpeedMotorTimer.Enabled = False
        'blinking slow
        Start_Blink(400)
        Log("Speed setting already")
        'go to main loop
    End If

End Sub

I change for testing

B4X:
Sub SpeedMotorTimer_Tick

    If SpeedMotor.AnalogRead > 5 And Not(SpeedMotorStatus) Then
        'blink fast
        Start_Blink(100)
        'already send message
        SpeedMotorStatus = True
    'if speed is ok then START
    Else If SpeedMotor.AnalogRead < 5 Then
        'maybe for future
        SpeedMotorStatus =False
        'timer not needed
        SpeedMotorTimer.Enabled = False
        'blinking slow
        Start_Blink(400)
        Log("Speed setting already")
        'go to main loop
        CallSubPlus("MainSub",0,0)
        Log("I'm back...")
    End If

End Sub

Sub MainSub

    Do While True
    
        If SpeedMotor.AnalogRead > 1000 Then
            Exit
        End If
    
    Loop


    Log("Still in MainSub")
    Start_Blink(1000)

End Sub

and never see "Still in MainSub", but see "I'm back...". Only freeze Arduino because led stay on or off.
Help me, how escape from SpeedMotorTimer_Tick to MainSub.

EDIT: Working well, but when I'm in MainSub then no blinking...
 
Last edited:
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Timers, in b4R DO NOT block the main thread, so you should check the value inside the timer's tick event and act accordingly still inside it!
 
Upvote 0

BaGRoS

Active Member
Licensed User
Longtime User
Looper don't block main thread?!
How stop Looper?

EDIT:
Ok, no exit.
B4X:
If LooperRun Then
    *CODE*
End If
should be fine :)
 
Last edited:
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
A looper will repeat it self as fast as possible but a do while or until will block it.
A timer will restart the sub every tick, regardless of any inside do while or until!
 
Upvote 0

BaGRoS

Active Member
Licensed User
Longtime User
Yes, I know

B4X:
'Main move procedure as Looper
Sub MotorMove
  
    'working only if MotorEnable
    If MotorEnable Then
      
        'if time is right
        If Micros > microsNextStep Then
          
            'make step
            p1.DigitalWrite(True)
            p1.DigitalWrite(False)
      
            'calculating time of next step
            microsNextStep = Micros + microDelayStep
          
        End If
      
    End If
  
End Sub

but after 70 minutes?!

B4X:
'Main move procedure as Looper
Sub MotorMove
  
    'working only if MotorEnable
    If MotorEnable Then
      
        'if time is right
        If Micros > microsNextStep or Micros < microDelayStep Then
          
            'make step
            p1.DigitalWrite(True)
            p1.DigitalWrite(False)
      
            'calculating time of next step
            microsNextStep = Micros + microDelayStep
          
        End If
      
    End If
  
End Sub

???
 
Upvote 0
Top