Just for fun. Have built a "flat" 3x3x3 LED cube on a breadboard and driving it with a Nano. There are 3 layers each with 9 LED's:
1. Layer 0 = Blue
2. Layer 1 = Green
3. Layer 2 = Red
All anodes on each layer connected to one another and go via a resistor (per layer) to Pins 12 (layer 0), 11 (layer 1), and 10 (layer 2).
All LED cathodes in each "column" connected together and go to pins 0, 1, 2, 3, 4, 5, 6, 7, and 8
A wiring nightmare on a breadboard..... But working.
The code that I am fooling around with (no where close to being optimised):
1. Layer 0 = Blue
2. Layer 1 = Green
3. Layer 2 = Red
All anodes on each layer connected to one another and go via a resistor (per layer) to Pins 12 (layer 0), 11 (layer 1), and 10 (layer 2).
All LED cathodes in each "column" connected together and go to pins 0, 1, 2, 3, 4, 5, 6, 7, and 8
A wiring nightmare on a breadboard..... But working.
The code that I am fooling around with (no where close to being optimised):
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 layer(3) As Pin 'Rows
Private column(9) As Pin 'Columns
Dim L2(9) As Boolean
Dim L1(9) As Boolean
Dim L0(9) As Boolean
Dim delaytime As ULong = 5
End Sub
Private Sub AppStart
' Serial1.Initialize(115200)
' Log("AppStart")
For i = 0 To 8
L2(i) = True
L1(i) = True
L0(i) = True
Next
layer(0).Initialize(12, layer(0).MODE_OUTPUT)
layer(1).Initialize(11, layer(1).MODE_OUTPUT)
layer(2).Initialize(10, layer(2).MODE_OUTPUT)
For i = 0 To 8
column(i).Initialize(i, column(i).MODE_OUTPUT)
Next
Delay (1000)
turneverythingoff
Dim p As Pin
p.Initialize(p.A6,p.MODE_INPUT)
RndSeed(p.AnalogRead)
AddLooper ("mylooper")
End Sub
Sub mylooper
For i = 0 To 8
L2(i) = True
L1(i) = True
L0(i) = True
Next
turneverythingoff
SpinCross
turneverythingoff
stepThreeEveryLayerLeftToRight
turneverythingoff
stepThreeEachLayerLeftToRight
turneverythingoff
' RunNative("loop1", Null)
' turneverythingoff
moveBackToFront
turneverythingoff
moveLeftToRight
turneverythingoff
stepOneAtATime
turneverythingoff
dropDownRandomly
turneverythingoff
dropUpRandomly
turneverythingoff
drawOutline
turneverythingoff
flickerOff
turneverythingoff
stepOutsideOfLayerCW
turneverythingoff
rainTopToBottom
turneverythingoff
stepOutsideOfLayerACW
turneverythingoff
flickerOn
turneverythingoff
columnRain
turneverythingoff
End Sub
Sub moveLeftToRight
For kk = 0 To 9
For i = 0 To 2
layer(i).DigitalWrite(True)
Next
For j = 0 To 2
column(j).DigitalWrite(False)
Next
Delay(100)
For j = 0 To 2
column(j).DigitalWrite(True)
Next
For j = 3 To 5
column(j).DigitalWrite(False)
Next
Delay(100)
For j = 3 To 5
column(j).DigitalWrite(True)
Next
For j = 6 To 8
column(j).DigitalWrite(False)
Next
Delay(100)
For j = 6 To 8
column(j).DigitalWrite(True)
Next
For j = 3 To 5
column(j).DigitalWrite(False)
Next
Delay(100)
For j = 3 To 5
column(j).DigitalWrite(True)
Next
For j = 0 To 2
column(j).DigitalWrite(False)
Next
Delay(100)
For j = 0 To 2
column(j).DigitalWrite(True)
Next
Next
End Sub
Sub moveBackToFront
For kk = 0 To 9
For i = 0 To 2
layer(i).DigitalWrite(True)
Next
For j = 0 To 6 Step 3
column(j).DigitalWrite(False)
Next
Delay(100)
For j = 0 To 6 Step 3
column(j).DigitalWrite(True)
Next
For j = 1 To 7 Step 3
column(j).DigitalWrite(False)
Next
Delay(100)
For j = 1 To 7 Step 3
column(j).DigitalWrite(True)
Next
For j = 2 To 8 Step 3
column(j).DigitalWrite(False)
Next
Delay(100)
For j = 2 To 8 Step 3
column(j).DigitalWrite(True)
Next
For j = 1 To 7 Step 3
column(j).DigitalWrite(False)
Next
Delay(100)
For j = 1 To 7 Step 3
column(j).DigitalWrite(True)
Next
For j = 0 To 6 Step 3
column(j).DigitalWrite(False)
Next
Delay(100)
For j = 0 To 6 Step 3
column(j).DigitalWrite(True)
Next
Next
End Sub
Sub columnRain
For c = 0 To 1000
Dim a As Long = Rnd(0,9)
Dim b As Long = Rnd(0,3)
column(a).DigitalWrite(False)
layer(b).DigitalWrite(True)
Delay(5)
column(a).DigitalWrite(True)
layer(b).DigitalWrite(False)
Next
End Sub
Sub stepOneAtATime
For i = 2 To 0 Step -1
layer(i).DigitalWrite(True)
For j = 0 To 8
column(j).DigitalWrite(False)
Delay(100)
column(j).DigitalWrite(True)
Next
turneverythingoff
Next
End Sub
Sub flickerOn
For k = 0 To 5
Dim i As Int = 24
Do While i <> 0
turnEverythingOn
Delay(i)
turneverythingoff
Delay(i)
i = i - 2
Loop
Next
End Sub
Sub flickerOff
turnEverythingOn
For k = 0 To 5
For i = 2 To 20
If i <> 20 Then
turneverythingoff
Delay(i+25)
turnEverythingOn
Delay(i)
End If
Next
Next
End Sub
Sub turneverythingoff
For i = 0 To 2
layer(i).DigitalWrite(False)
Next
For i = 0 To 8
column(i).DigitalWrite(True)
Next
End Sub
Sub dropDownRandomly
For zz = 0 To 20
Dim randvalue As Long = Rnd(0, 256)
randvalue = randvalue Mod 9
' Dim randvalue1 As Long = Rnd(0, 256)
' randvalue1 = randvalue1 Mod 16
For i = 2 To 0 Step -1
layer(i).DigitalWrite(True)
column(randvalue).DigitalWrite(False)
' Delay(100)
' column((randvalue1 + 7) Mod 7).DigitalWrite(False)
Delay(75)
column(randvalue).DigitalWrite(True)
' Delay(100)
' column((randvalue1 + 7) Mod 7).DigitalWrite(True)
layer(i).DigitalWrite(False)
Next
Delay(150)
Next
End Sub
Sub dropUpRandomly
For zz = 0 To 20
Dim randvalue As Long = Rnd(0, 256)
randvalue = randvalue Mod 9
' Dim randvalue1 As Long = Rnd(0, 256)
' randvalue1 = randvalue1 Mod 16
For i = 0 To 2
layer(i).DigitalWrite(True)
column(randvalue).DigitalWrite(False)
' Delay(100)
' column((randvalue1 + 7) Mod 7).DigitalWrite(False)
Delay(75)
column(randvalue).DigitalWrite(True)
' Delay(100)
' column((randvalue1 + 7) Mod 7).DigitalWrite(True)
layer(i).DigitalWrite(False)
Next
Delay(150)
Next
End Sub
Sub rainTopToBottom
Dim cnt1 As Int = 0
For k = 0 To 2000
Dim r1 As Long = Rnd(0,20)
If r1 = 0 Then cnt1 = cnt1 + 1
If cnt1 = 1 Then
Dim randvalue As Long = Rnd(0, 256)
randvalue = randvalue Mod 9
For i = 0 To 8
If L2(randvalue) = True And L1(randvalue) = True And L0(randvalue) = True Then
L2(randvalue) = False
else If L2(randvalue) = False And L1(randvalue) = True And L0(randvalue) = True Then
L2(randvalue) = True
L1(randvalue) = False
else If L2(randvalue) = True And L1(randvalue) = False And L0(randvalue) = True Then
L1(randvalue) = True
L0(randvalue) = False
else If L2(randvalue) = True And L1(randvalue) = True And L0(randvalue) = False Then
L0(randvalue) = True
End If
Next
End If
If cnt1 = 1 Then cnt1 = 0
For j = 0 To 8
column(j).DigitalWrite(L2(j))
Next
layer(2).DigitalWrite(True)
Delay(delaytime)
layer(2).DigitalWrite(False)
For j = 0 To 8
column(j).DigitalWrite(L1(j))
Next
layer(1).DigitalWrite(True)
Delay(delaytime)
layer(1).DigitalWrite(False)
For j = 0 To 8
column(j).DigitalWrite(L0(j))
Next
layer(0).DigitalWrite(True)
Delay(delaytime)
layer(0).DigitalWrite(False)
Next
End Sub
Sub drawOutline
For z = 0 To 2
For k = 1 To 200
For i = 0 To 3
column(i).DigitalWrite(False)
Next
For i = 5 To 8
column(i).DigitalWrite(False)
Next
layer(0).DigitalWrite(True)
Delay(2)
For i = 0 To 3
column(i).DigitalWrite(True)
Next
For i = 5 To 8
column(i).DigitalWrite(True)
Next
layer(0).DigitalWrite(False)
column(0).DigitalWrite(False)
column(2).DigitalWrite(False)
column(6).DigitalWrite(False)
column(8).DigitalWrite(False)
layer(1).DigitalWrite(True)
Delay(2)
column(0).DigitalWrite(True)
column(2).DigitalWrite(True)
column(6).DigitalWrite(True)
column(8).DigitalWrite(True)
layer(1).DigitalWrite(False)
For i = 0 To 3
column(i).DigitalWrite(False)
Next
For i = 5 To 8
column(i).DigitalWrite(False)
Next
layer(2).DigitalWrite(True)
Delay(2)
For i = 0 To 3
column(i).DigitalWrite(True)
Next
For i = 5 To 8
column(i).DigitalWrite(True)
Next
layer(2).DigitalWrite(False)
Next
Delay(100)
Next
End Sub
Sub turnEverythingOn
For i = 0 To 8
column(i).DigitalWrite(False)
For j = 0 To 2
layer(j).DigitalWrite(True)
Next
Next
End Sub
Sub stepOutsideOfLayerCW
For x = 0 To 50
Dim lyr As Int = Rnd(0,3)
layer(lyr).DigitalWrite(True)
For i = 0 To 2
column(i).DigitalWrite(False)
Delay(25)
column(i).DigitalWrite(True)
Next
For i = 5 To 8 Step 3
column(i).DigitalWrite(False)
Delay(25)
column(i).DigitalWrite(True)
Next
For i = 7 To 6 Step - 1
column(i).DigitalWrite(False)
Delay(25)
column(i).DigitalWrite(True)
Next
For i = 3 To 0 Step - 3
column(i).DigitalWrite(False)
Delay(25)
column(i).DigitalWrite(True)
Next
layer(lyr).DigitalWrite(False)
Next
End Sub
Sub stepOutsideOfLayerACW
For x = 0 To 50
Dim lyr As Int = Rnd(0,3)
layer(lyr).DigitalWrite(True)
For i = 0 To 6 Step 3
column(i).DigitalWrite(False)
Delay(25)
column(i).DigitalWrite(True)
Next
For i = 6 To 8
column(i).DigitalWrite(False)
Delay(25)
column(i).DigitalWrite(True)
Next
For i = 5 To 2 Step - 3
column(i).DigitalWrite(False)
Delay(25)
column(i).DigitalWrite(True)
Next
For i = 2 To 0 Step - 1
column(i).DigitalWrite(False)
Delay(25)
column(i).DigitalWrite(True)
Next
layer(lyr).DigitalWrite(False)
Next
End Sub
Sub stepThreeEachLayerLeftToRight
layer(0).DigitalWrite(True)
For i = 0 To 2
column(i).DigitalWrite(False)
Next
Delay(100)
For i = 0 To 2
column(i).DigitalWrite(True)
Next
Delay(100)
For i = 3 To 5
column(i).DigitalWrite(False)
Next
Delay(100)
For i = 3 To 5
column(i).DigitalWrite(True)
Next
Delay(100)
For i = 6 To 8
column(i).DigitalWrite(False)
Next
Delay(100)
For i = 6 To 8
column(i).DigitalWrite(True)
Next
Delay(100)
layer(0).DigitalWrite(False)
layer(1).DigitalWrite(True)
For i = 0 To 2
column(i).DigitalWrite(False)
Next
Delay(100)
For i = 0 To 2
column(i).DigitalWrite(True)
Next
Delay(100)
For i = 3 To 5
column(i).DigitalWrite(False)
Next
Delay(100)
For i = 3 To 5
column(i).DigitalWrite(True)
Next
Delay(100)
For i = 6 To 8
column(i).DigitalWrite(False)
Next
Delay(100)
For i = 6 To 8
column(i).DigitalWrite(True)
Next
Delay(100)
layer(1).DigitalWrite(False)
layer(2).DigitalWrite(True)
For i = 0 To 2
column(i).DigitalWrite(False)
Next
Delay(100)
For i = 0 To 2
column(i).DigitalWrite(True)
Next
Delay(100)
For i = 3 To 5
column(i).DigitalWrite(False)
Next
Delay(100)
For i = 3 To 5
column(i).DigitalWrite(True)
Next
Delay(100)
For i = 6 To 8
column(i).DigitalWrite(False)
Next
Delay(100)
For i = 6 To 8
column(i).DigitalWrite(True)
Next
Delay(100)
layer(2).DigitalWrite(False)
End Sub
Sub stepThreeEveryLayerLeftToRight
For k = 0 To 5
layer(0).DigitalWrite(True)
For i = 0 To 2
column(i).DigitalWrite(False)
Next
Delay(100)
For i = 0 To 2
column(i).DigitalWrite(True)
Next
layer(0).DigitalWrite(False)
Delay(100)
layer(1).DigitalWrite(True)
For i = 0 To 2
column(i).DigitalWrite(False)
Next
Delay(100)
For i = 0 To 2
column(i).DigitalWrite(True)
Next
layer(1).DigitalWrite(False)
Delay(100)
layer(2).DigitalWrite(True)
For i = 0 To 2
column(i).DigitalWrite(False)
Next
Delay(100)
For i = 0 To 2
column(i).DigitalWrite(True)
Next
layer(2).DigitalWrite(False)
Delay(100)
layer(0).DigitalWrite(True)
For i = 3 To 5
column(i).DigitalWrite(False)
Next
Delay(100)
For i = 3 To 5
column(i).DigitalWrite(True)
Next
layer(0).DigitalWrite(False)
Delay(100)
layer(1).DigitalWrite(True)
For i = 3 To 5
column(i).DigitalWrite(False)
Next
Delay(100)
For i = 3 To 5
column(i).DigitalWrite(True)
Next
layer(1).DigitalWrite(False)
Delay(100)
layer(2).DigitalWrite(True)
For i = 3 To 5
column(i).DigitalWrite(False)
Next
Delay(100)
For i = 3 To 5
column(i).DigitalWrite(True)
Next
layer(2).DigitalWrite(False)
Delay(100)
layer(0).DigitalWrite(True)
For i = 6 To 8
column(i).DigitalWrite(False)
Next
Delay(100)
For i = 6 To 8
column(i).DigitalWrite(True)
Next
layer(0).DigitalWrite(False)
Delay(100)
layer(1).DigitalWrite(True)
For i = 6 To 8
column(i).DigitalWrite(False)
Next
Delay(100)
For i = 6 To 8
column(i).DigitalWrite(True)
Next
layer(1).DigitalWrite(False)
Delay(100)
layer(2).DigitalWrite(True)
For i = 6 To 8
column(i).DigitalWrite(False)
Next
Delay(100)
For i = 6 To 8
column(i).DigitalWrite(True)
Next
layer(2).DigitalWrite(False)
Delay(100)
Next
End Sub
Sub SpinCross
layer(0).DigitalWrite(True)
column(4).DigitalWrite(False)
For k = 0 To 10
column(0).DigitalWrite(False)
column(8).DigitalWrite(False)
Delay(100)
column(0).DigitalWrite(True)
column(8).DigitalWrite(True)
column(1).DigitalWrite(False)
column(7).DigitalWrite(False)
Delay(100)
column(1).DigitalWrite(True)
column(7).DigitalWrite(True)
column(2).DigitalWrite(False)
column(6).DigitalWrite(False)
Delay(100)
column(2).DigitalWrite(True)
column(6).DigitalWrite(True)
column(5).DigitalWrite(False)
column(3).DigitalWrite(False)
Delay(100)
column(5).DigitalWrite(True)
column(3).DigitalWrite(True)
Next
layer(0).DigitalWrite(False)
layer(1).DigitalWrite(True)
For k = 0 To 10
column(0).DigitalWrite(False)
column(8).DigitalWrite(False)
Delay(100)
column(0).DigitalWrite(True)
column(8).DigitalWrite(True)
column(1).DigitalWrite(False)
column(7).DigitalWrite(False)
Delay(100)
column(1).DigitalWrite(True)
column(7).DigitalWrite(True)
column(2).DigitalWrite(False)
column(6).DigitalWrite(False)
Delay(100)
column(2).DigitalWrite(True)
column(6).DigitalWrite(True)
column(5).DigitalWrite(False)
column(3).DigitalWrite(False)
Delay(100)
column(5).DigitalWrite(True)
column(3).DigitalWrite(True)
Next
layer(1).DigitalWrite(False)
layer(2).DigitalWrite(True)
For k = 0 To 10
column(0).DigitalWrite(False)
column(8).DigitalWrite(False)
Delay(100)
column(0).DigitalWrite(True)
column(8).DigitalWrite(True)
column(1).DigitalWrite(False)
column(7).DigitalWrite(False)
Delay(100)
column(1).DigitalWrite(True)
column(7).DigitalWrite(True)
column(2).DigitalWrite(False)
column(6).DigitalWrite(False)
Delay(100)
column(2).DigitalWrite(True)
column(6).DigitalWrite(True)
column(5).DigitalWrite(False)
column(3).DigitalWrite(False)
Delay(100)
column(5).DigitalWrite(True)
column(3).DigitalWrite(True)
Next
End Sub