B4R Question How to Blink 2 LEDs Simaltaneously

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..
B4X:
#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
 

Mark Read

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):

B4X:
#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
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
i think its
B4X:
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
B4X:
Dim currentstate1 As Boolean

it would be easier if u use a array here
B4X:
Private pin10 As Pin
Private pin11 As Pin

B4X:
Private pin(40) As Pin
 
Last edited:
Upvote 0

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:
B4X:
timer1.Initialize("Blink1_Tick",1000)
timer2.Initialize("Blink2_Tick",2000)

Sub Blink1_Tick
Sub Blink2_Tick
But, the code in post#1 works.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
The Initialize method is different in B4R than in the other B4X products.
oh, i not expected that it is inconsistent there.
 
Upvote 0

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
 
Upvote 0

Similar Threads

Replies
0
Views
3K
  • Article
B4R Tutorial RGB Leds
Replies
8
Views
5K
Replies
8
Views
16K
Top