Making Things Interactive

February 14, 2008

Course Notes – 12 Feb 08

Filed under: Class Notes,Course Materials — jet @ 10:49 am

Some examples from Tuesday. We’ll go over this more next week, but this is a good starting point.


// subroutine examples.

// To use subroutines you need two things:
// 1) a subroutine definition
// 2) a call to the subroutine.

// You make a subroutine definition using the following grammar:

// void NAME(ARGUMENTS)
// {
// BODY
// }

// NAME: the name of your subroutine.  Letters and numbers and
// underscores are ok

// ARGUMENTS: if your subroutine needs information to do something, you
// put it here in a comma seperated list:

// BODY: the statements you want the subroutine to do for you.

// Examples:

// a subroutine with no arguments that turns pin 13 on and off
// void Blink13()
// {
//     digitalWrite(13,HIGH);
//     delay(500);
//     digitalWrite(13,LOW);
//     delay(500)
// }
// and in the code, you'd use it like this:
// Blink13();

// a subroutine with one argument that turns a pin on and off.
// which pin is turned on and off is determined by the argument
// "int pin".
// void BlinkLed(int pin)
// {
//     digitalWrite(pin,HIGH);
//     delay(500);
//     digitalWrite(pin,LOW);
//     delay(500)
// }
// and in the code, you'd use it like this:
// BlinkLed(13); 

// a subroutine with two arguments that turns a pin on and off, which
// pin is turned on and off and how long the delay is are determined
// by the first two arguments:
// void BlinkLedDelay(int pin, int amount)
// {
//     digitalWrite(pin,HIGH);
//     delay(amount);
//     digitalWrite(pin,LOW);
//     delay(amount)
// }
// and in the code, you'd use it like this:
// BlinkLedDelay(13, 500); 

//  Note: "subroutine" and "function" are effectively interchangeable.
//  Which you use often has more to do with what language you learned
//  first than with anything else.
//  Personally, I use "subroutine" to mean something that does
//  something and "function" to be something that returns a value (more
//  on that next week).

// our output pins
int redLed = 10;
int blueLed = 12;
int greenLed = 13;

// our status pin
int statusLed = 13;

// we use this in more than one place, so set it as a variable
int startingDelay = 500;

// our default delay amount for the examples
int delayAmount = startingDelay;

// the delay amount for the status pin
int statusDelay = 200; 

void setup()
{
    pinMode(statusLed,OUTPUT);

    pinMode(redLed,OUTPUT);
    pinMode(blueLed,OUTPUT);
    pinMode(greenLed,OUTPUT);
}

void loop()
{
    // blink the status LED briefly using the LedBlinkDelay
    LedBlinkDelay(statusLed, statusDelay);
    // or we could have a dedicated status blink that takes
    // no arguments
    // BlinkStatusLed();

    delayAmount = delayAmount + 100;
    if (delayAmount > 2000) {
	delayAmount = startingDelay;
    }   

    LedBlinkDelay(redLed, delayAmount);
    LedBlinkDelay(blueLed, delayAmount);
    LedBlinkDelay(greenLed, delayAmount);

}

// this function
void LedBlinkDelay(int myPin, int myDelay)
{
    digitalWrite(myPin, HIGH);
    delay(myDelay);
    digitalWrite(myPin, LOW);
    delay(myDelay);
}

void BlinkStatusLed()
{
    digitalWrite(statusLed, HIGH);
    delay(statusDelay);
    digitalWrite(statusLed, LOW);
    delay(statusDelay);
}
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: