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;

Sleeping in the Dark

Filed under: 2: Term Project Idea, Assignments, Gee Kim — gskim @ 5:24 pm

So my new ideas for the final project…  I want to make a product that’s going to help children learn to sleep in the dark.

Catching Fireflies 

So for this project, I imagine a led light (aka firefly) that you can put into a clear jar.  The firefly will light up, and slowly dim out.  This will give time for your pupils to adjust in the dark, and for children to fall asleep as it gets darker in the room.  If the child needs more light, more fireflies can be placed into the jar.  Eventually, I would think that the child would be too tired to get up to put another firefly in the jar for more light… and so they would end up sleeping in the dark.

 Reading in the Dark

This would be a bedtime story book that adults can read to children right before they fall asleep.  It would be a book that lights up (around a certain radius of your finger) as you scroll through the text.  The book only lights up in the dark, so it encourages children to want to turn the light off for their bedtime story.  Eventually the child will fall asleep in the dark after their story, and if not, they can play with the book using it as a night light.

My Codes

Filed under: 3: LadyAda Tutorials, Assignments, Gee Kim — gskim @ 5:11 pm

My Blink

img_0446.jpg

int ledPin = 13;                // LED connected to digital pin 13

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

void loop()                     // run over and over again
{
digitalWrite(ledPin, HIGH);   // sets the LED on
delay(500);                  // waits for a second
digitalWrite(ledPin, LOW);    // sets the LED off
delay(900);                  // waits for a second
}

Alternating Green and Red

int redPin = 12;                  // Red LED connected to digital pin 12
int greenPin = 11;                // Green LED connected to digital pin 11

void setup()                      // run once, when the sketch starts
{
pinMode(redPin, OUTPUT);        // sets the digital pin as output
pinMode(greenPin, OUTPUT);      // sets the digital pin as output
}

void loop()                       // run over and over again
{
digitalWrite(redPin, HIGH);     // sets the Red LED on
digitalWrite(greenPin, LOW);   // sets the Green LED on
delay(500);                     // waits for half a second
digitalWrite(redPin, LOW);      // sets the Red LED off
digitalWrite(greenPin, HIGH);    // sets the Green LED off
delay(500);                     // waits for half a second
}

int redPin = 12;                  // Red LED connected to digital pin 12
int greenPin = 11;                // Green LED connected to digital pin 11
int bluePin = 10;

void setup()                      // run once, when the sketch starts
{
pinMode(redPin, OUTPUT);        // sets the digital pin as output
pinMode(greenPin, OUTPUT);      // sets the digital pin as output
pinMode(bluePin, OUTPUT);
}

void loop()                       // run over and over again
{
digitalWrite(redPin, HIGH);     // sets the Red LED on
digitalWrite(greenPin, HIGH);   // sets the Green LED on
digitalWrite(bluePin, HIGH);
delay(500);                     // waits for half a second
digitalWrite(redPin, LOW);      // sets the Red LED off
digitalWrite(greenPin, LOW);    // sets the Green LED off
digitalWrite(bluePin, LOW);
delay(500);                     // waits for half a second
}

Color Mixing

img_0451.jpg

int redPin = 12;                  // Red LED connected to digital pin 12
int greenPin = 11;                // Green LED connected to digital pin 11
int bluePin = 10;

void setup()                      // run once, when the sketch starts
{
pinMode(redPin, OUTPUT);        // sets the digital pin as output
pinMode(greenPin, OUTPUT);      // sets the digital pin as output
pinMode(bluePin, OUTPUT);
}

void loop()                       // run over and over again
{
digitalWrite(redPin, HIGH);     // sets the Red LED on
delay(500);                     // waits for half a second
digitalWrite(redPin, LOW);     // sets the Red LED on
delay(500);                     // waits for half a second
digitalWrite(redPin, HIGH);     // sets the Red LED on
digitalWrite(bluePin, HIGH);
delay(500);                     // waits for half a second
digitalWrite(redPin, LOW);     // sets the Red LED on
digitalWrite(bluePin, LOW);
delay(500);                     // waits for half a second
digitalWrite(bluePin, HIGH);
delay(500);                     // waits for half a second
digitalWrite(bluePin, LOW);
delay(500);                     // waits for half a second
digitalWrite(greenPin, HIGH);    // sets the Green LED off
digitalWrite(bluePin, HIGH);
delay(500);                     // waits for half a second
digitalWrite(greenPin, LOW);    // sets the Green LED off
digitalWrite(bluePin, LOW);
delay(500);                     // waits for half a second
digitalWrite(greenPin, HIGH);    // sets the Green LED off
delay(500);                     // waits for half a second
digitalWrite(greenPin, LOW);    // sets the Green LED off
delay(500);                     // waits for half a second
digitalWrite(greenPin, HIGH);    // sets the Green LED off
digitalWrite(redPin, HIGH);     // sets the Red LED on
delay(500);                     // waits for half a second
digitalWrite(greenPin, LOW);    // sets the Green LED off
digitalWrite(redPin, LOW);     // sets the Red LED on
delay(500);                     // waits for half a second
}

HelloWorld

void setup()                    // run once, when the sketch starts
{
Serial.begin(9600);           // set up Serial library at 9600 bps
Serial.println(“Hello world!”);  // prints hello with ending line break
}

void loop()                       // run over and over again
{
// do nothing!
}

HelloWorld 20 Times with Delay

void setup()                    // run once, when the sketch starts
{
Serial.begin(9600);           // set up Serial library at 9600 bps

}

void loop()                       // run over and over again
{
Serial.println(“Hello world!”);  // prints hello with ending line break
delay(1000);
}

DriveCalc


/*
* Drive size calculator!
*/

int drive_gb = 100;
long drive_mb;
long drive_kb;
long real_drive_mb;
long real_drive_kb;

void setup()                    // run once, when the sketch starts
{
Serial.begin(9600);           // set up Serial library at 9600 bps

Serial.print(“Your HD is “);
Serial.print(drive_gb);
Serial.println(” GB large.”);

drive_mb = drive_gb;
drive_mb = drive_mb * 1024;
drive_kb = drive_mb * 1024;

Serial.print(“In theory, it can store “);
Serial.print(drive_mb);
Serial.print(” Megabytes, “);
Serial.print(drive_kb);
Serial.println(” Kilobytes.”);

real_drive_mb = drive_gb;
real_drive_mb = real_drive_mb * 1000;
real_drive_kb = real_drive_mb * 1000;

Serial.print(“But it really only stores “);
Serial.print(real_drive_mb);
Serial.print(” Megabytes, “);
Serial.print(real_drive_kb);
Serial.println(” Kilobytes.”);

Serial.print(“You are missing “);
Serial.print(drive_kb – real_drive_kb);
Serial.println(” Kilobytes!”);
}

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

Turning on LED with Switch

img_0455.jpg

Class Notes – Arduino Basics

Filed under: Class Notes, Course Materials — jet @ 3:13 pm

Notes from the first Arduino session: Arduino-1

Blog at WordPress.com.