Sub Process_Globals
Public Serial1 As Serial
Private Strip1 As NSRainbow ' rNSRainbow Library, WS2812B LED strip
Private tmrProgram_1 As Timer ' fast timer
Private tmrProgram_2 As Timer ' slow timer
Private tmrIR As Timer ' reads the IR and selects one of the program timers
Private ProgNum As Int = 0 ' current program
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
Strip1.Initialize(60, 8) ' no problem if initialized for 8 LEDs, but I need 60 LEDs
Strip1.SetBrightness(100)
Strip1.Clear(60)
RunNative("enableIRIn", Null) ' use C code instead of library
tmrProgram_1.Initialize("tmrProgram_1_Tick", 20) ' problem when less than 80 or so
tmrProgram_1.Enabled = False
tmrProgram_2.Initialize("tmrProgram_2_Tick", 150) ' no problems at this speed
tmrProgram_2.Enabled = False
tmrIR.Initialize("tmrIR_Tick", 100) ' speed doesn't seem to matter
tmrIR.Enabled = True
End Sub
' This timer demonstrates the problem where tmrIR can't correctly read IR
' because the timer interval is too fast (about 80 or faster).
Private Sub tmrProgram_1_tick
Strip1.SetColor(1, 0, 0, 200) ' turn LED red
Strip1.Show ' If this line is commented out then the IR works
End Sub
' This timer demonstrates that a slower interval (80 or slower) doesn't interfere
' with tmrIR reading the IR
Private Sub tmrProgram_2_Tick
Strip1.SetColor(1, 200, 0, 0) ' turn LED blue
Strip1.Show
End Sub
Sub tmrIR_Tick
Dim Result As UInt = RunNative("read", Null)
If Result > 0 Then
tmrProgram_1.Enabled = False ' disable timers
tmrProgram_2.Enabled = False ' disable timers
Select Result
Case 41565 ' IR button 1 was pressed
Log("Program 1")
ProgNum = 1
Case 25245 ' IR button 2 was pressed
Log("Program 2")
ProgNum = 2
Case Else ' junk from IR
Log("Else, Result: ", Result)
End Select
' enable timer of the selected program
Select ProgNum
Case 1
tmrProgram_1.Enabled = True
Case 2
tmrProgram_2.Enabled = True
End Select
End If
End Sub
' IR
#if C
#include "IRremote.h"
IRrecv irrecv(4); //signal pin. Change as needed
decode_results results;
B4R::Object res;
void enableIRIn(B4R::Object* o) {
irrecv.enableIRIn();
}
B4R::Object* read(B4R::Object* o) {
ULong result = 0;
if (irrecv.decode(&results)) {
irrecv.resume();
//b4r_main::_lastread = millis();
result = results.value;
}
return res.wrapNumber(result);
}
#end if