Skip to content

Commit eae4509

Browse files
authored
Merge pull request #2958 from FoamyGuy/sparkle_motion_examples
sparkle motion IR control neopixels examples
2 parents 7a43be2 + 62f367b commit eae4509

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

Sparkle_Motion_Examples/Arduino_Sparkle_Motion_IR_Remote/.feather_esp32_v2.test.only

Whitespace-only changes.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries
2+
// SPDX-FileCopyrightText: 2025 Tim Cocks for Adafruit Industries
3+
//
4+
// SPDX-License-Identifier: MIT
5+
6+
/*
7+
* Based on the SimpleReceiver.cpp and SimpleSender.cpp from the
8+
* Arduino-IRremote https://github.com/Arduino-IRremote/Arduino-IRremote.
9+
* by Armin Joachimsmeyer
10+
************************************************************************************
11+
* MIT License
12+
*
13+
* Copyright (c) 2020-2023 Armin Joachimsmeyer
14+
*
15+
*/
16+
17+
#include <Arduino.h>
18+
19+
#include <IRremote.hpp> // include the library
20+
#include <Adafruit_NeoPixel.h>
21+
22+
#define NEOPIXEL_STRIP_PIN 21
23+
#define NUM_PIXELS 8
24+
25+
#define IR_RECEIVE_PIN 32
26+
27+
Adafruit_NeoPixel NEOPIXEL_STRIP(NUM_PIXELS, NEOPIXEL_STRIP_PIN, NEO_GRB + NEO_KHZ800);
28+
29+
uint8_t upCmd = 0x5;
30+
uint8_t downCmd = 0xD;
31+
uint8_t rightCmd = 0xA;
32+
uint8_t leftCmd = 0x8;
33+
34+
uint16_t pixelHue = 0;
35+
uint8_t brightness = 25;
36+
37+
void setup() {
38+
Serial.begin(115200);
39+
while (!Serial)
40+
;
41+
Serial.println("Adafruit Sparkle Motion IR Remote Control NeoPixels Demo");
42+
IrReceiver.begin(IR_RECEIVE_PIN);
43+
Serial.print("IRin on pin ");
44+
Serial.print(IR_RECEIVE_PIN);
45+
NEOPIXEL_STRIP.begin();
46+
NEOPIXEL_STRIP.setBrightness(25);
47+
}
48+
49+
void loop() {
50+
/*
51+
* Check if received data is available and if yes, try to decode it.
52+
* When left or right buttons are pressed, change the pixelHue.
53+
* When up or down buttons are pressed, change the brightness.
54+
*/
55+
if (IrReceiver.decode()) {
56+
if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
57+
Serial.println("unknown");
58+
IrReceiver.printIRResultRawFormatted(&Serial, true);
59+
IrReceiver.resume();
60+
} else {
61+
IrReceiver.resume();
62+
//IrReceiver.printIRResultShort(&Serial);
63+
64+
// Ignore repeat codes from holding down the button
65+
if (IrReceiver.decodedIRData.flags == 0){
66+
//Serial.printf("Command: %d\n",IrReceiver.decodedIRData.command);
67+
if (IrReceiver.decodedIRData.command == upCmd){
68+
Serial.println("UP btn");
69+
brightness = min(brightness + 25, 255);
70+
}else if (IrReceiver.decodedIRData.command == downCmd){
71+
Serial.println("DOWN btn");
72+
brightness = max(brightness - 25, 0);
73+
}else if (IrReceiver.decodedIRData.command == leftCmd){
74+
Serial.println("LEFT btn");
75+
pixelHue = (pixelHue - 8192) % 65536;
76+
}else if (IrReceiver.decodedIRData.command == rightCmd){
77+
Serial.println("RIGHT btn");
78+
pixelHue = (pixelHue + 8192) % 65536;
79+
}
80+
81+
NEOPIXEL_STRIP.setBrightness(brightness);
82+
NEOPIXEL_STRIP.fill(NEOPIXEL_STRIP.gamma32(NEOPIXEL_STRIP.ColorHSV(pixelHue)));
83+
NEOPIXEL_STRIP.show();
84+
delay(100);
85+
}
86+
}
87+
Serial.println();
88+
}
89+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-FileCopyrightText: 2025 Tim Cocks for Adafruit Industries
3+
# SPDX-License-Identifier: MIT
4+
"""CircuitPython Adafruit Sparkle Motion IR remote control NeoPixels example."""
5+
import time
6+
7+
import board
8+
import pulseio
9+
from rainbowio import colorwheel
10+
11+
import neopixel
12+
import adafruit_irremote
13+
14+
pulsein = pulseio.PulseIn(board.IR, maxlen=120, idle_state=True)
15+
decoder = adafruit_irremote.NonblockingGenericDecode(pulsein)
16+
17+
IR_CODE_UP = (0, 253, 160, 95)
18+
IR_CODE_DOWN = (0, 253, 176, 79)
19+
20+
IR_CODE_RIGHT = (0, 253, 80, 175)
21+
IR_CODE_LEFT = (0, 253, 16, 239)
22+
23+
t0 = next_heartbeat = time.monotonic()
24+
25+
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
26+
27+
brightness = 1
28+
pixel.brightness = brightness / 10
29+
30+
color_number = 160
31+
pixel.fill(colorwheel(color_number))
32+
while True:
33+
for message in decoder.read():
34+
print(f"t={time.monotonic() - t0:.3} New IR Message")
35+
if isinstance(message, adafruit_irremote.IRMessage):
36+
if message.code == IR_CODE_UP:
37+
brightness = min(brightness + 1, 10)
38+
elif message.code == IR_CODE_DOWN:
39+
brightness = max(brightness - 1, 0)
40+
elif message.code == IR_CODE_RIGHT:
41+
color_number = (color_number + 32) % 256
42+
elif message.code == IR_CODE_LEFT:
43+
color_number = (color_number - 32) % 256
44+
45+
pixel.brightness = brightness / 10
46+
pixel.fill(colorwheel(color_number))
47+
48+
print("Decoded:", message.code)
49+
print("Brightness: ", brightness/10, " Color: ", hex(colorwheel(color_number)))
50+
elif isinstance(message, adafruit_irremote.NECRepeatIRMessage):
51+
print("NEC repeat!")
52+
elif isinstance(message, adafruit_irremote.UnparseableIRMessage):
53+
print("Failed to decode", message.reason)
54+
print("----------------------------")

0 commit comments

Comments
 (0)