// Copyright 2007, Sandeep Gangadharan // For more free scripts go to http://www.sivamdesign.com/scripts/

For this project, you’ll use three inputs to affect three outputs. Each button will change the brightness of an LED. Since each input does the same thing for its respective output, we will use a function to save some repetition in the code.

Code

/* * Tune a multicolor LED's colors with buttons * The analogWrite() function works on digital pins 3,5,6,9,10,11. */ int brightness; //holds the brightness of the outputs. Range is 0-255 //function that takes in the pin number and changes brightness on that pin void changeBrightness(int _pin){//'pass in' a pin number to the function brightness++; //increase the brightness variable by one analogWrite(_pin,brightness); //write 'brightness' to the pin you passed in. if(brightness == 255){ //reset brightness to 0 if it reaches max (255) brightness = 0; } } void setup(){ //Set all LEDS to output pinMode(9,OUTPUT); pinMode(10,OUTPUT); pinMode(11,OUTPUT); //one button per LED color pinMode(A5, INPUT_PULLUP); pinMode(A1, INPUT_PULLUP); pinMode(2, INPUT_PULLUP); } void loop(){ /* * When a button is held down, pass the corresponding LED pin number * to the function over and over again */ if(digitalRead(A5) == LOW){ changeBrightness(9); //pass the LED pin number to the function } else if(digitalRead(A1) == LOW){ changeBrightness(10); } else if(digitalRead(2) == LOW){ changeBrightness(11); } delay(10); //Speed of fade while button is LOW. Longer delay = slower fade } // (c) 2017 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

Challenges

How many can you complete? Change the code and hardware according to the challenges below. Upload your code to see the effect when you're finished. Complete a challenge? Check it off the list!

Videos

Read the Code Walkthrough Text (click to open)

Concepts

New Concept: Creating Functions

Functions are groups of code that run together. You use functions every time you write one of our programs- void setup() and void loop() are functions. When you're programming, you're filling out the contents of those functions.

When you use a function like digitalWrite() or tone(), you don't see the contents of the function, but you're using it by passing it arguments.

You can create your own functions in order to make your programs simpler. They're a labeled group of commands that you can use at any point in a program.  If you find yourself repeating the same statements multiple times within a program, consider making a function out of those commands. 

You should place your function outside of the void setup() and the void loop() functions. Put the function at the very top of your code. 

The first word in creating a function is its type. A void function simply does the code between the function's curly braces- it doesn't send a value back to the code. If you have a function that does a math equation, you may create an int function that can send a number back to the code and be used by the rest of the program.

The second word in the function is its name. It's best to name a function clearly so that you know what its job is.

The third part of the function is to define the arguments that the function requires to work. Not all functions require an input- they simply do their job when called. It's up to you to decide what information your function requires. When creating an argument for your function, you define what type of value it will be - int is usually fine for numbers. Then you create a name for that argument. This is the name you'll use for that value throughout the rest of the function. 

To require another argument, just type a comma, the next argument's type, and the next argument's name. You can require many arguments for a function to work, but it's recommended to keep the number low (3 or fewer) for simplicity. You can also create optional arguments, meaning that the argument will have a default value unless you call the function with that value changed. Optional parameters are a more advanced topic. 

After you've defined the type, name, and requirements of the function, you type an opening curly brace and then start typing the code that your function will run when called. When you want to use the value of the arguments that the computer passed your function, you type the name of the argument that you put in the parentheses.

For example, if you had a function that set pinMode to OUTPUT for any number you passed it, you might have:

void pinOut (int _pinNum){

      pinMode(_pinNum, OUTPUT);

}

That's not a very useful function (pinMode() is a function, too!), but it would work. 

Functions can contain if statements, loops, variables, and calls to other functions. They can contain a program that takes an hour to run- anything you define in between the curly braces. Once you've defined what your function will do, you can close the curly braces.

To make your function run, you have to call it. That is, you have to tell it to run and give it any information it requires in order to work. To call a function, you type its name and an open parentheses, the value you want to pass the function, and then a closing parentheses and a semicolon. 

A call to the pinOut function may look like:

pinOut(9);

There are multiple errors that can occur when you are using a function. For example, if you pass the function the wrong type of information, too much information, or too little information, it won't know what to do with that information and your code won't run. 

Functions are very useful in breaking up more complex code into separate pieces that can run alone.


Quiz

If you're having trouble, try to run a program that can help you find the answer.

1. When creating a function, where do you define what arguments the function will require?




2. When you call a function from the loop of your code, what code runs after the function has run?




3. When is a good time to think about creating a function?