Help shortening code

I just got my Arduino yesterday and this is the first code I've put together entirely myself. Everything works but I feel like the part where it's deciding which function to call could be shortened a lot, I just don't know how. Can someone let me know if it's possible and how? Another question I have is if it's in the middle of running a function the button is pressed it doesn't know. Anyway to fix that or is it just something that has to be dealt with? My goal is to have one heck of a Halloween show and I figure that gives me about a month to learn how to program it.

/*
  * Simple LED Display with button to change patterns
  * 
  * Everytime the button is pressed it skips to the next pattern
  * 
  * Created by Cort Naab
  *
  * Circuit:
  * LED from ground to Pin 3 (w/ 220ohm resistor)
  * LED from ground to Pin 6 (w/ 220ohm resistor)
  * Button from ground to Pin 2
*/

const int buttonPin = 2; // select pin for button
int buttonCount = 0; // variable to store number of button presses
int buttonState = 1; // variable to store button state
int lastbuttonState = HIGH; // stores last button state
const int led1Pin = 3; //select led pins
const int led2Pin= 6;

void setup() {
   
  pinMode(buttonPin, INPUT); // Set pin mode of button
  digitalWrite (buttonPin, HIGH); 
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  Serial.begin(9600); // Initialize the serial com
  Serial.println("Serial is active");
}

void loop() {
  // read the current button state
  buttonState = digitalRead(buttonPin);
  
  // check to see if button state has changed
  if (buttonState != lastbuttonState) {
    // if the button state has changed and is now pressed then...
    if (buttonState == LOW) {
      buttonCount++;
      Serial.print("number of button pushes;  ");
      Serial.println(buttonCount);
    }
  }
  // Save current button state
  lastbuttonState = buttonState;
  // Set pattern to display
  if (buttonCount == 0) {
    blinkLED();
  }
  if (buttonCount == 1) {
    alternateLED();
  }
  if (buttonCount == 2) {
    pulseLED();
  }
  if (buttonCount == 3) {
    buttonCount = 0;
  }
}

void blinkLED() {
  digitalWrite(led1Pin, HIGH);
  digitalWrite(led2Pin, HIGH);
  delay(1000);
  digitalWrite(led1Pin, LOW);
  digitalWrite(led2Pin, LOW);
  delay(1000);
}

void alternateLED() {
  digitalWrite(led1Pin, HIGH);
  digitalWrite(led2Pin, LOW);
  delay(1000);
  digitalWrite(led1Pin, LOW);
  digitalWrite(led2Pin, HIGH);
  delay(1000);
}

void pulseLED() {
  digitalWrite(led1Pin, HIGH);
  digitalWrite(led2Pin, HIGH);
  delay(50);
  digitalWrite(led1Pin, LOW);
  digitalWrite(led2Pin, LOW);
  delay(50);
  digitalWrite(led1Pin, HIGH);
  digitalWrite(led2Pin, LOW);
  delay(50);
  digitalWrite(led1Pin, LOW);
  digitalWrite(led2Pin, HIGH);
  delay(50);
}

This pattern:

  if (buttonCount == 0) {
    blinkLED();
  }
  if (buttonCount == 1) {
    alternateLED();
  }
  if (buttonCount == 2) {
    pulseLED();
  }
  if (buttonCount == 3) {
    buttonCount = 0;
  }

can be expressed as:

  switch (buttonCount) {
  case 0: blinkLED(); break;
  case 1: alternateLED(); break;
  case 2: pulseLED(); beak;
  case 3: buttonCount = 0; break
}

Another question I have is if it's in the middle of running a function the button is pressed it doesn't know. Anyway to fix that or is it just something that has to be dealt with?

See the blink without delay tutorial:
http://arduino.cc/en/Tutorial/BlinkWithoutDelay

You might also want to investigate button debouncing. There are several pieces of code in the playground that can do this.
http://arduino.cc/playground/Main/InterfacingWithHardware#phi_interfaces

:slight_smile: Thanks for the help guys. Time for me to move onto an actual project. Rest assured you will be hearing from me again.

Working code:

/*
  * Simple LED Display with button to change patterns
  * 
  * Everytime the button is pressed it skips to the next pattern
  * 
  * Created by Cort Naab
  *
  * Circuit:
  * LED from ground to Pin 3 (w/ 220ohm resistor)
  * LED from ground to Pin 6 (w/ 220ohm resistor)
  * Button from ground to Pin 2
*/

const int buttonPin = 2; // select pin for button
int buttonCount = 0; // variable to store number of button presses
int buttonState = 1; // variable to store button state
int lastbuttonState = HIGH; // stores last button state
const int led1Pin = 3; //select led pins
const int led2Pin= 6;
int fadeRate = 5;
int blinkState = 0;
int fade = 35; //setting initial fade brightness
long previousTime = 0;
long blinkInterval = 1000;
long altInterval = 600;
long pulseInterval = 50;
long fadeInterval = 20;
unsigned long currentTime = millis();

void setup() {
   
  pinMode(buttonPin, INPUT); // Set pin mode of button
  digitalWrite (buttonPin, HIGH); 
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  Serial.begin(9600); // Initialize the serial com
  Serial.println("Serial is active");
}

void loop() {
  // update current time
  currentTime = millis();
  // read the current button state
  buttonState = digitalRead(buttonPin);
  
  // check to see if button state has changed
  if (buttonState != lastbuttonState) {
    // if the button state has changed and is now pressed then...
    if (buttonState == LOW) {
      buttonCount++;
      Serial.print("number of button pushes;  ");
      Serial.println(buttonCount);
    }
  }
  // Save current button state
  lastbuttonState = buttonState;
  // Set pattern to display
  switch (buttonCount) {
    case 0: blinkLED();
    case 1: alternateLED();
    case 2: pulseLED();
    case 3: fadeLED();
    case 4: buttonCount = 0;
}

void blinkLED(){
  if (currentTime - previousTime > blinkInterval){ // checks to see if the interval has passed
   previousTime = currentTime; // updates previousTime
   if (blinkState == 1)
   {
     digitalWrite(led1Pin, LOW);
     digitalWrite(led2Pin, LOW);
     blinkState = 0;
   }
   else
   {
     digitalWrite(led1Pin, HIGH);
     digitalWrite(led2Pin, HIGH);
     blinkState = 1;
   }
  }
}

void alternateLED() {
  if (currentTime - previousTime > altInterval){
    previousTime = currentTime;
    if (blinkState == 0){
      digitalWrite(led1Pin, HIGH);
      digitalWrite(led2Pin, LOW);
      blinkState = 1;
    }
    else 
    {
      digitalWrite(led1Pin, LOW);
      digitalWrite(led2Pin, HIGH);
      blinkState = 0;
    }
  }
}

void pulseLED() {
  if (currentTime - previousTime > pulseInterval){
    previousTime = currentTime;
    if (blinkState == 0){
      digitalWrite(led1Pin, HIGH);
      digitalWrite(led2Pin, HIGH);
      blinkState = 1;
   }
    else if (blinkState == 1){
      digitalWrite(led1Pin, LOW);
      digitalWrite(led2Pin, LOW);
      blinkState = 2;
   }
    else if (blinkState == 2){
      digitalWrite(led1Pin, HIGH);
      digitalWrite(led2Pin, LOW);
      blinkState = 3;
    }
    else
    {
      digitalWrite(led1Pin, LOW);
      digitalWrite(led2Pin, HIGH);
      blinkState = 0;
    }
  }
}

void fadeLED() {
  if (currentTime - previousTime > fadeInterval){
    previousTime = currentTime;
    analogWrite(led1Pin, fade);
    analogWrite(led2Pin, -fade);
    fade = fade + fadeRate;
    if (fade == 35 || fade == 255){
      fadeRate = -fadeRate ;
    }
  }
}

Your switch lacks "break"s, so each "case" will fall into the next when each function returns.
Is that what you intended?

My bad, I forgot to recopy the code after I fixed that. Here is the working code.

/*
  * Simple LED Display with button to change patterns
  * 
  * Everytime the button is pressed it skips to the next pattern
  * 
  * Created by Cort Naab
  *
  * Circuit:
  * LED from ground to Pin 3 (w/ 220ohm resistor)
  * LED from ground to Pin 6 (w/ 220ohm resistor)
  * Button from ground to Pin 2
*/

const int buttonPin = 2; // select pin for button
int buttonCount = 0; // variable to store number of button presses
int buttonState = 1; // variable to store button state
int lastbuttonState = HIGH; // stores last button state
const int led1Pin = 3; //select led pins
const int led2Pin= 6;
int fadeRate = 5; // fadeRate used in fadeLED() function
int blinkState = 0; // placeholder for functions to remember where they last were
int fade = 35; //setting initial fade brightness
long previousTime = 0; // gets set later by functions and is used in a operation that replaces "delay"
long blinkInterval = 1000; // delay length of blink function
long altInterval = 600; // delay length of alternate funcation
long pulseInterval = 50; // delay legth of pulse function
long fadeInterval = 20; // delay rate of fade function
unsigned long currentTime = millis(); /* sets currentTime to millis that the machine has been on
                                       * this number gets very large very fast so it needs to be in 
                                       * long format.
                                       */

void setup() {
   
  pinMode(buttonPin, INPUT); // Set pin mode of button
  digitalWrite (buttonPin, HIGH); 
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  Serial.begin(9600); // Initialize the serial com
  Serial.println("Serial is active");
}

void loop() {
  // update current time
  currentTime = millis();
  // read the current button state
  buttonState = digitalRead(buttonPin);
  
  // check to see if button state has changed
  if (buttonState != lastbuttonState) {
    // if the button state has changed and is now pressed then...
    if (buttonState == LOW) {
      buttonCount++;
      Serial.print("number of button pushes;  ");
      Serial.println(buttonCount);
    }
  }
  // Save current button state
  lastbuttonState = buttonState;
  // Set pattern to display based on function
  switch (buttonCount) {
    case 0: blinkLED(); break;
    case 1: alternateLED(); break;
    case 2: pulseLED(); break;
    case 3: fadeLED(); break;
    case 4: buttonCount = 0; break;
}

/* There are only notes on the first and last function because 
   the middle functions are essentially the same as blinkLED
*/
void blinkLED(){
  /* This replaces the delay you would normally need for "blink"
   * It checks to see if the set interval has passed
   * if it has lets the prgram move on, otherwise skips the rest
   * of this function. This is done to allow a (almost) constant
   * recheck of whether the button has been pressed. Without it
   * button presses get missed during delays
  */
  if (currentTime - previousTime > blinkInterval){ 
    // updates previousTime
    previousTime = currentTime;

   // blinkState is used throughout the entire program as a spaceholder
   if (blinkState == 1) 
   {
     // turn both LED's off
     digitalWrite(led1Pin, LOW);
     digitalWrite(led2Pin, LOW);
     blinkState = 0;
   }
   else
   {
     // turn both LED's on
     digitalWrite(led1Pin, HIGH);
     digitalWrite(led2Pin, HIGH);
     blinkState = 1;
   }
  }
}

void alternateLED() {
  if (currentTime - previousTime > altInterval){
    previousTime = currentTime;
    if (blinkState == 0){
      digitalWrite(led1Pin, HIGH);
      digitalWrite(led2Pin, LOW);
      blinkState = 1;
    }
    else 
    {
      digitalWrite(led1Pin, LOW);
      digitalWrite(led2Pin, HIGH);
      blinkState = 0;
    }
  }
}

void pulseLED() {
  if (currentTime - previousTime > pulseInterval){
    previousTime = currentTime;
    if (blinkState == 0){
      digitalWrite(led1Pin, HIGH);
      digitalWrite(led2Pin, HIGH);
      blinkState = 1;
   }
    else if (blinkState == 1){
      digitalWrite(led1Pin, LOW);
      digitalWrite(led2Pin, LOW);
      blinkState = 2;
   }
    else if (blinkState == 2){
      digitalWrite(led1Pin, HIGH);
      digitalWrite(led2Pin, LOW);
      blinkState = 3;
    }
    else
    {
      digitalWrite(led1Pin, LOW);
      digitalWrite(led2Pin, HIGH);
      blinkState = 0;
    }
  }
}

// this function uses the internal PWM by through the analogWrite command
void fadeLED() {
  // setting the delay same as blinkLED()
  if (currentTime - previousTime > fadeInterval){
    previousTime = currentTime;
    
    // turns on LED 1 to brightness they were left off at or initial brightness set up top
    analogWrite(led1Pin, fade);
    // turns on LED 2 to the inverse brightness
    analogWrite(led2Pin, -fade);
    // Changes fade value by adding rate specified up top
    fade = fade + fadeRate;
    // when the program reaches maximum fade 0 or 255 this reverses its direction
    if (fade == 35 || fade == 255){
      fadeRate = -fadeRate ;
    }
  }
}