Hello guys, im trying to run a code in wich i can change the pattern in a Led ring, my problem is that while a pattern is running the arduino won´t recieve another command.
i´ll leave my code below
#include <IRremote.h>
#include <Adafruit_NeoPixel.h>
#define PIN 9
#define NUMPIXELS 16 // Numero de leds
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_RGB + NEO_KHZ800 );
int delayval = 100; // Tempo de delay
// Definir porta para ligar o sensorIR
int RECV_PIN = 2;
// Inicialização de variáveis a partir da libraria
IRrecv irrecv(RECV_PIN);
decode_results results;
int X=255;
int Time = 10;
int Speed = 100;
void setup()
{
pixels.begin();
Serial.begin(9600);
irrecv.enableIRIn();
}
void loop()
{
if(irrecv.decode(&results))
{
unsigned int value = results.value; // Atribui o valor numérico ao valor recebido para ser utilizado no switch case
switch(value)
{
case 0xFFA25D:
Serial.println(" On ");
X = 255;
Time = 10;
Speed = 100;
colorWipe(pixels.Color( 0, X, 0), Speed);
break;
case 0xFF629D:
Serial.println(" timer ");
if (Time > 1000){
Time = 10 ;
} else{
Time = Time + 50;
}
if (Time < 255){
colorWipe(pixels.Color( Time, 0, 0), 50);
}
else if (Time > 255 && Time < 510){
colorWipe(pixels.Color( Time - 255 ,Time - 255 , 0), 50);
}
else if (Time > 510 && Time < 765){
colorWipe(pixels.Color( Time - 579 , Time - 510, 0), 50);
}
else if (Time > 765 && Time < 959){
colorWipe(pixels.Color( 0, Time - 765, 0), 50);
}
else if (Time > 959 && Time < 1000){
colorWipe(pixels.Color( 0, 0, 255), 50);
}
break;
case 0xFFE21D:
Serial.println(" off ");
X = 255;
Time = 10;
Speed= 100;
colorWipe(pixels.Color( 0, 0, 0), 50);
break;
case 0xFF22DD:
Serial.println(" R ");
colorWipe(pixels.Color( 0, X, 0), 50);
break;
case 0xFF02FD:
Serial.println(" G ");
colorWipe(pixels.Color( X, 0, 0), 50);
break;
case 0xFFC23D:
Serial.println(" B ");
colorWipe(pixels.Color( 0, 0, X), 50);
break;
case 0xFFE01F:
Serial.println(" v1 ");
int R,G,B;
if (Speed > 100){
Speed = 20 ;
} else{
Speed = Speed + 10;
}
R= random(255);
G= random(255);
B= random(255);
colorWipe(pixels.Color( G, R, B), Speed);
break;
case 0xFFA857:
Serial.println(" v2 ");
Serial.println(" timer ");
if (Time > 10){
Time = 1000 ;
} else{
Time = Time - 50;
}
if (Time < 255){
colorWipe(pixels.Color( Time, 0, 0), 50);
}
else if (Time > 255 && Time < 510){
colorWipe(pixels.Color( Time - 255 ,Time - 255 , 0), 50);
}
else if (Time > 510 && Time < 765){
colorWipe(pixels.Color( Time - 579 , Time - 510, 0), 50);
}
else if (Time > 765 && Time < 1000){
colorWipe(pixels.Color( 0, Time - 765, 0), 50);
}
break;
case 0xFF906F:
Serial.println(" v3 ");
if (Speed < 20){
Speed = 100 ;
} else{
Speed = Speed - 10;
}
R= random(255);
G= random(255);
B= random(255);
colorWipe(pixels.Color( G, R, B), Speed);
break;
case 0xFF6897:
Serial.println(" a1 ");
R= random(X);
G= random(X);
B= random(X);
colorWipe(pixels.Color( G, R, B), Speed);
break;
case 0xFF9867:
Serial.println(" a2 ");
for (int y =0;y<Time;y++){
RGBloop(Time);
}
break;
case 0xFFB04F:
Serial.println(" a3 ");
for (int y =0;y<Time;y++){
theaterChase(pixels.Color( 0, X, 0), Speed); // Blue
theaterChase(pixels.Color( 0, 0, X), Speed);
}
break;
case 0xFF30CF:
Serial.println(" m1 ");
colorWipe(pixels.Color( 20, 255, 147), Speed);
break;
case 0xFF18E7:
Serial.println(" m2 ");
colorWipe(pixels.Color( 234, 34, 255), Speed);
break;
case 0xFF7A85:
Serial.println(" m3 ");
for(int y=0;y<Time;y++){
theaterChase(pixels.Color( X, X, X), Speed);
}
break;
case 0xFF10EF:
Serial.println(" blanco ");
for (int y =0;y<Time;y++){
R= random(255);
G= random(255);
B= random(255);
theaterChase(pixels.Color( G, R, B), Speed); // Blue
}
break;
case 0xFF38C7:
Serial.println(" fade ");
for (int y=0;y<Time;y++){
rainbow(10);
}
break;
case 0xFF5AA5:
Serial.println(" jump ");
for (int y;y<Time;y++){
theaterChaseRainbow(Speed);
}
break;
case 0xFF42BD:
Serial.println(" reset ");
X= 255;
Time = 10;
Speed = 100;
colorWipe(pixels.Color( X, X, X), Speed);
break;
case 0xFF4AB5:
Serial.println(" glow down ");
X= X-50;
colorWipe(pixels.Color( X, X, X), 50);
break;
case 0xFF52AD:
Serial.println(" glow up ");
X= X+50;
colorWipe(pixels.Color( X, X, X), 50);
break;
default:
Serial.println(" other button ");
break;
}
}
irrecv.resume(); // Leitura do próximo valor emitido pelo comando
delay(500);
}
void colorWipe(uint32_t color, int wait) {
for(int i=0; i<pixels.numPixels(); i++) { // For each pixel in strip...
pixels.setPixelColor(i, color); // Set pixel's color (in RAM)
pixels.show(); // Update strip to match
delay(wait); // Pause for a moment
}
}
void theaterChase(uint32_t color, int wait) {
for(int a=0; a<10; a++) { // Repeat 10 times...
for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
pixels.clear(); // Set all pixels in RAM to 0 (off)
// 'c' counts up from 'b' to end of strip in steps of 3...
for(int c=b; c<pixels.numPixels(); c += 3) {
pixels.setPixelColor(c, color); // Set pixel 'c' to value 'color'
}
pixels.show(); // Update strip with new contents
delay(wait); // Pause for a moment
}
}
}
void rainbow(int wait) {
for(long firstPixelHue = 0; firstPixelHue < 3*65536; firstPixelHue += 256) {
for(int i=0; i<pixels.numPixels(); i++) { // For each pixel in strip...
int pixelHue = firstPixelHue + (i * 65536L / pixels.numPixels());
pixels.setPixelColor(i, pixels.gamma32(pixels.ColorHSV(pixelHue)));
}
pixels.show(); // Update strip with new contents
delay(wait); // Pause for a moment
}
}
// Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames.
void theaterChaseRainbow(int wait) {
int firstPixelHue = 0; // First pixel starts at red (hue 0)
for(int a=0; a<30; a++) { // Repeat 30 times...
for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
pixels.clear(); // Set all pixels in RAM to 0 (off)
// 'c' counts up from 'b' to end of strip in increments of 3...
for(int c=b; c<pixels.numPixels(); c += 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 / pixels.numPixels();
uint32_t color = pixels.gamma32(pixels.ColorHSV(hue)); // hue -> RGB
pixels.setPixelColor(c, color); // Set pixel 'c' to value 'color'
}
pixels.show(); // Update strip with new contents
delay(wait); // Pause for a moment
firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
}
}
//olor wheel has a range of 65536 but it's OK if we roll over, so
// just count from 0 to 365536. Adding 256 to firstPixelHue each time
// means we'll make 365536/256 = 768 passes through this outer loop:
for(long firstPixelHue = 0; firstPixelHue < 3*65536; firstPixelHue += 256) {
for(int i=0; i<pixels.numPixels(); i++) { // For each pixel in strip...
// Offset pixel hue by an amount to make one full revolution of the
// color wheel (range of 65536) along the length of the strip
// (strip.numPixels() steps):
int pixelHue = firstPixelHue + (i * 65536L / pixels.numPixels());
// strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
// optionally add saturation and value (brightness) (each 0 to 255).
// Here we're using just the single-argument hue variant. The result
// is passed through strip.gamma32() to provide 'truer' colors
// before assigning to each pixel:
pixels.setPixelColor(i, pixels.gamma32(pixels.ColorHSV(pixelHue)));
}
pixels.show(); // Update strip with new contents
delay(wait); // Pause for a moment
}
}
void RGBloop(int wait){
for(int j =0;j<6;j++){
for (int k=0 ; k< 256;k++){
switch(j){
case 0: pixels.fill(pixels.Color(k, 0, 0, wait)); break;
case 1: pixels.fill(pixels.Color(k, k, 0, wait)); break;
case 2: pixels.fill(pixels.Color(0, k, 0, wait)); break;
case 3: pixels.fill(pixels.Color(0, k, k, wait)); break;
case 4: pixels.fill(pixels.Color(0, 0, k, wait)); break;
case 5: pixels.fill(pixels.Color(k, 0, k, wait)); break;
}
pixels.show();
delay(wait);
}
for (int k= 255; k>=0;k--){
switch(j){
case 0: pixels.fill(pixels.Color(k, 0, 0, wait)); break;
case 1: pixels.fill(pixels.Color(k, k, 0, wait)); break;
case 2: pixels.fill(pixels.Color(0, k, 0, wait)); break;
case 3: pixels.fill(pixels.Color(0, k, k, wait)); break;
case 4: pixels.fill(pixels.Color(0, 0, k, wait)); break;
case 5: pixels.fill(pixels.Color(k, 0, k, wait)); break;
}
pixels.show();
delay(wait);
}
}
}
escribe o pega el código aquí
You can turn on interrupt 0 (pin 2) and use that to tell the sketch that an IR code is coming.
When the interrupt is acknowledged, you disable the pixel code section and wait for a repeat of the code.
i.e. You press a button on the remote, then you press the button a 2nd time to select the new animation you want.
BTW
In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
Use the </> icon from the ‘reply menu’ to attach the copied sketch.
Have a look here: GitHub - Arduino-IRremote/Arduino-IRremote: Infrared remote library for Arduino: send and receive infrared signals with multiple protocols
What Arduino board are you using? On a board with 16-bit integers, you aren't going to fit that 24-bit code into 'value'.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.