Making Things Interactive

February 1, 2008

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

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: