Making Things Interactive

February 1, 2008

How to post code

Filed under: Course Materials,References — jet @ 6:18 pm

There’s a way to post code to the blog that makes it much easier to read:

  1. Start a new entry
  2. When you’re ready to insert your source, click on the “Code” tab in the editor
  3. Start a sourcecode block with the sourcecode tag: [sourcecode language='cpp']
  4. Paste in your PDE source after the tag.
  5. After your source code, close the sourcecode block with: [/sourcecode]
  6. Click the “Visual” tab to go back to visual editing mode.

Your code should now format much more nicely in the blog. If things look really screwy, go into the Code tab and make sure that the sourcecode tags are as they should be — some times cutting and pasting the tags don’t work and you’ll have to type them in by hand.  (We do it this way because WordPress will not let us upload .pde files as attachments to posts.)
Here’s an example of how it will display if it is working correctly:

/*
* led_switch  turn an LED on and off using an external switch
 */

int ledPin = 13;                // LED connected to digital pin 13, it has a resistor built in
int switchPin = 8;              // switch is connected to pin 8
int delayAmount = 500;          // the delay() command take the number of milliseconds, 500 is half a second
int switchStatus = 0;           // whether or not the switch is pushed

void setup()                    // run once, when the sketch starts
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
  pinMode(switchPin, INPUT);    // sets the digital pin as input
}

void loop()                     // run over and over again
{

  switchStatus = digitalRead(switchPin);  // read the pin

  if (switchStatus == HIGH)
   {                             // this block of  commands are only executed if switchStatus is HIGH
    digitalWrite(ledPin, HIGH);  //
    delay(delayAmount);          //
    digitalWrite(ledPin, LOW);   //
    delay(delayAmount);          //
  }                              //
}

int i;
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

Create a free website or blog at WordPress.com.

%d bloggers like this: