B4R Question How to Blink 2 LEDs Simaltaneously

Discussion in 'B4R Questions' started by pokhraj_d, Aug 21, 2018.

  1. pokhraj_d

    pokhraj_d Member

    Hello All,
    I have the sketch which blinks one LED every 1 sec [Connect at Pin10 with arduino].
    Now I want to blink another LED at pin11. But it is not working.
    The below is the sketch..
    Code:
    #Region Project Attributes
        
    #AutoFlushLogs: True
        
    #CheckArrayBounds: True
        
    #StackBufferSize: 300
    #End Region

    Sub Process_Globals
        
        
    Public Serial1 As Serial
        
    Private pin10 As Pin
        
    Private pin11 As Pin
        
    Private timer1 As Timer
        
    Private timer2 As Timer
    End Sub

    Private Sub AppStart
        Serial1.Initialize(
    115200)
        
    Log("AppStart")
        pin10.Initialize(
    10,pin10.MODE_OUTPUT)
        pin11.Initialize(
    11,pin11.MODE_OUTPUT)
        timer1.Initialize(
    "blink_tick",1000)
        timer2.Initialize(
    "blink_tick2",2000)
        timer1.Enabled=
    True
        timer2.Enabled=
    True
    End Sub

    Private Sub blink_tick
        
    Dim currentstate As Boolean
        currentstate = pin10.DigitalRead
        
    'Log("The value of current State : ", currentstate)
        Dim newstate As Boolean= Not(currentstate)
        
    'Log("The value of Newstate :" , newstate)
        pin10.DigitalWrite(newstate)
        
    'Log("The DR value : " , pin10.DigitalRead)
    End Sub

    Private Sub blink_tick2
        
    Dim currentstate1 As Boolean
        currentstate1 = pin11.DigitalRead
        
    'Log("The value of current State1 : ", currentstate1)
        Dim newstate1 As Boolean= Not(currentstate1)
        
    'Log("The value of Newstate :" , newstate1)
        pin11.DigitalWrite(newstate1)
        
    'Log("The DR value : " , pin11.DigitalRead)
    End Sub
    Please advice ..

    Thanks-
    Pokhraj Das
     
  2. mark35at

    mark35at Well-Known Member Licensed User

    Firstly, I think you are in the wrong forum with your question. I think it should be in B4R questions forum but Erel will probably move it.

    Secondly, you could try this (not tested):

    Code:
    #Region Project Attributes
        
    #AutoFlushLogs: True
        
    #CheckArrayBounds: True
        
    #StackBufferSize: 300
    #End Region

    Sub Process_Globals
       
        
    Public Serial1 As Serial
        
    Private pin10 As Pin
        
    Private pin11 As Pin
        
    Private timer1 As Timer
        
    Dim currentstate As Boolean
        
    Dim Pin11State As Int=0
    End Sub

    Private Sub AppStart
        Serial1.Initialize(
    115200)
        
    Log("AppStart")
        pin10.Initialize(
    10,pin10.MODE_OUTPUT)
        pin11.Initialize(
    11,pin11.MODE_OUTPUT)
        timer1.Initialize(
    "blink_tick",1000)
        timer1.Enabled=
    True
       
    End Sub

    Private Sub blink_tick
        currentstate = pin10.DigitalRead
        
    'Log("The value of current State : ", currentstate)
       
        Pin11State=Pin11State+
    1
       
        
    If Pin11State=2 Then                    'blink every second tick
            pin11.DigitalWrite(True)
            Pin11State=
    0
        
    Else
            pin11.DigitalWrite(
    False)
        
    End If
       
        currentstate= 
    Not(currentstate)
        pin10.DigitalWrite(currentstate)
    End Sub
     
  3. MarkusR

    MarkusR Well-Known Member Licensed User

    i think its
    Code:
    timer1.Initialize("Blink1",1000)
    timer2.Initialize(
    "Blink2",2000)

    Sub Blink1_Tick
    Sub Blink2_Tick
    u can use the same name for variables inside different subs.
    it was not necessary to rename the variable in the copy of a sub
    Code:
    Dim currentstate1 As Boolean
    it would be easier if u use a array here
    Code:
    Private pin10 As Pin
    Private pin11 As Pin
    Code:
    Private pin(40As Pin
     
    Last edited: Aug 22, 2018
  4. klaus

    klaus Expert Licensed User

    I tested your code and it works here !?
    There must be something else beeing wrong?
    Is your wireing OK?

    @MarkusR
    The Initialize method is different in B4R than in the other B4X products.
    I would have used:
    Code:
    timer1.Initialize("Blink1_Tick",1000)
    timer2.Initialize(
    "Blink2_Tick",2000)

    Sub Blink1_Tick
    Sub Blink2_Tick
    But, the code in post#1 works.
     
    Peter Simpson likes this.
  5. tigrot

    tigrot Well-Known Member Licensed User

    Tested your code as well. It works, check led and wireing. Maybe led is reversed?
     
    Peter Simpson likes this.
  6. MarkusR

    MarkusR Well-Known Member Licensed User

    oh, i not expected that it is inconsistent there.
     
  7. Peter Simpson

    Peter Simpson Expert Licensed User

    No issues with your code here either...
     
    tigrot likes this.
  8. pokhraj_d

    pokhraj_d Member

    Yes.... Its my mistake.... Wiring was wrong.. Its working fine now. Thanks for the concepts of Array of Pins @MarkusR . I am following these rules only.
    Again thank you all

    Thanks-
    Pokhraj Das
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice