B4R Code Snippet Control LED brightness via 1838 IR receiver

SubName: Use a standard remote control to turn up/down the brightness of an LED.
Description: You can use this basic code to control an LED (or anything else for that matter) that you want with a standard IR receiver. I was using a single off board VS1838B/TL1838 38KHz IR receiver with a Samsung TV remote control. Using the PWM (Pulse Width Modulation) pins is essential for controlling the brightness.

B4X:
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
    Public GreenLED As Pin
    Public ReadIR As IrReceive
    Public LEDVal As Byte
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")

    GreenLED.Initialize(5, GreenLED.MODE_OUTPUT)    'Pin 5
    GreenLED.AnalogWrite(LEDVal)

    ReadIR.Initialize(6, "IrReceive_Decoded")    'Pin 6
    ReadIR.Enable
End Sub

Sub IrReceive_Decoded (Result As IrResult)
    'Log("IR receive code = ", Result.Value)

    Select Result.Value
        Case "3772833823"    'Samsung TV volume + Button
            If LEDVal <= 250 Then LEDVal = LEDVal + 5 '255 Max
            GreenLED.AnalogWrite(LEDVal)
        Case "3772829743"    'Samsung TV volume - Button
            If LEDVal >= 5 Then LEDVal = LEDVal - 5 '0 Min
            GreenLED.AnalogWrite(LEDVal)
        Case Else
            Log("Unknown code...")
    End Select
  
    'Log("VAL = ", LEDVal)
End Sub

Tags: PWM, IR, Receiver, Arduino, LED, Resistor

Remote Controlled LED_bb.png
 
Last edited:
Top