Hey,
My attachInterrupt doesn't work correctly. First of all, here's my code:
//Libraries from here___________________________________________________________________________
#include <IRremote.h>
#include <Adafruit_NeoPixel.h>
//Constants from here___________________________________________________________________________
//Ledstrip
const int dinPin = 2;
const int numOfLeds = 288;
const int numOfLeds2 = (numOfLeds - 1);
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(numOfLeds, dinPin, NEO_GRB + NEO_KHZ800);
//Rainbow Calculations
const float a = 127.5;
const int b = 255;
const float pi = 3.1415926535897932384626433832795;
const float c_red = (pi / 3);
const float c_green = pi;
const float c_blue = (5 * pi / 3);
const float d = pi;
//Relay
int relay = 4;
volatile byte relayState = LOW;
//Remote
const int RECV_PIN = 3;
IRrecv irrecv(RECV_PIN);
decode_results results;
// RGB Color Values
float red = 0;
float green = 0;
float blue = 0;
float rgb = 0;
int red2 = 0;
int green2 = 0;
int blue2 = 0;
float red3 = 0;
float green3 = 0;
float blue3 = 0;
//Pixel Selection
volatile float increment = 0.033333333;
volatile float color = 0;
float whichled = 0;
volatile int mode = 10;
int fade = 0;
boolean up = true;
boolean left = true;
float night = 0;
int moment = 0;
float strength = 0;
int night2 = 0;
// Voids start here____________________________________________________________________________
void Colourm2() { //Calculate Color With Brightness
red = a + b * sin(c_red + d * color);
if (red < 0){ red = 0; } else if (red > 255){ red = 255; }
green = a + b * sin(c_green + d * color);
if (green < 0){ green = 0; } else if (green > 255){ green = 255; }
blue = a + b * sin(c_blue + d * color);
if (blue < 0){ blue = 0; } else if (blue > 255){ blue = 255; }
rgb = red + green + blue;
red2 = red * 255 / rgb;
green2 = green * 255 / rgb;
blue2 = blue * 255 / rgb;
}
void cycleDown() { //Cycles Through The Colors
color = (color - (increment * 5));
}
void cycleUp() { //Cycles Through The Colors
color = (color + (increment * 5));
}
void Fade() { //Cycles Through Brightness
if (up == true) {
fade = fade + 5;
if (fade >= 100) {
up = false;
}
} else if (up == false) {
fade = fade - 5;
if (fade <= 0) {
up = true;
}
}
pixels.setBrightness(fade);
}
void Mode() {
if (irrecv.decode(&results)){
switch(results.value){
case 0xFF629D: break; //^
case 0xFF22DD: cycleDown(); break; //<
case 0xFF02FD: if (relayState == LOW){relayState = HIGH;} else if (relayState == HIGH){relayState = LOW;} digitalWrite(relay,relayState); break; //OK
case 0xFFC23D: cycleUp(); break; //>
case 0xFFA857: break; //V
case 0xFF6897: mode = 1; break; //1
case 0xFF9867: mode = 2; break; //2
case 0xFFB04F: mode = 3; break; //3
case 0xFF30CF: mode = 4; break; //4
case 0xFF18E7: mode = 5; break; //5
case 0xFF7A85: mode = 6; break; //6
case 0xFF10EF: mode = 7; break; //7
case 0xFF38C7: mode = 8; break; //8
case 0xFF5AA5: break; //9
case 0xFF42BD: break; //*
case 0xFF4AB5: break; //0
case 0xFF52AD: break; //#
}
irrecv.resume();
}
}
void Show() { //Pushes RGB Values to Led
pixels.setPixelColor(whichled, pixels.Color(red2, green2, blue2));
pixels.show();
}
void troubleShooting() {
Serial.print("red ");
Serial.println(red2);
Serial.print("green ");
Serial.println(green2);
Serial.print("blue ");
Serial.println(blue2);
Serial.print("night ");
Serial.println(night);
Serial.print("whichled ");
Serial.println(whichled);
Serial.print("strength");
Serial.println(strength);
}
// Setup and loop from here__________________________________________________________________
void setup(){
pixels.begin(); // Initializes the NeoPixel library
pixels.setBrightness(100); // Set pixel Brightness
pinMode(relay, OUTPUT); // Sets relay pin to output
digitalWrite(relay, relayState); // Writes the first relay state
irrecv.enableIRIn(); // Enables the IR receiver
irrecv.blink13(true); // Enables the Led on the IR receiver
pinMode(RECV_PIN, INPUT_PULLUP);
attachInterrupt(1,Mode,CHANGE);
Serial.begin(9600); // Enables serial data output
randomSeed(analogRead(1)); // Enables randomness
}
void loop(){
while ((whichled < numOfLeds) && (mode == 1 )) { //Chasing Rainbow
Colourm2();
Show();
color = color + increment;
if (whichled == numOfLeds2) {
color = color + increment;
whichled = -1;
}
whichled++;
}
while ((whichled < numOfLeds) && (mode == 2 )) { //Full Rainbow
Colourm2();
Show();
if (whichled == numOfLeds2) {
color = color + increment;
whichled = -1;
}
whichled++;
}
while ((whichled < numOfLeds) && (mode == 3 )) { //Static Color
Colourm2();
Show();
if (whichled == numOfLeds2) {
whichled = -1;
}
whichled++;
}
while ((whichled < numOfLeds) && (mode == 4 )) { //Fade
Colourm2();
Show();
if (whichled == numOfLeds2) {
whichled = -1;
Fade();
}
whichled++;
}
while ((moment >= 0) && (mode == 5 )) { //Around the Globe
Colourm2();
red3 = red2;
green3 = green2;
blue3 = blue2;
while (night < 5) {
strength = (night / 5 + 0.2);
red2 = (red3 * strength);
green2 = (green3 * strength);
blue2 = (blue3 * strength);
night2 = night;
whichled = (moment + night2) % (numOfLeds);
Show();
night++;
delay(10);
}
night = -1;
moment++;
}
while ((moment >= 0) && (mode == 6 )) { //Night Rider
Colourm2();
red3 = red2;
green3 = green2;
blue3 = blue2;
while (night < 5) {
strength = (night / 5 + 0.2);
red2 = (red3 * strength);
green2 = (green3 * strength);
blue2 = (blue3 * strength);
night2 = night;
whichled = (numOfLeds - (abs(((moment + night2) % (numOfLeds * 2)) - (numOfLeds))));
Show();
night++;
delay(10);
}
night = -1;
moment++;
}
while ((moment >= 0) && (mode == 7 )) { //Around the Globe RGB
Colourm2();
red3 = red2;
green3 = green2;
blue3 = blue2;
while (night < 5) {
strength = (night / 5 + 0.2);
red2 = (red3 * strength);
green2 = (green3 * strength);
blue2 = (blue3 * strength);
night2 = night;
whichled = (moment + night2) % (numOfLeds);
Show();
night++;
delay(10);
}
night = -1;
moment++;
color = color + increment;
}
while ((moment >= 0) && (mode == 8 )) { //Night Rider RGB
Colourm2();
red3 = red2;
green3 = green2;
blue3 = blue2;
while (night < 5) {
strength = (night / 5 + 0.2);
red2 = (red3 * strength);
green2 = (green3 * strength);
blue2 = (blue3 * strength);
night2 = night;
whichled = (numOfLeds - (abs(((moment + night2) % (numOfLeds * 2)) - (numOfLeds))));
Show();
night++;
delay(10);
}
night = -1;
moment++;
color = (2 * whichled / 60);
}
}
It's quite a long code, designed to control a ws2812b led strip to have multiple modes of lighting.
It uses a power supply unit to power the strip (not the arduino), which is turned on and off with a relay.
An IR receiver receives commands from a remote, which are processed in the void "Mode".
The constants for the remote are annotated with // Remote, the behaviour of the pin (including the interrupt) is defined in the void "setup".
I'd like the strip to act the following way. I press a button on the remote, the receiver registers, and tells the arduino to change the mode of the strip. The mode of the strip changes almost immediately.
How it currently behaves: The remote works while mode == 0, so when the led strip isn't displaying. While this is true, I can turn on and off the relay, and with it the power supply. When mode is not 0, so the led strip is displaying something, the arduino no longer responds to the remote. The light on the IR receiver does light up.
When I remove the attachInterrupt from the script, the arduino doens't respond at all to the remote, which is to be expected.