B4R Question Arduino - Servo - SR505

Beja

Expert
Licensed User
Longtime User
Hi all,
Since a few days I am trying to modify [B]inakigarm[/B] tutorial listed below.. it's working like a charm, In the example, the servo goes up (servo rotates clockwise) until
it counts 180 degrees , then then it goes down (servo rotates counterclockwise) then repeats the process forever (closed loop).
I want to split the timer_tick sub into two conditional branches;
1. If the SR505 input is high then the counter goes up (if it's not already at 180 degrees then it does nothing
2. If the SR505 input is low then the counter goes down (if it's not already false then it does nothing
That's all.. I will donate $50 for the first solution.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Public servo1 As Servo
    Public pinservo As Pin
    Public Timer1 As Timer
    Public angleservo As UInt
    Public upangle As Boolean

End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    pinservo.Initialize (4,pinservo.MODE_OUTPUT)    'connect servo to pin 4 of Arduino
    servo1.Attach(pinservo.PinNumber)                'assign servo to device on pin servo

    Timer1.Initialize ("Timer1_Tick",50)           'Call Timer every second
    Timer1.Enabled=True

    angleservo=servo1.Read                             'initial servo angle
    upangle=True                                    'Increment angle

End Sub

Sub Timer1_Tick

    angleservo=servo1.read
  
    Select upangle                                    'Increment angle
  
        Case True
            If (angleservo >=0 And angleservo <180) Then
                Log ("up angle ",angleservo)
                angleservo=angleservo +3
                servo1.Write(angleservo)
            End If
      
            If angleservo=180 Then upangle=False
      
        Case False                                    'Decrement angle
      
            If angleservo <=180 And angleservo>0 Then
                Log ("down angle ",angleservo)
                angleservo=angleservo-3
                servo1.Write(angleservo)
            End If
      
            If angleservo=0 Then upangle=True
  
    End Select

End Sub
 

janderkan

Well-Known Member
Licensed User
Longtime User
This is not tested and it will not cost you anything.

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Public servo1 As Servo
    Public pinservo As Pin
    Public Timer1 As Timer
    Public angleservo As Int   '<-

    Public pin505 As Pin
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    pinservo.Initialize (4,pinservo.MODE_OUTPUT)    'connect servo to pin 4 of Arduino
    servo1.Attach(pinservo.PinNumber)                'assign servo to device on pin servo

    Timer1.Initialize ("Timer1_Tick",50)           'Call Timer every 50 millisecond
    Timer1.Enabled=True

    pin505.Initialize(5,pin505.MODE_INPUT_PULLUP)
End Sub

Sub Timer1_Tick
    angleservo=servo1.read
  
    Select pin505.DigitalRead
        Case True                                            'Increment angle
            If angleservo<180 Then
                Log ("up angle ",angleservo)
                angleservo=angleservo +3
                If angleservo>180 Then angleservo=180
                servo1.Write(angleservo)
            End If
    
        Case False                                        'Decrement angle
            If angleservo>0 Then
                Log ("down angle ",angleservo)
                angleservo=angleservo-3
                If angleservo<0 Then angleservo=0
                servo1.Write(angleservo)
            End If
    End Select
End Sub
 
Upvote 0
Top