Ok, so I'm having some problems putting the finishing touches on the latest project. The Idea is to have an Infrared receiver mounted on a single Arduino Uno, taking ques from a remote to select preprogramed patterns on a Neopixel strip (selection from the Neopixel Strand test)
Here is some code...
//Always comment your code like a violent psychopath will be maintaining it and they know where you live
#include <Adafruit_NeoPixel.h> //Neopixel Library
#include <IRLibAll.h> //IR reciever Library
IRrecvLoop myReceiver(2); //IR receiver on IO pin 2
IRdecode myDecoder; //Decoder object
unsigned long oldCode = 00000000; //Comparison variable to evaluate the execution of the 'Check' function later
Adafruit_NeoPixel strip (3,3,NEO_RGBW + NEO_KHZ800); //Creates the Pixel strip as an object in the code
void setup() {
strip.begin(); //Initialise the Neopixel strip
strip.show(); //Initialise all pixels to 'off'
myReceiver.enableIRIn(); // Start the IR receiver
}
void loop() {
check(); //Run the check function
delay(20); //Delay before running the loop again by 20 milliseconds giving time to recieve signals
}
void check() { //check Function: Checks for an IR signal before nominating which of the test displays to run
if (oldCode = myDecoder.value){ //Evaluates if the IR code recieved from the remote matches 'oldCode' and if it does....
return; //Terminates the check Function returning its values
}
if (myReceiver.getResults()) {
myDecoder.decode();
if (myDecoder.protocolNum == NEC) {
switch(myDecoder.value) { //Activate this switch statement based on the value 'myDecoder' holds
case 0xFFA25D: Serial.println("Untethered button, please select from 0-8"); break;
case 0xFFE21D: Serial.println("Untethered button, please select from 0-8"); break;
case 0xFF629D: Serial.println("Untethered button, please select from 0-8"); break;
case 0xFF22DD: Serial.println("Untethered button, please select from 0-8"); break;
case 0xFF02FD: Serial.println("Untethered button, please select from 0-8"); break;
case 0xFFC23D: Serial.println("Untethered button, please select from 0-8"); break;
case 0xFFE01F: Serial.println("Untethered button, please select from 0-8"); break;
case 0xFFA857: Serial.println("Untethered button, please select from 0-8"); break;
case 0xFF906F: Serial.println("Untethered button, please select from 0-8"); break;
case 0xFF9867: Serial.println("Untethered button, please select from 0-8"); break;
case 0xFFB04F: Serial.println("Untethered button, please select from 0-8"); break;
case 0xFF6897:
colorWipe(strip.Color( 0, 0, 0), 50); // Black/off "0"
Serial.println("0 - Black/off");
break;
case 0xFF30CF:
colorWipe(strip.Color(255, 0, 0), 50); // Red "1"
Serial.println("1 - All red");
break;
case 0xFF18E7:
colorWipe(strip.Color( 0, 255, 0), 50); // Green "2"
Serial.println("2 - All green");
break;
case 0xFF7A85:
colorWipe(strip.Color( 0, 0, 255), 50); // Blue "3"
Serial.println("3 - All blue");
break;
case 0xFF10EF:
theaterChase(strip.Color(127, 127, 127), 50); // White "4"
Serial.println("4 - All white");
break;
case 0xFF38C7:
theaterChase(strip.Color(127, 0, 0), 50); // Red "5"
Serial.println("5");
break;
case 0xFF5AA5:
theaterChase(strip.Color( 0, 0, 127), 50); // Blue "6"
Serial.println("6");
break;
case 0xFF42BD:
rainbow(10); // "7"
Serial.println("7");
break;
case 0xFF4AB5:
theaterChaseRainbow(50); // "8"
Serial.println("8");
break;
case 0xFF52AD: Serial.println("Untethered button, please select from 0-8"); break;
case 0xFFFFFFFF: Serial.println("Please release button and reselect"); break;
}
}
oldCode = myDecoder.value; //make the new button state equal the old buttonstate preventing the button from activating if statement
}
}
void colorWipe(uint32_t color, int wait) { //Colour wipe test
while(true) {
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
strip.setPixelColor(i, color); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
check();
delay(wait); // Pause for a moment
}
}
}
void theaterChase(uint32_t color, int wait) { //Theatre Chase test
while(true) {
for(int a=0; a<10; a++) { // Repeat 10 times...
for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
strip.clear(); // Set all pixels in RAM to 0 (off)
for(int c=b; c<strip.numPixels(); c += 3) { // 'c' counts up from 'b' to end of strip in steps of 3...
strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
check(); //Run the check function
}
strip.show(); // Update strip with new contents
delay(wait); // Pause for a moment
}
}
}
}
void rainbow(int wait) { //Rainbow test function
while(true) { //while this function is active
for(long firstPixelHue = 0; firstPixelHue < 3*65536; firstPixelHue += 256) { //Sets differing colours for the rainbow
for(int i=0; i<strip.numPixels(); i++) { //For each pixel in strip...
int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels()); //balances the colour pattern along the length of the strip
strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue))); //run the colour pattern along the strip
check(); //run the check function
}
strip.show(); //Update strip with new contents
delay(wait); //Pause for a moment
}
}
}
void theaterChaseRainbow(int wait) {
while(true) {
int firstPixelHue = 0; //First pixel starts at red
for(int a=0; a<30; a++) { //Repeat 30 times...
for(int b=0; b<3; b++) { //'b' counts from 0 to 2...
strip.clear(); //Set all pixels to off
for(int c=b; c<strip.numPixels(); c += 3) { //'c' counts up from 'b' to end of strip in increments of 3, hue of pixel 'c' is offset by an amount to make one full revolution of the color wheel (range 65536) along the length of the strip (strip.numPixels() steps):
int hue = firstPixelHue + c * 65536L / strip.numPixels(); //create the hue variable and balance the rainbow colouring across the strip
uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB
strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
check(); //Run the check function
}
strip.show(); // Update strip with new contents
delay(wait); // Pause for a moment
firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
}
}
}
}
... and here is a picture of the build...
the IR sensor plugs into IO pin 2 on the Arduino, 5V and GND on the Breadboard; The Neopixel Strip plugs into IO pin 3 and GND on the Arduino and 5V on the board.
The Problems
I have tried to write a loop that uses a class call to decide what is going on ("check"), I have also written another version where the switch statement is place in the loop instead. The problem from what I understand is that the IR input cannot receive whilst the loop is running (as it cannot interrupt) yet the test patterns will not play unless the loop is running. I have looked at solutions that involve using two Arduino's with either one running IR and the Neopixel respectively but unfortunately this will not match up with my brief - I need to have it running on one Arduino.
Any Ninja's out there have any ideas?