Start code with a button

Hello, I'm working on a school project where I've got to press a button to start a sequence of lights that I can only have run once (so no loop) and I want nothing to happen until the button is pressed

int Distribution = 1; //LEDs
int Drones = 2; //LEDs
int Collection = 3; //LEDs
int Holding = 4; //LEDs
int Drive = 5; //LEDs
int Bridge = 6; //LEDs
int Quarters = 7; //LEDs
int Storage = 8; //LEDs
int AI = 9; //LEDs
int Galley = 10; //LEDs
int Button = 11; //Button PIN


void setup() {
pinMode(Distribution,OUTPUT);
pinMode(Drones,OUTPUT);
pinMode(Collection,OUTPUT);
pinMode(Holding,OUTPUT);
pinMode(Drive,OUTPUT);
pinMode(Bridge,OUTPUT);
pinMode(Quarters,OUTPUT);
pinMode(Storage,OUTPUT);
pinMode(AI,OUTPUT);
pinMode(Galley,OUTPUT);
pinMode(Button,INPUT);

//Button press to start sequence here

delay(4000); //initial speech
//room 1
digitalWrite(Distribution,HIGH);
delay(6000);
digitalWrite(Distribution,LOW);
delay(100);
//room 2
digitalWrite(Drones,HIGH);
delay(6000);
digitalWrite(Drones,LOW);
delay(100);
//room 3
digitalWrite(Collection,HIGH);
delay(6000);
digitalWrite(Collection,LOW);
delay(100);
//room 4
digitalWrite(Holding,HIGH);
delay(6000);
digitalWrite(Holding,LOW);
delay(100);
//room 5
digitalWrite(Drive,HIGH);
delay(6000);
digitalWrite(Drive,LOW);
delay(100);
//room 6
digitalWrite(Bridge,HIGH);
delay(6000);
digitalWrite(Bridge,LOW);
delay(100);
//room 7
digitalWrite(Quarters,HIGH);
delay(6000);
digitalWrite(Quarters,LOW);
delay(100);
//room 8
digitalWrite(Storage,HIGH);
delay(6000);
digitalWrite(Storage,LOW);
delay(100);
//room 9
digitalWrite(AI,HIGH);
delay(6000);
digitalWrite(AI,LOW);
delay(100);
//room 10
digitalWrite(Galley,HIGH);
delay(6000);
digitalWrite(Galley,LOW);
//final delay
delay(300);
//this is the end
//I don't want this to loop
}


void loop() {
}

It is just a bunch of leds that I want to start with a button press
FYI, I'm fairly new to this stuff so if you could explain what your doing that would be very helpful.

Thanks for the help!!

Did you forget some pinModes?

yeah, I didn't quite get them copied in give me a minute

There must be about a minute of delays in there :frowning:

This is happening while another Arduino is playing an MP3 file that is about 2 minutes long, those delays are subject to change because I can't get the lights to turn on with the button!

In setup have a while loop looking at the button input , when the button is pressed escape the whole loop

The OP said no loop.

I get what your saying, but I don't exactly know how to do that and a loop like this would be fine, I just need the lights to not be in a loop.

    pinMode (Button, INPUT_PULLUP);    // button pulls pin to ground

    while (HIGH, digitalRead (BUTTON)) // wait for button press
        ;

    digitalWrite(Distribution,HIGH);
    ...

That seems to have worked!
Thanks!!!

1 Like

And I have another question, at the start of the code, Pin 1 breifly turns on at the beginning of the code, is that something that just happens and I shouldn't use it?

Pin one is half of the serial interface

I vaguely know what that means, so that just means if I don't want the led to turn on, I just don't use that pin?

What happened when you tried it?

It worked great, just right after I upload the led connected to pin 1 turns on for a second then back off.

So, its, not working anymore, when I upload the code, the code just starts and goes, doesn't loop, but doesn't matter if the button gets pressed or not.

int Distribution = 1; //LEDs
int Drones = 2; //LEDs
int Collection = 3; //LEDs
int Holding = 4; //LEDs
int Drive = 5; //LEDs
int Bridge = 6; //LEDs
int Quarters = 7; //LEDs
int Storage = 8; //LEDs
int AI = 9; //LEDs
int Galley = 10; //LEDs
int Button = 11; //Button PIN


void setup() {
pinMode(Distribution,OUTPUT);
pinMode(Drones,OUTPUT);
pinMode(Collection,OUTPUT);
pinMode(Holding,OUTPUT);
pinMode(Drive,OUTPUT);
pinMode(Bridge,OUTPUT);
pinMode(Quarters,OUTPUT);
pinMode(Storage,OUTPUT);
pinMode(AI,OUTPUT);
pinMode(Galley,OUTPUT);
pinMode(Button,INPUT);

//Button press to start sequence here
 pinMode (Button, INPUT_PULLUP);    // button pulls pin to ground

    while (HIGH, digitalRead (Button));
    delay(4000); //initial speech
//room 1
digitalWrite(Distribution,HIGH);
delay(6000);
digitalWrite(Distribution,LOW);
delay(100);
//room 2
digitalWrite(Drones,HIGH);
delay(6000);
digitalWrite(Drones,LOW);
delay(100);
//room 3
digitalWrite(Collection,HIGH);
delay(6000);
digitalWrite(Collection,LOW);
delay(100);
//room 4
digitalWrite(Holding,HIGH);
delay(6000);
digitalWrite(Holding,LOW);
delay(100);
//room 5
digitalWrite(Drive,HIGH);
delay(6000);
digitalWrite(Drive,LOW);
delay(100);
//room 6
digitalWrite(Bridge,HIGH);
delay(6000);
digitalWrite(Bridge,LOW);
delay(100);
//room 7
digitalWrite(Quarters,HIGH);
delay(6000);
digitalWrite(Quarters,LOW);
delay(100);
//room 8
digitalWrite(Storage,HIGH);
delay(6000);
digitalWrite(Storage,LOW);
delay(100);
//room 9
digitalWrite(AI,HIGH);
delay(6000);
digitalWrite(AI,LOW);
delay(100);
//room 10
digitalWrite(Galley,HIGH);
delay(6000);
digitalWrite(Galley,LOW);
//final delay
delay(300);
//this is the end
//I don't want this to loop
}


void loop() {
}

Assign a pin as input with pullup. Place the code that you want to run in a function after the closing bracket for void loop.

In void loop write an if statement with something like
If (btnPin == 0) { functionName()};

The code in the function will only run when your button is pressed...

That's an unusual construct.

Are you sure that your wanted functionality is
microcontroller is switched on
wait for button-press
if button is pressed make LED-lightshow for a single time
never never ever make LED-light-show again

to repeat
waiting for button etc. power must be switched to off and on again?

Or do you want
each time the button is pressed make LED-Lightshow only once
then go back to wait for the next button-press?

I want