Button controlled pin

I have an Arduino nano.

I am trying to program it so that when a button is pushed it sends current out of 5 pins. Then it is a random delay before it randomly turns off when of the currents (going to be lights in thoose, so a random light will turn off after a random time).

Here is the code I have:

int randomLight = 2 + (rand() % 5);
int randomTime = 15000 + (rand() % 90000);
const int buttonPin = 7;
const int ledPinOne = 2;
const int ledPinTwo = 3;
const int ledPinThree = 4;
const int ledPinFour = 5;
const int ledPinFive = 6;


void setup() {
  pinMode(buttonPin, INPUT_PULLUP);

  pinMode(ledPinOne, OUTPUT);
  pinMode(ledPinTwo, OUTPUT);
  pinMode(ledPinThree, OUTPUT);
  pinMode(ledPinFour, OUTPUT);
  pinMode(ledPinFive, OUTPUT);
  
}

void loop() {
  if (digitalRead(buttonPin) == LOW)
  {
  digitalWrite(ledPinOne, HIGH);
  digitalWrite(ledPinTwo, HIGH);
  digitalWrite(ledPinThree, HIGH);
  digitalWrite(ledPinFour, HIGH);
  digitalWrite(ledPinFive, HIGH);
  delay(randomTime);
  digitalWrite(randomLight, LOW);
  }
}

No lights are turning on. What have I done wron and how could I have written the code so that it works?

Thanks in advance!

How is it all wired up?

These lines

int randomLight = 2 + (rand() % 5);
int randomTime = 15000 + (rand() % 90000);

are a declaration of two variables and also establish an initial value.

They do not make a magic connection between the variables and what you are trying to do with them.

Only by assigning new values will the initial values change to new values.

Try

void loop() {

  randomLight = 2 + (rand() % 5);
  randomTime = 15000 + (rand() % 90000);

  if (digitalRead(buttonPin) == LOW)
  {
    digitalWrite(ledPinOne, HIGH);
    digitalWrite(ledPinTwo, HIGH);
    digitalWrite(ledPinThree, HIGH);
    digitalWrite(ledPinFour, HIGH);
    digitalWrite(ledPinFive, HIGH);
    delay(randomTime);
    digitalWrite(randomLight, LOW);
  }
}

HTH

a7


Something like in the picture. But don't use led with resistance like that. It is going to be one cable out from each of the pins 2-7 with the button on 7 and then all of them connected to ground.

Ah, that is true. Thanks, missed that I had to update the value when the button is pressed. But it doesn't solve it fully. My first problem is that nothing happens when I push the button. It doesn't even turn on the lights.

But then they should need to be inside the if statement?

Can you fix the wiring diagram, it’s very difficult to see what is wired where.

Very easily wired. One light to each of 2-6 and then the button to 7. Everything to ground.

Are your LEDs backwards?

I hope you don't mean you are not using a series current limiting resistor one for each LED.

That would be okay, but where they are it's like shuffling the dice 1000s of times a second.

Practically speaking makes no difference. But it's a nice trick to remember, a way of using real world events (how long is a button pressed, or how long since it has been) to scramble things real good.

a7

No, and that is only wiring for testing at New Arduino Nano Project - Wokwi Simulator. IRL I don't use leds and the output works.

With the code below my light turns on with different intervalls. So that part (the light) works.

int randomTime = 15000 + (rand() % 90000);
const int ledPinOne = 2;

void setup() {
 
  pinMode(ledPinOne, OUTPUT);
  
}

void loop() {
  
  delay(randomTime);
  digitalWrite(ledPinOne, HIGH);
  delay(2000);
  digitalWrite(ledPinOne, LOW);

}

No, I don't use leds. I have USB in the other end of the cable where a light is connected that runs on 5V.

But how do I make my code work when pushing the button? The lights without anything of the random doesn't even turn on.

I can't run that code, but I'd bet you breakfast that the interval is identical every time. Maybe even the very same the every time you restart the code.

Post a simple sketch like your last one, with your attemtp to make it controlled by pushbutton.

It's hard to see, so I assume your switch is wired between digital pin 7 and GND. Looks like it should work…

a7

I was going to use the time I am waiting for she who must never be kept waiting (irony defined) to lash up your project and run your code.

All I managed was to find that rand() returns an int, values between including 0 to 32767.

You should use random() instead. The you could also use

  int x = random(42); // returns random # 0 ... 41

or

  int x = random(42, 99); // returns random # 42 ... 98

or

  int x = random(); // returns random # 0 ... 2147483647

So you don't need the modulo operator.

Later we can talk about a better way to handle LEDs on five pins. What you are doing is convenient but relies on some things that may end up being inconvenient. No worries for now.

a7

Sorry, just copied my old code and explained it a bit bad. My point with this was only that the light and board works and everything is connected correctly. So I have eliminated that.


Better picture. Haven't tried the code IRL after changing according to your suggestions. Only tried the simulation and that one doesn't work.

If you find real life to differ from the wokwi, suspect first that it is your problem IRL but post a link to the simulation. There are very interested ppl who want to make it perfecter.

a7

how did you test it if the code doesn't work?

did you write code that just turns on the LEDs and nothing else?

@wineitot I'm at the beach, but I did try to clean up your wiring... you can move wires around after if you place them badly first time. Just select the wire. Use the little dots to drag it until you're happy. It makes it easier to see where the wires actually go.

I tried. It doesn't work too well on my atblet. BUT I did see when I moved a LED that they are not connected in the circuit.

You need to wire between the components, not just get them visually close.

So, not the simulator, not real life. :expressionless:

a7

Ah, thank you. Then the problem is solved. Just bad interface on the page then. I got tricked by the components attaching to each other, so it seemed like they were connected.

Thanks a lot for the help!

There's a grid, even when you can't see it, that makes things line up.

Be warned… there is no schematic drawing program I am aware of that connects pins or components because they get close to one another.

In better software there may be warnings about that.

In the wokwi, you don't need the series current limiting resistors, and LEDs will illuminate to full brightness just on the weak INPUT_PULLUP current.

Those are def two things that can make real life differ.

a7