Arduino Ledstrip With Remote Controler
by cleoboonstra in Circuits > Arduino
1018 Views, 2 Favorites, 0 Comments
Arduino Ledstrip With Remote Controler
This tutorial is about controlling a led strip with an Arduino
and an IR remote controller.
Supplies
- Arduino Uno
- Led strip: only WS2811
- IR remote and receiver
- 470Ω resistor
Wiring
See the picture for the wiring.
Download Libraries
Download the adafruit neopixal library:
https://adafruit.github.io/Adafruit_NeoPixel/html/index.html
And download the IRremote library:
https://www.arduino.cc/reference/en/libraries/irremote/
Now add the libraries to your Arduino. See the picture and add the zipfiles to your Arduino.
Define and Setup
Now add the following code:
#include <IRremote.h> #include <Adafruit_NeoPixel.h> #define PIN 6 #define RECV_PIN 7 Adafruit_NeoPixel strip = Adafruit_NeoPixel(30, PIN, NEO_GRB + NEO_KHZ800); IRrecv irrecv(RECV_PIN); decode_results results; void setup() { strip.begin(); strip.show(); // inilatation of all LEDs Serial.begin(9600); // Start serial irrecv.enableIRIn(); // Start the receiver Serial.println("Enabled IRin"); } void loop() { }
Find Channels
Add the following code to your loop:
void loop() { if (irrecv.decode(&results)){ int value = results.value; Serial.println(value); } }
Now you print the values (in your serial monitor) of the remote controller when a button is pressed. So you can find out the value corresponding to every button. For example, you upload your code to your Arduino and opened the serial monitor, when you press now button 1, there will be print a value like -6789. Do this for all your buttons and write then down (like the picture above).
Print Button Values
Now add switch statements to get your serial monitor to print which button is pressed. Notice the values can be different for your specific remote controller.
void loop() { if (irrecv.decode(&results)){ int value = results.value; //Serial.println(value); switch(value){ case -6789: //Keypad button "1" Serial.println("1"); } switch(value){ case 15355: //Keypad button "2" Serial.println("2"); } switch(value){ case 5699: //Keypad button "3" Serial.println("3"); } switch(value){ case -16833: //Keypad button "4" Serial.println("4"); } switch(value){ case -7177: //Keypad button "5" Serial.println("5"); } switch(value){ case 539: //Keypad button "6" Serial.println("6"); } switch(value){ case 25979: //Keypad button "7" Serial.println("7"); } switch(value){ case 15547: //Keypad button "8" Serial.println("8"); } switch(value){ case -6241: //Keypad button "9" Serial.println("9"); } switch(value){ case 5499: //Keypad button "0" Serial.println("0"); } switch(value){ case -521: //Keypad button "star" Serial.println("star"); } switch(value){ case -997: //Keypad button "hashtag" Serial.println("hastag"); } switch(value){ case 7611: //Keypad button "arrow up" Serial.println("arrow up"); } switch(value){ case 19899: //Keypad button "arrow right" Serial.println("arrow right"); } switch(value){ case -4645: //Keypad button "arrow down" Serial.println("arrow down"); } switch(value){ case -11233: //Keypad button "arrow left" Serial.println("arrow left"); } switch(value){ case 19227: //Keypad button "ok" Serial.println("ok"); } irrecv.resume(); } }
Add Light Effects
Now we get to the fun part. I used the following website for the color effects: https://www.tweaking4all.nl/hardware/arduino/arduino-ws2812-led/
Add their code to your own code and customize it the way you want.
// Put leds on void colorWipe(uint32_t c, uint8_t wait) { for(uint16_t i=0; i<strip.numPixels(); i++) { strip.setPixelColor(i, c); strip.show(); delay(wait); } } uint32_t Wheel(byte WheelPos) { if(WheelPos < 85) { return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); } else if(WheelPos < 170) { WheelPos -= 85; return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); } else { WheelPos -= 170; return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); } } void rainbow(uint8_t wait) { uint16_t i, j; for(j=0; j<256; j++) { for(i=0; i<strip.numPixels(); i++) { strip.setPixelColor(i, Wheel((i+j) & 255)); } strip.show(); delay(wait); } } void rainbowCycle(uint8_t wait) { uint16_t i, j; for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel for(i=0; i< strip.numPixels(); i++) { strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255)); } strip.show(); delay(wait); } } void theaterChaseRainbow(uint8_t wait) { for (int j=0; j < 256; j++) { // gebruik alle 256 kleuren in het wiel for (int q=0; q < 3; q++) { for (int i=0; i < strip.numPixels(); i=i+3) { strip.setPixelColor(i+q, Wheel( (i+j) % 255)); //zet elke 3de LED aan } strip.show(); delay(wait); for (int i=0; i < strip.numPixels(); i=i+3) { strip.setPixelColor(i+q, 0); //zet elke 3de LED uit } } } }
Don't forget to add it to your switch statements. For example:
switch(value){ case -6789: //Keypad button "1" Serial.println("1"); colorWipe(strip.Color(255, 0, 0), 50); // Red }
Now you are done!