Skip to content

Instantly share code, notes, and snippets.

@bennamallory
Created April 9, 2021 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennamallory/e9ef30d979bf2924127909908def35cc to your computer and use it in GitHub Desktop.
Save bennamallory/e9ef30d979bf2924127909908def35cc to your computer and use it in GitHub Desktop.
/* Author: Mallory Benna
* Date: April 8, 2021
* Purpose: To change neopixel display with IR Remote
* References: IR - https://github.com/Arduino-IRremote/Arduino-IRremote/blob/master/examples/ReceiveDemo/ReceiveDemo.ino#L38
*
*/
#include <Adafruit_NeoPixel.h> //import NeoPixel library
#include <Arduino.h>
#include <IRremote.h> //Import IR Remote library
#define LED_PIN 8 //Pin connected to NeoPixel on Arduino
#define LED_COUNT 256 // Number of NeoPixels attached to Arduiino
// Define NeoPixel Object
// LED_COUNT = Number of pixels in NeoPixel strip
// LED_PIN = Arduino pin number
// Pixel type flags:
// NEO_GRB = Pixels wired fro GRB bitstream
// NEO_KHZ800 = 800 KHz bitstream (most NeoPixel products with WS2812 LEDs)
Adafruit_NeoPixel neoPix(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
int IR_RECEIVE_PIN = 12; //Pin declaration for IR Remote
//Structure to hold mapped values from Excel
struct MappedVals{
int location;
int R;
int G;
int B;
};
void setup() {
Serial.begin(9600);
neoPix.begin(); //Initialize the neoPixel
neoPix.show(); //Turn all pixels 'off'
/*
* Start the receiver, enable feedback LED and take LED feedback pin from the internal boards definition
*/
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
}
void loop() {
/*
* Check if received data is available and if yes, try to decode it.
* Decoded result is in the IrReceiver.decodedIRData structure.
*
* E.g. command is in IrReceiver.decodedIRData.command
* address is in command is in IrReceiver.decodedIRData.address
* and up to 32 bit raw data in IrReceiver.decodedIRData.decodedRawData
*/
if (IrReceiver.decode()) {
// Print a short summary of received data
IrReceiver.printIRResultShort(&Serial);
if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
// We have an unknown protocol here, print more info
IrReceiver.printIRResultRawFormatted(&Serial, true);
}
Serial.println();
/*
* !!!Important!!! Enable receiving of the next value,
* since receiving has stopped after the end of the current received data packet.
*/
IrReceiver.resume(); // Enable receiving of the next value
/*
* Finally, check the received data and perform actions according to the received command
*/
if (IrReceiver.decodedIRData.command == 0x45) { //power button
Serial.println("hello");
X();
} else if (IrReceiver.decodedIRData.command == 0x46) { //vol up
Serial.println("second");
onePix();
}
}
}
//Function to map Excel Image to NeoPixel Index from: https://www.youtube.com/watch?v=A_S3LAUQHwU
MappedVals mapLEDXY(int y, int x, int RED, int GREEN, int BLUE) {
int RGBlocation = 0;
if (y % 2 == 0) { //even column
RGBlocation = x + y * 16;
} else { //odd column
RGBlocation = 15 - x + y * 16;
}
MappedVals valuesMap; //instantiate new struct
//set struct values
valuesMap.location = RGBlocation;
valuesMap.R = RED;
valuesMap.G = GREEN;
valuesMap.B = BLUE;
return valuesMap;
}
void X() {
neoPix.clear();
MappedVals mappedValuesArr[] = { mapLEDXY(0, 0, 0, 0, 10), mapLEDXY(0, 15, 0, 0, 10), mapLEDXY(1, 1, 0, 0, 10),
mapLEDXY(1, 14, 0, 0, 10), mapLEDXY(2, 2, 0, 0, 10), mapLEDXY(2, 13, 0, 0, 10),
mapLEDXY(3, 3, 0, 0, 10), mapLEDXY(3, 12, 0, 0, 10), mapLEDXY(4, 4, 0, 0, 10),
mapLEDXY(4, 11, 0, 0, 10), mapLEDXY(5, 5, 0, 0, 10), mapLEDXY(5, 10, 0, 0, 10),
mapLEDXY(6, 6, 0, 0, 10), mapLEDXY(6, 9, 0, 0, 10), mapLEDXY(7, 7, 0, 0, 10),
mapLEDXY(7, 8, 0, 0, 10), mapLEDXY(8, 7, 0, 0, 10), mapLEDXY(8, 8, 0, 0, 10),
mapLEDXY(9, 6, 0, 0, 10), mapLEDXY(9, 9, 0, 0, 10), mapLEDXY(10, 5, 0, 0, 10),
mapLEDXY(10, 10, 0, 0, 10), mapLEDXY(11, 4, 0, 0, 10), mapLEDXY(11, 11, 0, 0, 10),
mapLEDXY(12, 3, 0, 0, 10), mapLEDXY(12, 12, 0, 0, 10), mapLEDXY(13, 2, 0, 0, 10),
mapLEDXY(13, 13, 0, 0, 10), mapLEDXY(14, 1, 0, 0, 10), mapLEDXY(14, 14, 0, 0, 10),
mapLEDXY(15, 0, 0, 0, 10), mapLEDXY(15, 15, 0, 0, 10) };
for(int i=0; i < 32; i++){
neoPix.setPixelColor(mappedValuesArr[i].location,mappedValuesArr[i].R,mappedValuesArr[i].G,mappedValuesArr[i].B);
neoPix.show();
}
delay(10);
}
void onePix() {
neoPix.clear();
neoPix.setPixelColor(11,255,0,255);
neoPix.show();
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment