Pushbutton and light array. Where do I start?

Is integrating the pushbutton example and the LED array example feasible? I have been trying to wrap my brain around it but I'm rather new to this and I have no idea where to start with a project like this.

It depends what you have in mind when you write "integrating"

By integrating I mean combining the two examples so that I can turn on the light array with a pushbutton and then turn it off with the same button. It seems like it would be easy for someone with experience with the arduino board and the programming to do, but I lack both of these traits, so where do I start?

After looking at the examples that was the first sketch I made on my own. I will see if I still have it.

I still have it. It should work, however, I have not used it in a year. Also, its probably not the best why to accomplish your task as it was my first try at making a program!

const int ledPin =  13;      // the number of the LED pin
const int buttonPin = 2;
int buttonState = 0;  
// Variables will change:
int ledState = 0;             


void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);   
  pinMode(buttonPin, INPUT);
}

void loop()
{

  while (true) {


    buttonState = digitalRead(buttonPin);


    if (buttonState == HIGH) {

      digitalWrite(ledPin, HIGH);
      ledState = 1;
      delay(375);
      buttonState = digitalRead(buttonPin);


      while (ledState == 1)

      {

        buttonState = digitalRead(buttonPin);
        if (buttonState == HIGH) {

          digitalWrite(ledPin, LOW);

          delay(375);

          ledState = 0;

        }
      }

    }



  }
}

Moderator edit: CODE TAGS. { sob }

Thank you, that looks spot on for the pushbutton to turn on a single led, but I am looking to turn on 6 leds in an array that loops from lowest to highest. So far, I have found an example by searching google and it looks like this:

#(const int buttonPin = 8; //pushbutton pin
int timer = 200;
int ledPins = {2,3,4,5,6,7}; //an array of pin #'s
int pinCount = 6; // the number of pins
int thisPin = pinCount - 1;

//variable will change
int buttonState = 0; // variable for reading pushbutton

void setup() {
int thisPin;
pinMode(buttonPin, INPUT);
for (int thisPin = 0; thisPin < pincount; thisPin++){
pinMode(ledPins[thisPin], OUTPUT);
}
}

void loop() {
buttonState = digitalRead(buttonPin); //check pushbutton if pressed buttonState is HIGH:
digitalWrite(ledPins[thisPin], HIGH);
//loop from lowest pin to the highest
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
digitalWrite(ledPins[thisPin], HIGH);
delay(60);
digitalWrite(ledPins[thisPin], LOW);

})

I am getting an error when I verify it though. the error reads: pinCount not declared in this scope

the error reads: pinCount not declared in this scope

pincount != pinCount.

Please use code tags

I altered the pinCount as suggested, yet I am getting a new error in the same spot saying "expected initializer ‘!’ before token…

const int buttonPin = 8; //pushbutton pin
int timer = 200;
int ledPins[] = {2,3,4,5,6,7}; //an array of pin #'s
int pinCount ! = pinCount; // the number of pins
int thisPin = pinCount - 1;

//variable will change
int buttonState = 0; // variable for reading pushbutton

void setup() {
int thisPin;
  pinMode(buttonPin, INPUT);
  for (int thisPin = 0; thisPin < pincount; thisPin++){
    pinMode(ledPins[thisPin], OUTPUT);
  }
}

void loop() {
  buttonState = digitalRead(buttonPin); //check pushbutton if pressed buttonState is HIGH:
  digitalWrite(ledPins[thisPin], HIGH);
  //loop from lowest pin to the highest
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
  digitalWrite(ledPins[thisPin], HIGH);
  delay(60);
  digitalWrite(ledPins[thisPin], LOW);
  
}
int pinCount ! = pinCount;

Even without the exclamation mark, that doesn’t make sense.
What are you trying to do?

for (int thisPin = 0; thisPin < pincount; thisPin++){

Still need to correct your spelling of “pinCount”

This sketch does turn the led on with one button press and off with the next press.
It does include a debounce section since it is looking for user presses and not noise.
My “button” is a jumper from pin 2 that I ground on the USB connector to “press”.

This is a real time code example. It does not need to complete a series of actions before loop() finishes. It has no hold-ups blocking execution. Note that the button part and the led part are separate code that pass information in variables.

This sketch works and has room to add more buttons, leds, whatever. As long as loop() runs fast it will remain more responsive than human senses can keep up with.

Jacks, if you can learn this then you can do many things at once on Arduino. Next thing is to learn how to make things happen on time/how to use the millis() and micros() timers.

// Push and let go the button once, led turns on. Again, led turns off.
// Button is debounced and wired directly to ground. 
// The button can be a jumper from pin 2 to the grounded USB connector.

const byte ledPin =  13;      // the number of the LED pin

const byte buttonPin = 2;
byte buttonRead;  // wired for pullup -- if down then LOW, if up then HIGH 
byte lastRead = HIGH; // so debounce knows previous read
const int buttonDebounceDone = 5000; // debounced when 5000 reads are the same
int buttonDebounceCount = 0; // for counting unchanged reads
byte buttonState = 0;  // this is after debouncing state.
// 0 = button is up after debounce
// 1 = button is down after debounce

byte processState = 0;  // this is tricky, the button gets pushed twice
// 0 = 1st press has not happened, led is OFF, looking for press
// 1 = 1st press is down, led is ON, looking for release
// 2 = 1st push relesased, led stays ON, looking for press
// 3 = 2nd press is down, led is OFF, looking for release
// there is no state 4, 2nd push release changes state to 0


void setup() 
{
  pinMode( ledPin, OUTPUT );  // default is LOW 
  pinMode( buttonPin, INPUT_PULLUP ); // my button connects to ground, not 5V
  // however that means that the pin is LOW when the button is pressed.
  buttonDebounceCount = -1; // start 'debounced'
}

void loop() // make sure that loop() runs fast and stays in the "now".
{
  // this is the button code, it handles debouncing.
  // loop() runs so fast that 5000 reads are done in less than 1/100th seconds.
  // button debounce is important 
  buttonRead = digitalRead( buttonPin ); // momentary state
  
  if ( buttonRead != lastRead )
  {
    buttonDebounceCount = 0; // Read changed, start the count over
  }
  else if ( buttonDebounceCount >= 0 )
  {
    buttonDebounceCount++;
    if ( buttonDebounceCount >= buttonDebounceDone ) // finally!
    {
      buttonState = !buttonRead; // mission accomplished, button is stable
      // note that buttonState is opposite buttonRead
      buttonDebounceCount = -1; // stop count check while read is the same
    }
  }
  lastRead = buttonRead;

// this is the led code that uses buttonState and processState
// note that this always runs, not just when the button is pressed
  switch ( processState )
 {
   case 0: // 0 = 1st press has not happened, led is OFF, looking for press
   if ( buttonState == 1 ) // button is pressed 
   {
     processState = 1;
     digitalWrite(ledPin, HIGH);
   }
   break; // note that until the 1st press, this case runs over and over
   
   case 1: // 1 = 1st press is down, led is ON, looking for release
   if ( buttonState == 0 ) // button is released 
   {
     processState = 2;
   }
   break; // note that until the 1st release, this case runs over and over
   
   case 2: // 2 = 1st push relesased, led stays ON, looking for press
   if ( buttonState == 1 ) // button is pressed 
   {
     processState = 3;
     digitalWrite(ledPin, LOW);
   }
   break; // note that until the 2nd press, this case runs over and over
   
   case 3: // 3 = 2nd press is down, led is OFF, looking for release
   if ( buttonState == 0 ) // button is released 
   {
     processState = 0; // back to the start
   }
   break; // note that until the 2nd release, this case runs over and over
 } 
}

edited – it was working on release instead of press, I made
buttonState = buttonRead; // mission accomplished, button is stable
into
buttonState = !buttonRead; // mission accomplished, button is stable
for the fix

edited again, changed the debounce count from 1000 to 5000 because my jumper-as-a-pin got past 100 and -still- bounced when I slid the pin on the connector!

So, I’m still working on this project. I have rewritten the code several times, and I have finally gotten it to a point where I really felt like it was going to work, so I verified it and uploaded the code, but nothing happens when I press the button… :(. I am hoping someone can look at this code and tell me where I am messing up. I added debounce for the button and everything. Thank you for your help ahead of time.

const int pushButton = 8; //pushButton input pin
const int ledPins[] = {
    2,3,4,5,6,7};//calls out led pin placement
int pinCount = 6; //number of pins
int timer = 200;
int buttonState = 0; //variable to read buttonState

//variables will change:
int ledState = HIGH;//the current state of the output pin
int lastButtonState = LOW;//the previous reading from the input pin

//the following variables are long's because the time, measured in milliseconds,
//will quickly become a bigger number than can be stored in int.
long lastDebounceTime = 0; //the last time the output pin wa toggled
long debounceDelay = 50; //the debounce time; increase if the output flickers

void setup() {
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 0; thisPin < pinCount; thisPin++)  {
    pinMode(thisPin, OUTPUT);      
    pinMode(pushButton, INPUT);

  }
}
  
void loop() {
  int reading = digitalRead(pushButton);
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  } 
  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;
      
      //only toggle the led if the new button state is high
      if (buttonState == HIGH) {
        ledState = !ledState;
        
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
  digitalWrite(ledPins[thisPin], HIGH);
  //loop from lowest pin to the highest
  delay(60);
  digitalWrite(ledPins[thisPin], LOW);
        }
      }
    }
  }
}

I didn't want to throw millis() at you yet, I like that way better but your debounce code is not going to work so well.

You never change lastButtonState from LOW.

  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

So every time the button reads HIGH, it sets the timer to 0.

You have blended you button code with your led code which will make changes a bit more difficult. I separate actions for a reason. I didn't need the 4 states of the state machine but I wanted to show you a basic state machine and didn't do the best job of it. It's just a way to set up separated code blocks to handle a task on an "as it comes" basis.

Also when you sketch is in delay() it won't see any button push. That's a 3.6 sec window while the 6 led sequence runs. Perhaps instead, set a variable that cues a separate block of code to use (millis() - X >= ledDelay) to run those even when the button has not been pushed which is why that code should be outside of the ((millis() - lastDebounceTime) > debounceDelay) block. Do you see why it can't work inside of that if?