Need help with my code

Hello guys,

For school I have to make a project using the Arduino. I’ve decided to create a lamp using a Adafruit Neopixel Ring with 24 LEDs. I want to turn the LEDs on and off using a remote control. So far I have been able to use the code that is on the website of Adafruit and it work with my Neopixel Ring but it only turns on 1 LED instead of 24 LEDs. I tried to change the code by changing the 1 to a 24 in the code but sadly that is not working for me. I can turn on the single LED but I wanna be able turn them all on and off. I am not quite code with reading and understanding codes because I have only using the Arduino for such a short time so I hope you guys can help me with my issue. I have the code here:

#include <Adafruit_NeoPixel.h>
#include <IRLibAll.h>

IRrecv myReceiver(2);//receiver on pin 2
IRdecode myDecoder;//Decoder object



//One NeoPixel connected to pin 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(24,6,NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  myReceiver.enableIRIn(); // Start the receiver
}

void loop() {
  if (myReceiver.getResults()) {
    myDecoder.decode();
    if (myDecoder.protocolNum == NEC) {
      switch(myDecoder.value) {
        case 0xFF30CF:  //Button 1
          strip.setPixelColor(0,255,0,0);//Red
          break;
        case 0xFF18E7:  //Button 2
          strip.setPixelColor(0,0,255,0);//Green
          break;
        case 0xFF7A85:  //Button 3
          strip.setPixelColor(0,0,0,255);//Blue
          break;
      }
    strip.show();
    myReceiver.enableIRIn(); //Restart the receiver
    }
  }
}

I really hope I can get a fast response because I need to have this thing working within a week! Thank you in advance!!

What does this line do?
strip.setPixelColor(0,255,0,0);

When you call:

strip.setPixelColor(0,255,0,0);//Red

you’re only setting the color for the first (0) pixel. You need a loop like:

.
.
.
for( int i=0; i<24; i++ )
    strip.setpixelColor( i, 255, 0, 0 );
.
.
.

first of all thank you for the reply. But I am confused on how I should add that to my code.

strip.setPixelColor(0,0,255,0);
.
.
.

Your code only set one pixel i.e. to red green or blue.

As mentioned you have to turn on all the LEDs on. :wink:

As offered by Blackfin.

so if I change strip.setPixelColor(0,0,255,0); to strip.setPixelColor(24,0,255,0); it will turn on all my LEDs?

case 0xFF30CF: //Button 1
for( int i=0; i<24; i++ )
{
strip.setpixelColor( i, 255, 0, 0 ); //red
}
break;

etc.

Ah okay I got them all turned on but now I have another issue. 1 LED turns blue and the 23 turn red. I copied your code and added it to my code. Is there something I am doing wrong now?

#include <Adafruit_NeoPixel.h>
#include <IRLibAll.h>

IRrecv myReceiver(2);//receiver on pin 2
IRdecode myDecoder;//Decoder object



//One NeoPixel connected to pin 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(24,6,NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  myReceiver.enableIRIn(); // Start the receiver
}

void loop() {
  if (myReceiver.getResults()) {
    myDecoder.decode();
    if (myDecoder.protocolNum == NEC) {
      switch(myDecoder.value) {
       case 0xFF30CF:  //Button 1
       for(int i=0; i<24; i++)
       {
        strip.setPixelColor(i, 255, 0, 0);  //red        
        }
        case 0xFF18E7:  //Button 2
        for(int i=0; i<24; i++)
        {
          strip.setPixelColor(i,0,255,0);//Green
          break;
        }
        case 0xFF7A85:  //Button 3
        for(int i=0; i<24; i++)
        {
          strip.setPixelColor(i,0,0,255);//Blue
          break;
        }
      }
    strip.show();
    myReceiver.enableIRIn(); //Restart the receiver
    }
  }
}

strip.setPixelColor(0,0,0,255);//Blue :wink:

You need some more breaks in your switch/case, but not inside your for loops.

Time to study:
https://www.arduino.cc/reference/en/language/structure/control-structure/for/
and
https://www.arduino.cc/reference/it/language/structure/control-structure/switchcase/

BTW
What does this line do?
strip.setPixelColor(0,255,0,0);

AWOL:
You need some more breaks in your switch/case, but not inside your for loops.

I added a break and now I am back with one LED on instead of all of them. on top of that it only remains on blue instead of Red > Green > Blue when I push buttons on my remote

larryd:
strip.setPixelColor(0,0,0,255);//Blue :wink:

I dont know what I am doing wrong anymore it was working.. sort of

larryd:
BTW
What does this line do?
strip.setPixelColor(0,255,0,0);

It is suppose to turn the LED to Red

We need to see the code as it is now.

Yes, LED 1, that is zero relative.
That’s why you need to use the for loop to do all 24 LEDs 0-23.

Did you read how to use switch case?
https://www.arduino.cc/reference/it/language/structure/control-structure/switchcase/

Show us your code as it is now.

#include <Adafruit_NeoPixel.h>
#include <IRLibAll.h>

IRrecv myReceiver(2);//receiver on pin 2
IRdecode myDecoder;//Decoder object



//One NeoPixel connected to pin 6
Adafruit_NeoPixel strip = Adafruit_NeoPixel(24,6,NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  myReceiver.enableIRIn(); // Start the receiver
}

void loop() {
  if (myReceiver.getResults()) {
    myDecoder.decode();
    if (myDecoder.protocolNum == NEC) {
      switch(myDecoder.value) {
       case 0xFF30CF:  //Button 1
       for(int i=0; i<24; i++)
       {
        strip.setPixelColor(i, 255, 0, 0);  //red        
        }
        break;
        case 0xFF18E7:  //Button 2
        for(int i=0; i<24; i++)
        {
          strip.setPixelColor(i,0,255,0);//Green
        }
        break;
        case 0xFF7A85:  //Button 3
        for(int i=0; i<24; i++)
        {
          strip.setPixelColor(i,0,0,255);//Blue
        }
        break;
      }
    strip.show();
    myReceiver.enableIRIn(); //Restart the receiver
    }
  }
}

That is the code now but I managed to get it to work with this~ Thank you guys~ I will edit the code with some other colours~
I think I made an typo while editing the code

The first zero is the LED number you are setting.
strip.setPixelColor(0,255,0,0);

When you make the first number a variable, you can use it to set subsequent LEDs
strip.setPixelColor(i,255,0,0);

Makes sense?

Now you should add comments to your sketch to remind yourself what’s happening a month from now.

Extra credit.
Make the ring rgbrgbrgbrgb etc. when a IR button is pressed.