Control Led in a Led Switch Button

Hi everybody,
I'm an amateur using arduino to create a toy for my kid.

In this project, I use an Arduino Mega and a switch led button (Link)

I wanted to control the led (make it blink before it's pushed on) on this switch to tell the user (my son) which button needs to be pushed on.
I wanted also to get the information when the button is pushed to go to next step of the game.

I decided to put the + of the button on my output 50 to control the led
and to attach the button to my input 51 to get the information "high" when the button is pushed on.

I had some troubleshooting in my project with an incoherent information after the button is pushed so I isolated it in a smaller project :

const int buttonPin1 = 51;        // the number of the pushbutton pin
const int ledbuttonPin1 = 50;     // the number of the pushbuttonLED pin

int buttonState1 = 0;         // variable for reading the pushbutton status

void setup() {
  Serial.begin(9600);
  pinMode(buttonPin1, INPUT);
  pinMode(ledbuttonPin1, OUTPUT);

}

void loop() {
  buttonState1 = digitalRead(buttonPin1);
  digitalWrite(ledbuttonPin1, buttonState1);     // On the project, the ledbuttonPin1 is used to blik as a call to action.
  Serial.print("buttonState1=");
  Serial.println(buttonState1);
  delay(30);

}

The problem I had is the following :
The serial returns "buttonState1=0" while the button isn't pushed.
As soon as it is pushed, "buttonState1=1"
Until there it works as expected

But, when I unlock the button, it still get the information "buttonState1=1" during a variable amount of time (15 seconds or 1 minutes).

Usually when the arduino is reseted after some time, it goes back to "buttonState1=0" until it's pushed again. I need to have a more precise information in order to get the information as soon the button is unlocked.

Maybe I should add a resistance, maybe this kind of button is not done to be used with a control of the led while getting the info from the button ...

Every help would be greatly appreciated.

To be more precise, I have 6 buttons in my project and they are linked like this (maybe this explain some troubleshootings because it's not the cleanest way to solder it ...)

Thank you all

isn't that a 12V switch?

Oh !
That was quick.

Indeed, as the light worked great and the button was ordered 2 years ago I didn't pay attention.
Is there a way to use it or I'll have to change the 6 switches ?

The voltage should only affect the brightness of the LED, usually they will glow with as little as 2V, but that depends on the actual specs for the button (check the spec sheet). You should try to wire the button to function as a ground switch which is pulled up rather than a voltage switch.

EDIT: Are you sure that the button is not a latching push button (stays on until pushed again)?

if the LED turns on at 5V (you would need to measure also the current draw, I assume there is a resistor in the package) then you can use it.

If the current draw is low enough (like less than 20mA when you power the LED with 5V), then this is how I would try to wire it: (if you are unsure add a 220Ω Resistor in series with the ledPin wire but then you might not have enough power to turn on the led)

ledPin goes to pin 2 and buttonPin to pin 3 on your Arduino

and play with this code:

const byte ledPin = 2;
const byte buttonPin = 3;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(115200); Serial.println();
}

void loop() {
  if (digitalRead(buttonPin) == LOW) {    // button is pressed
    Serial.println("Pressed");
    digitalWrite(ledPin, HIGH); // turn led on
  } else {   // button is released
    Serial.println("Released");
    digitalWrite(ledPin, LOW); // turn led on
  }
  delay(1000); // just for testing purpose, we verify only once per second so that the Serial output is slow
}

(open the Serial monitor at 115200 bauds)

this way you can read the state of the button in code and control the led as well.

Thanks for your quick replies,

Yes, the button is not momentary, it stays pushed until I push it again.
I'm not familiar with the ground switch, I usually use. I'll try it this way, it could solve my problem.

@J-M-L it's very clear, I try it as soon as I turn back to work on this project and keep you in touch but I'm very hopeful with this solution.

I'll give you a quick view of my full project to show you the beautiful gift to my son you both contributed at.

Thanks

can't wait to see the pictures :slight_smile:

hope it works

It worked great !
Thank you ! Still impressed how great this community is ! Thanks to people like you.

I'll do my best to give it back.

Here's some pics of my spatial cockpit for my son. The idea is to plug headphones/helmet and my son's cousin (and R2D2 sounds) will guide him to make his spaceship take off and land on moon or go to ISS etc ... I also made a controllable mini spaceship on a rotating background (giving a false Y-Axis movement) with asteroids he has to avoid (thanks to an old DVD reader to make it move on an X-Axis) with a joystick. All of this stored in a suitcase

2 Likes

Fantastic ! (And lucky son!)

1 Like

Thank you very much

As a latching switch, you could get away with detecting the change of state - and using that to toggle the operating mode.

The only problem is the button face will be slightly depressed while it’s ‘on’.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.