Turn on random LED with an analog push button

Hello everybody, my name is Geovannys, nice to meet you.
I need to solve this problem for class, please.
Thank you for your information
I am using nano with expansion board

`int boton= 3;
int leds[]={9,10,11,12};
int numleds = sizeof(leds) / sizeof(leds[0]);

void setup() {
  // put your setup code here, to run once:
 Serial.begin(9600);
 pinMode(A3,INPUT);//Asigna el pin 2 como entrada el pin 2 donde está la entrada del botón
   
for(int i=0; i< numleds; i++){
pinMode(leds[i],OUTPUT);
   }

randomSeed(analogRead(0));

}

void loop() {
  // put your main code here, to run repeatedly:

boton = digitalRead(A3);//digital read número de pin donde se encuentra el pulsador
//Asigno a la variable "ValorBot" el valor que viene del pulsador cuando lo presiono
Serial.println(boton);
delay(500);

if (digitalRead(boton) == HIGH){
  int led =random(0,numleds);
  digitalWrite(leds[led],HIGH);
  delay(1000);
  digitalWrite(leds[led],LOW);
}
}  
`
  1. I want to press the button and turn on a LED randomly each time.

Welcome to the forum

What happens when you upload the sketch ?

Have you got a pulldown resistor keeping pin A3 LOW when the button is not pressed ?

Try with
if (boton == HIGH){

1 Like

Good mornig.
it`s more logical, thank you.

Consider using INPUT_PULLUP in pinMode() for the input pin to turn on the built in pullup resistor. Arrange the button wiring to take the pin LOW when pressed and detect LOW as a button press. No need for extra external components

Also, take note of post #3 above. You need to test the state of the button pin

1 Like

Good mornig.
Good morning, nice to meet you.
What happens when you upload the sketch ?

switch (contador){
//encender los Leds de acuerdo al contador

case 0:
//Todos los leds encendido
digitalWrite(led_1, HIGH);
digitalWrite(led_2, LOW);
digitalWrite(led_3, LOW);
digitalWrite(led_4, LOW);
break;

Have you got a pulldown resistor keeping pin A3 LOW when the button is not pressed ?

of course

The problem is solved, thank you.
I share a very useful link that I found on the arduino forum.
Beginners: using the switch - case statement

1 Like

Please share what the problem and solution was

1 Like
int led_1 = 9;
int led_2 = 10;
int led_3 = 11;
int led_4 = 12;
int pulsadoravanzar = 3;
int valorpul = 0; //variable usada para el antirrebote del controlador avanzar
int contador; //variable asociada a un contador

void setup() {
  Serial.begin(9600);
  // Declaración de entradas y sálidas digitales:
pinMode(pulsadoravanzar,INPUT);//Asigna el pin 3 como entrada el pin 3 donde está la entrada del botón
pinMode(led_1,OUTPUT);
pinMode(led_2,OUTPUT);
pinMode(led_3,OUTPUT);
pinMode(led_4,OUTPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
pulsadoravanzar = digitalRead(A3);//digital read número de pin donde se encuentra el pulsador
//Asigno a la variable "ValorBot" el valor que viene del pulsador cuando lo presiono
Serial.println(pulsadoravanzar);
delay(500);

if (pulsadoravanzar == LOW){
  valorpul = 1; //cambia de estado si se presiona el pulsador
} 
if (pulsadoravanzar == HIGH && valorpul == 1){
  valorpul = 0; //se reinicia la variable del antirrebote
  contador++; //el contador aumenta en una unidad

if (contador > 4){
  contador = 0; //se reinicia el contador regresando al LED 1
  }
}

switch (contador){
//encender los Leds de acuerdo al contador

case 0:
//Todos los leds encendido
digitalWrite(led_1, HIGH);
digitalWrite(led_2, LOW);
digitalWrite(led_3, LOW);
digitalWrite(led_4, LOW);
break;

case 1:
//Enciende el led 1 y apaga los demás
digitalWrite(led_1, LOW);
digitalWrite(led_2, HIGH);
digitalWrite(led_3, LOW);
digitalWrite(led_4, LOW);
break;

case 2:
//Enciende el led 2 y apaga los demás
digitalWrite(led_1, LOW);
digitalWrite(led_2, LOW);
digitalWrite(led_3, HIGH);
digitalWrite(led_4, LOW);
break;

case 3:
//Enciende el led 3 y apaga los demás
digitalWrite(led_1, LOW);
digitalWrite(led_2, LOW);
digitalWrite(led_3, LOW);
digitalWrite(led_4, HIGH);
break;

case 4:
//Enciende el led 4 y apaga los demás
digitalWrite(led_1, HIGH);
digitalWrite(led_2, HIGH);
digitalWrite(led_3, HIGH);
digitalWrite(led_4, HIGH);
break;


}

}  

Thanks, I'm pleased you found it helpful

1 Like

I read this topic hoping to find out about this "analog push button". In my mind, all push buttons are digital.

1 Like

I think it is important that you know what type of connection you use, and I have read that not all pins are the same.

It is a simple exercise but I am learning, thank you very much.

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