[SOLVED] 5 Push buttons + LED

Hello everyone,

I try to make a small lamp for my nephew and ran into an issue.

The main idea is a small box with a LED strip and five button :

  • 3 for changing the color (mode 1, 2 and 3)
  • 2 for controlling the birghtness.

So i did this code :

#define PinLeds         7
#define NumberLeds      21
#define PinButtonPlus   6
#define PinButtonMinus  5
#define PinButtonMode1  4
#define PinButtonMode2  3
#define PinButtonMode3  2

int StatePlus;
int StateMinus;
int StateMode1;
int StateMode2;
int StateMode3;

int brightness = 135;

#include <FastLED.h>
CRGB     leds[NumberLeds];

void setup() {
  pinMode(PinButtonPlus, INPUT);
  pinMode(PinButtonMinus, INPUT);
  pinMode(PinButtonMode1, INPUT);
  pinMode(PinButtonMode2, INPUT);
  pinMode(PinButtonMode3, INPUT);

  FastLED.addLeds<NEOPIXEL, PinLeds>(leds, NumberLeds);
  fill_solid(leds, NumberLeds, CRGB(0,0,255));
  FastLED.show();
}

void loop() {
  StatePlus = digitalRead(PinButtonPlus);
  StateMinus = digitalRead(PinButtonMinus);
  StateMode1 = digitalRead(PinButtonMode1);
  StateMode2 = digitalRead(PinButtonMode2);
  StateMode3 = digitalRead(PinButtonMode3);

  if (StatePlus == HIGH) {
    if (brightness == 255) {
      brightness = 255;
    } else {
      brightness = brightness + 30;
    }
  }

  if (StateMinus == HIGH) {
    if (brightness == 15 || brightness == 0) {
      brightness = 0;
    } else {
      brightness = brightness - 30;
    }
  }

  if (StateMode1 == HIGH) {
    fill_solid(leds, NumberLeds, CRGB(255,255,0));   
  } 

  if (StateMode2 == HIGH) {
    fill_solid(leds, NumberLeds, CRGB(237,0,0));   
  }

  if (StateMode3 == HIGH) {
    fill_solid(leds, NumberLeds, CRGB(22,184,78));    // Couleur "Menthe"
  }

  FastLED.show();
  

}

Regarding some tutorial on the web, i use an Arduino nano with :

  • LED strip on D7, +5V and Ground
  • Push button on :
    --- 5V (red wires with green heat skrink tube)
    --- Ground with 20k resistor (didn't work without it...) (black wires with green heat shrink tube)
    --- Digital pin 2 to 6 with a 10k resistor inside the red heat shrink tube.


And as you guess, when i press one button, all the button fire.... :slight_smile:

I don't really know where i made a mistake.

Could you help me ?

Thank you,

T.

I doubt there's much we can do without a schematic unless it's software. I can't help you with the software, but if it's hardware and you're serious about fixing it print out the schematic, get a meter and put it in resistance/continuity mode, ohm out each and every connection , and if it passes, highlight yellow
on the schematic with a felt tip highlighter.
Do that for the entire circuit.

Hello,

It is my first schematic, i hope it will be good enough.

So, i wire my push button to 5V and Ground + Digital pin with 10k resistor.
And a LED strip to Digital pin 7 with Ground and 5V.

And when i push on button, all the button activates.

Thank you

Software is not my area of expertise but even I can see you don't have any diagnostic prints so it's no wonder you have no idea what's happening. Add print statements everywhere so you can read the states. I would also add delays to see if there is a timing issue. I would suggest adding a statement to blink the onboard led for a second if 'state' = expected state but the print statements should tell you that.That's about the most I can say on software but I've debugged similar sketches by adding delays and print statements and using the
serial monitor or a terminal cspture program like Clear Terminal.

The switches are not wired right. Disconnect the 5V wires from all the switches. Connect one side of the switches to ground. Remove all of the resistors. Wire the other side of each switch to an input pin. In setup(), set the plnMode for the switch pins to INPUT_PULLUP. The inputs will read LOW when the switch is pressed so change your code accordingly.

Can you post the vendor link for those switches.
I've never seen them before.

They are perfectly ordinary pushbuttons with two terminals.

It is identical to this concurrent thread. Just a misunderstanding of how to make connections.

You could leave the black wires with the resistors in place, cut off the black wires connected together and connect the red wire to ground instead of 5 V. Then do what groundFungus explains in the code.

If all the switches are wired across 5V wouldn't it reset the board every time you press one ?

raschemmel:
If all the switches are wired across 5V wouldn't it reset the board every time you press one ?

Oh well, minor detail! :grinning:

Sorry for the delay, I was unable to login to the forum, need to try all the browsers I have :o

groundFungus:
The switches are not wired right. Disconnect the 5V wires from all the switches. Connect one side of the switches to ground. Remove all of the resistors. Wire the other side of each switch to an input pin. In setup(), set the plnMode for the switch pins to INPUT_PULLUP. The inputs will read LOW when the switch is pressed so change your code accordingly.

Quickly : totally did it ! Thanks, everything is now working ! :slight_smile:

raschemmel:
Software is not my area of expertise but even I can see you don't have any diagnostic prints so it's no wonder you have no idea what's happening. Add print statements everywhere so you can read the states. I would also add delays to see if there is a timing issue.

I voluntary remove the Serial.print to put a smaller code, but I use it a lot :slight_smile:

Paul__B:
You could leave the black wires with the resistors in place, cut off the black wires connected together and connect the red wire to ground instead of 5 V. Then do what groundFungus explains in the code.

I ended by doing what groundFungus said, so something like that :
(not exactly, I have a 5V input from the plug)

I guess it is simpler, not using any resistor :slight_smile:
Thanks to both of you.

raschemmel:
If all the switches are wired across 5V wouldn't it reset the board every time you press one ?

..... Indeed :smiley:

Thanks you all for the quick replies, all is working, can give it to my nephew tomorrow :slight_smile: