Drone light - Leds are not working

Hello,

I just build a light for under my drone which can attached under it to do some lightpainting.

It was a very simple build.
9V battery - ubec 5v-7A - arduino nano - 8 ws2812 Leds - dipswitch 4x

My idea was to get white on dipswitch 1 and other colors on the other dipswitch.
just tried to get 3 colors. I do have power on my nano and led, but there is no light.

The dipswitch is input-pullup

Do I have the code not right?
thank you.
Bert


#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif

// How many NeoPixels are attached to the Arduino?
#define PIXEL_PIN 6
#define PIXEL_COUNT 8  // Number of NeoPixels
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

int DIP2 =  2;
int DIP3 = 3;
int DIP4 = 4;
int DIP5 = 5;


void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  //pinMode(6,       OUTPUT);

  strip.begin(); // Initialize NeoPixel strip object (REQUIRED)
  strip.show();  // Initialize all pixels to 'off'

}

void loop() {


  DIP2 = digitalRead(2);
  DIP3 = digitalRead(3);
  DIP4 = digitalRead(4);
  DIP5 = digitalRead(5);


  if (DIP2 == LOW ){
  strip.setPixelColor(60, 255, 255, 255);
    strip.show();  // Initialize all pixels to 'off'
  }
  else if (DIP3 == LOW ){
  strip.setPixelColor(60, 0, 255, 0);
    strip.show();  // Initialize all pixels to 'off'
  }

  else if (DIP4 == LOW ){
  strip.setPixelColor(60, 255, 0, 0);
    strip.show();  // Initialize all pixels to 'off'
  }

  else if (DIP5 == LOW ){
  strip.setPixelColor(60, 0, 0, 255);
    strip.show();  // Initialize all pixels to 'off'
  }

  else if (DIP2 == LOW, DIP3 == LOW, DIP4 == LOW, DIP5 == LOW ){
  strip.setPixelColor(0, 0, 0, 0);  // Zwart, is uit
   strip.show();  // Initialize all pixels to 'off'
  }
}

What do you expect from this line? Probably not what you think!

What happens and what do You want to happen?

That call sets pixel number 60 to white.

All your calls are like that.

To turn the entire strip a color, use a for loop and set every pixel 0 through N - 1 number of pixels to the desired color.

Then show the strip.

There may be a way to at once set all N pixels, I do no see it but didn't look too hard.

OK, you can use

  strip.fill(color, first, count);

HTH

a7

I was hoping if all the switches are on, the LEDs would go black, out.

You wanna use &&, or and, not commas in your logical expression.

a7

There is not a led working.
My plan was, switch 1 on, all the 8 LEDs white. If switch 2, 3 or 4 are on I get other colors.

Oh thank you.
Now I get somewhere.
I was thinking it was the brightness of the LEDs

Guessing?

Put Your code aside and make test code for the troubling functions, one at the time..
The shorter code the easier to debug. Debugging an entire project is the amateur way to do it. The big guys test small parts of the project and integrates each small functioning peace to the main code body.

Thank you,

I thought this was a small part.
Nothing more then a switch and a few LEDs.
But you got it right, maybe I should start with the switch and 1 normal led.

Tomorrow I will continue, no time tonight.

Thank you

Tomorrow start with your friend mr. google, it took me less than two minutes to find the kind of tutorial and documentation that will take the guesswork out of your endeavour.

a7

Almost, but not small enough.

I got it working, thank you for that.
Maybe the code can a bit cleaner, but I don't know how.

#include <Adafruit_NeoPixel.h>
#define PIN 6
#define NUM_LIGHTS  8

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LIGHTS, PIN, NEO_GRB + NEO_KHZ800);

int DIP1 =  2;
int DIP2 =  3;
int DIP3 =  4;
int DIP4 =  5;

void setup() {

  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);


  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {

  DIP1 = digitalRead(2);
  DIP2 = digitalRead(3);
  DIP3 = digitalRead(4);
  DIP4 = digitalRead(5);

  uint32_t low = strip.Color(0, 0, 0);
  //uint32_t high = strip.Color(255, 255, 255);


  if (DIP1 == LOW ) {
    for ( int i = 0; i < NUM_LIGHTS; i++) {
      strip.setPixelColor(i, 255, 255, 255);
      strip.show();
    }
  }
  else if (DIP1 == HIGH ) {
    for ( int i = 0; i < NUM_LIGHTS; i++) {
      strip.setPixelColor(i, low);
      strip.show();
    }
  }

  
  if (DIP2 == LOW ) {

    for ( int i = 0; i < NUM_LIGHTS; i++) {
      strip.setPixelColor(i, 0, 255, 0);
      strip.show();
    }
  }
  else if (DIP2 == HIGH ) {
    for ( int i = 0; i < NUM_LIGHTS; i++) {
      strip.setPixelColor(i, low);
      strip.show();
    }
  }

  
  if (DIP3 == LOW ) {

    for ( int i = 0; i < NUM_LIGHTS; i++) {
      strip.setPixelColor(i, 255, 0, 0);
      strip.show();
    }
  }
  else if (DIP3 == HIGH ) {
    for ( int i = 0; i < NUM_LIGHTS; i++) {
      strip.setPixelColor(i, low);
      strip.show();
    }
  }

  if (DIP4 == LOW ) {

    for ( int i = 0; i < NUM_LIGHTS; i++) {
      strip.setPixelColor(i, 0, 0, 255);
      strip.show();
    }
  }
  else if (DIP4 == HIGH ) {
    for ( int i = 0; i < NUM_LIGHTS; i++) {
      strip.setPixelColor(i, low);
      strip.show();
    }
  }
}

I think if you had read @alto777's suggestion, you might have not needed the for loops. But they may be a place where you could do something other that run all the LEDs to one colour.

Also, think about this:

    unsigned char dipSwitches = (DIP5 ? 8 : 0) + (DIP4 ? 4 : 0) +  (DIP3 ? 2 : 0) +  (DIP2 ? 1 : 0);

    switch (dipSwitches) {
    case 0 :

        break;

    case 1 :

        break;

// and so forth

    case 15 :

        break;
    }

So your assignment is to learn about the ternary conditional operator:

and the switch/case statement:

Where I linked or your favorite learning source.

HTH

a7

I will look in it tonight
Thank you,
Bert

TBC, my sketchy sketch is just a skeleton suggestion and if you learn about the switch/case statement you will see it provides a means for doing 16 different things based on the settings of the four dio switches.

a7

This was a good lesson, learned a lot.
This project is come to an end, the lights does not produce enough light to brighten up the subject, but i wanted to learn and understand your assignment.
My project is now different, only a few white color led with a lot of lumen and one switch must do the trick.
I embedded the sketch, if it could be better I will try that either to learn.

Thank you for the tips and tricks.
Bert

// Arduino Nano Standard Layout
// Used with Arduino Nano
// By Jeremy S. Cook 3/18/2019
//
// Includes code from:
// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif

// How many NeoPixels are attached to the Arduino?
#define PIXEL_PIN 6
#define PIXEL_COUNT 8  // Number of NeoPixels
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

int DIP2 = 2;
int DIP3 = 3;
int DIP4 = 4;
int DIP5 = 5;

uint32_t white = strip.Color(255, 255, 255);
uint32_t red      = strip.Color(255, 0, 0);
uint32_t green  = strip.Color(0, 255, 0);
uint32_t blue    = strip.Color(0, 0, 255);
uint32_t black  = strip.Color(0, 0, 0); // off


void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
 

  strip.begin(); // Initialize NeoPixel strip object (REQUIRED)
  strip.show();  // Initialize all pixels to 'off'

}

void loop() {


  DIP2 = digitalRead(2);
  DIP3 = digitalRead(3);
  DIP4 = digitalRead(4);
  DIP5 = digitalRead(5);


unsigned char dipSwitches = (DIP5 ? 4 : 0) + (DIP4 ? 3 : 0) +  (DIP3 ? 2 : 0) +  (DIP2 ? 1 : 0);

    switch (dipSwitches) {
    case 0 :
        strip.fill(black, 0, 8);
        strip.show();
        break;

    case 1 :
        strip.fill(white, 0, 8);
        strip.show(); 
        break;

    case 2 :
        strip.fill(red, 0, 8);
        strip.show(); 
        break;

    case 3 :
        strip.fill(green, 0, 8);
        strip.show(); 
        break;

    case 4 :
        strip.fill(blue, 0, 8);
        strip.show(); 
        break;
    }
 
}

The LEDs can be very bright to look at, but not much in the way of illumination.

It is nice that you have learned.

I have modified your sketch to return it closer to the idea I gave you. The four switches make 16 different patterns (in my code) according to the values of four binary bits. Your new expression is valid, but really only recognizes and differentiates between 5 cases. No switch, and four with just one switch on. Here on means open (INPUT_PULLUP).

The below is code that should function identically, but would preserve the possibility of using, say, a certain two switches on and the other two off to mean something different, viz:

// Arduino Nano Standard Layout
// Used with Arduino Nano
// By Jeremy S. Cook 3/18/2019
//
// Includes code from:
// NeoPixel Ring simple sketch (c) 2013 Shae Erisson
// released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif

// How many NeoPixels are attached to the Arduino?
#define PIXEL_PIN 6
#define PIXEL_COUNT 8  // Number of NeoPixels
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

int DIP2 = 2;
int DIP3 = 3;
int DIP4 = 4;
int DIP5 = 5;

uint32_t white = strip.Color(255, 255, 255);
uint32_t red      = strip.Color(255, 0, 0);
uint32_t green  = strip.Color(0, 255, 0);
uint32_t blue    = strip.Color(0, 0, 255);
uint32_t black  = strip.Color(0, 0, 0); // off

uint32_t magenta  = strip.Color(255, 0, 255); // magenta

void setup() {
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);


  strip.begin(); // Initialize NeoPixel strip object (REQUIRED)
  strip.show();  // Initialize all pixels to 'off'

}

void loop() {


  DIP2 = digitalRead(2);
  DIP3 = digitalRead(3);
  DIP4 = digitalRead(4);
  DIP5 = digitalRead(5);


  unsigned char dipSwitches = (DIP5 ? 8 : 0) + (DIP4 ? 4 : 0) +  (DIP3 ? 2 : 0) +  (DIP2 ? 1 : 0);

  switch (dipSwitches) {
    case 0 :
      strip.fill(black, 0, 8);
      strip.show();
      break;

    case 1 :
      strip.fill(white, 0, 8);
      strip.show();
      break;

    case 2 :
      strip.fill(red, 0, 8);
      strip.show();
      break;

    case 4 :
      strip.fill(green, 0, 8);
      strip.show();
      break;

    case 8 :
      strip.fill(blue, 0, 8);
      strip.show();
      break;

    case 7 :
      strip.fill(magenta, 0, 8);
      strip.show();
      break;

    // unimplemented. use case N : and 3, 5, 7, 9, 10, 11, 12, 13, 14, 15 for multi-switch cases

    default :  //unused combinations
      strip.fill(white, 0, 8);
      strip.show();
      break;
  }
}

For example, cases 9 through 15 are those with DIP5 open, and the 7 ways the three remaining switches can be open or closed.

I would guess you have noticed that in your code, the pattern doesn't change until only one switch is open, or all are closed. I think. Too late to test it for real.

See if that makes sense. You may see a need for it, or something similar in other code you read.

a7

If I understand your subsequent comments, and code, I think you've misunderstood what's going on here; I'll attempt to explain.
The variable dipSwitches is being assigned a value that is the sum of four conditions; the end result is a value which could be anything between 0 and 10 (4+3+2+1). So your switch statement should really handle any of those. The four conditions you seem to want (only 1 of the four switches set, values 4/3/2/1), should be 4 different cases, and the switch statement should also include a default case, which will handle all other possible values. Hope that helps.
C
Hope that helps.

Do you mean i can say switch 1 and 4 makes 5 and put a case on 5.
Should I do 3 switches 2-3-4 I use case 9 or am I totally wrong now.