Archive

Posts Tagged ‘function’

Writing Functions

July 16, 2012 Leave a comment

This is a simple concept so I’m going to get through it quickly. A function is a block of code that has a name and a property and is designed to be easily reusable. When you create a function, you are essentially creating a machine that does a certain job, and you can call upon it at any time to do that job.

A simple function would be one that adds two numbers together:

int sum(int x, int y)
{
	int ans = 0;  //holds the answer
	ans = x + y;  //calculates the sum
	return ans  //returns the answer
}

The first part “int sum(int x, int y)” is the header, and has a name “sum()“, parameters that go in between the parentheses “int x, int y“, and the type of return value desired “int …“.

Within the body of the function (whats between the brackets) is what will be done when the function is called upon. So, say that I called for sum(3,2), looking at the body of the function I can tell that the result will be 5.

We can use this with the Arduino in quite the same way, for example, if we wanted to turn the blink sketch into a function that blinked LED 13 a certain number of times (n) it would look something like this:

int ledPin = 13;
 int blinkVal = 3;
void setup()
 {
 pinMode(ledPin, OUTPUT);
 blinkTimes(blinkVal); //here the function is run once
 }
void loop(){}
void blinkTimes(int n) //function definition
 {
  for (int i = 0; i < n; i++)
  {
  digitalWrite(ledPin,HIGH);
  delay(1000);
  digitalWrite(ledPin,LOW);
  delay(1000);
  }
 }

The function in use is colored blue, the number of blinks is red, the function definition is green, and the defined parameter within the function is bold and black.

Now that you have the function defined you can put it at the bottom of your sketch and forget about it, calling it when ever you need the defined task done. However, the true power of functions comes from the ability to use multiple parameters. Say I want to be able to adjust not only the number of times that it blinks, but also the delay in between. That is easy enough to do, all I need is to add another parameter and modify the body of the function to include it:

void blinkTimes(int n, int d) //function definition
 {
  for (int i = 0; i < n; i++)
  {
  digitalWrite(ledPin,HIGH);
  delay(d);
  digitalWrite(ledPin,LOW);
  delay(d);
  }

So if I call the function blinkTimes(10,500); now it will blink 10 times turning on and off for 500 milliseconds.

Sources: [C4Learn, C4LearnMYCPLUS]

Advertisements

if Test

January 9, 2012 Leave a comment

The “if” is a test you give to the Arduino. In this test you make the questions and you determine what happens when it passes or fails. If the test that you made is passed, as in something happens that you wanted to happen, then you want the Arduino to do a certain action. Optionally, if the test is failed or something unexpected happens and the test is failed, you can tell the Arduino to take a different action.

If you wanted to make a test on the Arduino that would turn on a light for 5 seconds every time someone walked through a door it would look something like this:

if (PersonWalkingThroughDoor == 1) {
 digitalWrite(LIGHT, HIGH);
 delay(5000);
 digitalWrite(LIGHT, LOW);
 }

And a translated human readable version to understand whats going on:

if (a person walks through the door) {
 turn on the light;
 wait 5 seconds;
 turn off the light;
 }

Now, let’s try to understand the Arduino version of the test and see why it works…

A person can only be walking through the door or not, we can represent this as 1 or 0, respectively. Then, we can assign this state to a certain variable – which can be thought of as a digital box that can hold thing – we’ll call this variable “PersonWalkingThroughDoor”.

If a person is walking though a door our variable PersonWalkingThroughDoor would equal 1, otherwise it would be 0. Given this basic setup we can run a test, and that is exactly what this part of our code does:

if (PersonWalkingThroughDoor == 1) {

If our variable PersonWalkingThroughDoor is equal to 1, then it would pass our test and go into the action phase, where it would preform a certain task.

digitalWrite(LIGHT, HIGH);
delay(5000);
digitalWrite(LIGHT, LOW);
}

What happens in our “action phase” are just some basic commands, first we use digitalWrite() to set the pin attached to the LIGHT variable to HIGH (turning on the light), then we wait 5000 milliseconds, then we turn the light off.
Finally, if you want to expand the function of the test then you can include an “else” clause which would look something like this:


 if (PersonWalkingThroughDoor == 1) {
 digitalWrite(LIGHT, HIGH);
 delay(5000);
 digitalWrite(LIGHT, LOW);
 }
 else {digitalWrite(LIGHT, LOW);}

In that case, if the initial test is failed you can tell the Arduino to take a different action. In our case it would simply keep the light off if there is nobody walking through the door, and it truly inessential as the light is turned off by default after 5 seconds.

So that’s it, that’s the basic overview of an if function. You might know it, but the best way to understand it is to try it out yourself.