Move code into a function?

I'm making a toy tiger that flashes its eyes and growls. I'm trying to get them both to happen simultaneously when I push a button.

After many hours of trial and error (mostly error) I found the following code provided by UKHeliBob a few years ago for someone who just wanted to flash an LED twice (thanks UKHeliBob!).

It works fine for the tiger's eyes, so the code so far is fine. (I have the left eye LED programmed below and will just duplicate that code for the right eye later).

I also want the growl to happen simultaneously, so I'm thinking I'd like to make separate functions for the blinking and the growl.

So my question is, how do I get the following code into a function that activates when I push a button, but the button press is separate from the blinking so it can start the growl, too?

(I'd like the button to start several more functions in the future, all independent of each other -- so button-sense in one function/loop? and other actions in their own functions).

(I have the growl code elsewhere and will bring it into a function later if I can get some help on just getting the blinking into a function first.)

I've tried for more hours on making it into a function and can't get it. Sure could use some help.

Thanks!

//10k connected from Pin 2 to gnd
//switchPin button connected from Pin 2 to +5V

const byte switchPin = 2;
const byte leftEyePin = 5;
unsigned long startTime;
const unsigned long leftEyeLEDinterval = 50;
unsigned long leftEyeLEDtimer;
boolean flashing = false;
byte count = 0;
byte previousSwitchState = HIGH;

void setup ()
{
  pinMode (switchPin, INPUT);
  pinMode (leftEyePin, OUTPUT);
  leftEyeLEDtimer = millis ();
}

void loop ()
{
  if (!flashing)
  {
    // see if switch is open or closed
    byte switchState = digitalRead (switchPin);

    // has it changed since last time?
    if (switchState != previousSwitchState && switchState == LOW)
    {
      flashing = true;
      count = 0;
      startTime = millis();
    }
    previousSwitchState = switchState;  // remember for next time
  }
  else
  {
    unsigned long currentTime = millis();
    if (currentTime - startTime >= leftEyeLEDinterval)
    {
      digitalWrite(leftEyePin, !digitalRead(leftEyePin));
      startTime = currentTime;
      count++;
      if (count == 8)
      {
        flashing = false;
        digitalWrite(leftEyePin, HIGH);
      }
    }
  }
}

void toggleLeftEyeLED ()
{

}

I'll give you a hint: loop() is a function.

Not sure what you mean, but I want everything out of void loop () that doesn't have to be there.

Cut and paste isn't working today?

Thanks for your participation and I understand what you're tying to do by making me think, but unfortunately your hints are of no use to this non-programmer at all. I would welcome and appreciate some specific help from anyone who is willing to be more to the point.

MorganS:
I'll give you a hint: loop() is a function.

MorganS:
Cut and paste isn't working today?

Your problem description describes the solution...

callmebob:
I want everything out of void loop () that doesn't have to be there.

Take everything out. Paste it into the new function you already created. Add a call to that function inside loop().

Void loop()
{
Insidefunction();
}

Void insidefunction()
{
Tiger code things
}

The code in Several Things at a Time and in the more extensive Planning and Implementing a Program illustrates the use of functions to make programs manageable.

...R

MorganS -- Now that's the kind of help I can use! Thank you! -- I'm amazed at how simple some of these things can be with a little help. I know that you guys don't want to give away everything to the lazy guys who don't want to do any research. But for those of us who spend hours tearing our hair out trying to get it right before asking for help, it's fantastic.

MorganS:
Your problem description describes the solution...

Take everything out. Paste it into the new function you already created. Add a call to that function inside loop().

.
.

jokefox - thanks for taking it a step further. I'm happy to say that I did understand that, but your input is a great detailed addition.

jokefox:
Void loop()
{
Insidefunction();
}

Void insidefunction()
{
Tiger code things
}

.
.

Robin -- Believe me, I've studied your tutorials and tried your ideas for many, many hours -- but sometimes specific help on specific problems is what's really needed.

Robin2:
The code in Several Things at a Time and in the more extensive Planning and Implementing a Program illustrates the use of functions to make programs manageable.

...R

callmebob:
Robin -- Believe me, I've studied your tutorials and tried your ideas for many, many hours -

I didn't know.

If you have specific questions about parts of them I will try to help.

...R

Robin2:
I didn't know.

If you have specific questions about parts of them I will try to help.

...R

Thank you. I have used your tutorials to help understand and write a few projects, and I refer to them often. They certainly are helpful! They helped me in some of my hobby projects -- some that have worked out, and some like this one that I couldn't figure out with general tutorial info and have needed more specific help that I mentioned. Now we'll see if I make it through the the rest of this project!