Missing some code

single button to light simple leds in sequence, but need some startup code.
new to this and can manipulate code to a point.
the code below works fine for lighting leds or "bands" and does what i need.

here is what im missing.
when the arduino is powered on nothing happens untill the button is pressed, on startup i would like pin8 to be high then when the button is pressed to cycle from 8 to 9,10,11,12 ect .
lost how to do this thanks.
posted in led section as the code was written for leds

//dt307 button band changer 
int BandPins[5] = {8, 9, 10, 11, 12};
const int buttonPin = 13;
int buttonState = HIGH;
int pushCounter = 0;
int numberOfBands = 3; //DT307 SELECT NUMBER OF BANDS 1-5
void setup() {
  pinMode(buttonPin, INPUT);
  for (int i = 0; i <= 4; i++) {
    pinMode(BandPins[i], OUTPUT);
  }
}
void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonState == LOW) {
    for (int i = 0; i < numberOfBands; i++) {
      if (pushCounter % numberOfBands == i) {
        digitalWrite(BandPins[i], HIGH);
      }
      else {
        digitalWrite(BandPins[i], LOW);
      }
    }
    pushCounter++;
    delay(500);
  }
}

So at the end of setup() you could use a while() loop that will break once the button is pressed. You can combine while() and digitalRead(). You don't need to store the output of digitalRead() in a variable.
Btw, do you want the program to start once the button is pressed, or once it's pressed and then released?

So after the while() loop described above, use digitalWrite to set pin8 HIGH.

The cycling bit you've already got, so you only have to put another while() loop that waits for a button press (and/or release?) between the digitalWrite bit I mentioned above, and your existing loop() code.

All modifications could be put at the end of setup() since they apparently run only once.

See if you can attempt to write some code and then feel free to ask questions if/when you get stuck.

the code will be on a nano board.
when the nano is powered up i want the first "led or band" that is connected to pin 8 to be HIGH and all others LOW .
then when the button is pressed and released to cycle to the next "led or band" on pin 9 goes HIGH then with another press and release pin 10 goes HIGH. the amount of "led bands" is defined by
sorry for my terms
total amount of "led bands" selectable and pinouts this dont change

int BandPins[5] = {8, 9, 10, 11, 12};

and for selecting the number of "led bands" depending on what i need say 3 or 4 or 5 ect

int numberOfBands = 3;

this all works fine

thanks for reply

so all the code works perfect for me accept when the board is turned on all the outputs are low and i want pin 8 to be high as a starting point

hope ive explained clearer and apologise for any confusion

No problem.

Yeah, so that's what your code already does, doesn't it?

So go ahead and modify your code to add the button response at the start.

sorted it now thanks

  digitalWrite(8, HIGH);

added to run once

There you go!

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