Hi guys...
Here's another example of RGB "smart LED" manipulation...
And the code that goes with it...
This code uses a pseudo 2D array (snippet provided by EREL) and, of course, my WS2812B Code Module
(search the forum for it!)
My coding skills are far from perfect (maybe they are in a galaxy far... far... far... hmmm... far away!!) so I've used too many variables and maybe even some redundancy in it, but hey, it looks cool!!
Here's another example of RGB "smart LED" manipulation...
And the code that goes with it...
B4X:
#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 300
#End Region
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
Private My2dArray(8 * 5) As Byte '2D Array workaround (8x5)
Private XDirection1() As String = Array As String("Left","Right")
Private XDirection2() As String = Array As String("Left","Right")
Private XDirection3() As String = Array As String("Left","Right")
Private YDirection1() As String = Array As String("Up","Down")
Private YDirection2() As String = Array As String("Up","Down")
Private YDirection3() As String = Array As String("Up","Down")
Private XPosition1, Yposition1, XOld1,YOld1 As Int
Private XPosition2, Yposition2, XOld2,YOld2 As Int
Private XPosition3, Yposition3, XOld3,YOld3 As Int
Private XDirValue1, YDirValue1 As Int
Private XDirValue2, YDirValue2 As Int
Private XDirValue3, YDirValue3 As Int
Private Timer1,Timer2, Timer3 As Timer
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
Timer1.Initialize("Timer1_Tick",100) 'Initialize the Timer
Timer2.Initialize("Timer2_Tick",150) 'Initialize the Timer
Timer3.Initialize("Timer3_Tick",200) 'Initialize the Timer
WS2812B.Initialize(40) 'Initialize our LED ARRAY
'prepare the 2D-Array
Private n As Int = 0
For x = 0 To 4
For y = 0 To 7
Set(x,y,n)
n = n + 1
Next
Next
Line1
Delay(200)
Line2
Delay(200)
Line3
End Sub
Private Sub Line1
'Set Initial Position
Dim x As Int = Rnd(0,8)
If x > 0 And x < 7 Then
XPosition1 = x
Dim y As Int = Rnd(0,2)
If y >0 Then
Yposition1 = 4
Else
Yposition1 = 0
End If
Else
XPosition1 = x
Dim y As Int = Rnd(0,5)
Yposition1 = y
End If
Log("Position ",XPosition1, " : ", Yposition1)
Log("LED : ", Get(Yposition1,XPosition1))
'Light Led in first position
WS2812B.SetLed(Get(Yposition1,XPosition1),0,0,12)
'Refresh Old values so that in tick the previous position gets turned off
XOld1 = XPosition1
YOld1 = Yposition1
'Define starting Direction
XDirValue1 = Rnd(0,2)
YDirValue1 = Rnd(0,2)
Log("going ",XDirection1(XDirValue1))
Log("and ",YDirection1(YDirValue1))
'Start Timer
Timer1.Enabled=True
End Sub
Private Sub Timer1_Tick
'Turn Off the previous LED Position
XOld1 = XPosition1
YOld1 = Yposition1
WS2812B.SetLed(Get(YOld1,XOld1),0,0,0)
'Check Direction
Log("going ",XDirection1(XDirValue1))
Log("and ",YDirection1(YDirValue1))
Select XDirValue1
Case 0 'Left
If XPosition1 > 0 Then
XPosition1 = XPosition1 - 1
Else
XDirValue1 = 1
End If
Case 1 'Right
If XPosition1 < 7 Then
XPosition1 = XPosition1 + 1
Else
XDirValue1 = 0
End If
End Select
Select YDirValue1
Case 0 'Up
If Yposition1 > 0 Then
Yposition1 = Yposition1 - 1
Else
YDirValue1 = 1
End If
Case 1 'Down
If Yposition1 < 4 Then
Yposition1 = Yposition1 + 1
Else
YDirValue1 = 0
End If
End Select
Log(XPosition1," : ",Yposition1)
'Light Led in first position
WS2812B.SetLed(Get(Yposition1,XPosition1),0,0,30)
End Sub
Private Sub Line2
'Set Initial Position
Dim x As Int = Rnd(0,8)
If x > 0 And x < 7 Then
XPosition2 = x
Dim y As Int = Rnd(0,2)
If y >0 Then
Yposition2 = 4
Else
Yposition2 = 0
End If
Else
XPosition2 = x
Dim y As Int = Rnd(0,5)
Yposition2 = y
End If
Log("Position ",XPosition2, " : ", Yposition2)
Log("LED : ", Get(Yposition2,XPosition2))
'Light Led in first position
WS2812B.SetLed(Get(Yposition2,XPosition2),0,0,12)
'Refresh Old values so that in tick the previous position gets turned off
XOld2 = XPosition2
YOld2 = Yposition2
'Define starting Direction
XDirValue2 = Rnd(0,2)
YDirValue2 = Rnd(0,2)
Log("going ",XDirection2(XDirValue2))
Log("and ",YDirection2(YDirValue2))
'Start Timer
Timer2.Enabled=True
End Sub
Private Sub Timer2_Tick
'Turn Off the previous LED Position
XOld2 = XPosition2
YOld2 = Yposition2
WS2812B.SetLed(Get(YOld2,XOld2),0,0,0)
'Check Direction
Log("going ",XDirection2(XDirValue2))
Log("and ",YDirection2(YDirValue2))
Select XDirValue2
Case 0 'Left
If XPosition2 > 0 Then
XPosition2 = XPosition2 - 1
Else
XDirValue2 = 1
End If
Case 1 'Right
If XPosition2 < 7 Then
XPosition2 = XPosition2 + 1
Else
XDirValue2 = 0
End If
End Select
Select YDirValue2
Case 0 'Up
If Yposition2 > 0 Then
Yposition2 = Yposition2 - 1
Else
YDirValue2 = 1
End If
Case 1 'Down
If Yposition2 < 4 Then
Yposition2 = Yposition2 + 1
Else
YDirValue2 = 0
End If
End Select
Log(XPosition2," : ",Yposition2)
'Light Led in first position
WS2812B.SetLed(Get(Yposition2,XPosition2),0,30,0)
End Sub
Private Sub Line3
'Set Initial Position
Dim x As Int = Rnd(0,8)
If x > 0 And x < 7 Then
XPosition3 = x
Dim y As Int = Rnd(0,2)
If y >0 Then
Yposition3 = 4
Else
Yposition3 = 0
End If
Else
XPosition3 = x
Dim y As Int = Rnd(0,5)
Yposition3 = y
End If
Log("Position ",XPosition3, " : ", Yposition3)
Log("LED : ", Get(Yposition3,XPosition3))
'Light Led in first position
WS2812B.SetLed(Get(Yposition3,XPosition3),0,0,12)
'Refresh Old values so that in tick the previous position gets turned off
XOld1 = XPosition3
YOld1 = Yposition3
'Define starting Direction
XDirValue3 = Rnd(0,2)
YDirValue3 = Rnd(0,2)
Log("going ",XDirection3(XDirValue3))
Log("and ",YDirection3(YDirValue3))
'Start Timer
Timer3.Enabled=True
End Sub
Private Sub Timer3_Tick
'Turn Off the previous LED Position
XOld1 = XPosition3
YOld1 = Yposition3
WS2812B.SetLed(Get(YOld1,XOld1),0,0,0)
'Check Direction
Log("going ",XDirection3(XDirValue3))
Log("and ",YDirection3(YDirValue3))
Select XDirValue3
Case 0 'Left
If XPosition3 > 0 Then
XPosition3 = XPosition3 - 1
Else
XDirValue3 = 1
End If
Case 1 'Right
If XPosition3 < 7 Then
XPosition3 = XPosition3 + 1
Else
XDirValue3 = 0
End If
End Select
Select YDirValue3
Case 0 'Up
If Yposition3 > 0 Then
Yposition3 = Yposition3 - 1
Else
YDirValue3 = 1
End If
Case 1 'Down
If Yposition3 < 4 Then
Yposition3 = Yposition3 + 1
Else
YDirValue3 = 0
End If
End Select
Log(XPosition3," : ",Yposition3)
'Light Led in first position
WS2812B.SetLed(Get(Yposition3,XPosition3),30,0,0)
End Sub
Private Sub Get(x As Byte, y As Byte) As Byte
Return My2dArray(x * 8 + y)
End Sub
Private Sub Set(x As Byte, y As Byte, value As Byte)
My2dArray(x * 8 + y) = value
End Sub
This code uses a pseudo 2D array (snippet provided by EREL) and, of course, my WS2812B Code Module
(search the forum for it!)
My coding skills are far from perfect (maybe they are in a galaxy far... far... far... hmmm... far away!!) so I've used too many variables and maybe even some redundancy in it, but hey, it looks cool!!
Last edited: