|
Intro to Physical Computing Syllabus Research & Learning Other Class pages
ITP Help Pages |
Introduction to Physical Computing - Fall 2009Instructor: Rory Nugent (rory.nugent at nyu.edu) Links Syllabus Helpful Books
Student Blogs Wednesday Class Garrad Bradley Thursday Class Adebunmi Adeleke Final Project
Weekly Notes Week One
Blinking LED
// The following example code will blink an LED attached on pin 2
int ledPin = 2; // the led is connected on pin 2
void setup()
{
// set up the ledPin as an output
pinMode(ledPin,OUTPUT);
}
void loop()
{
digitalWrite(ledPin,HIGH); // turn the LED on
delay(1000); // pause for 1 second (1000 ms)
digitalWrite(ledPin,LOW); // turn the LED off
delay(1000); // pause for 1 second (1000 ms)
// rinse and repeat
}
LED switch control
// The following example code will turn an LED on and off based on
// the activity of a button switch
int ledPin = 2; // an LED will be attached on pin 2
int switchPin = 3; // a switch will be attached on pin 3
int switchState = 0; // a temporary variable for storing the switch state
void setup()
{
pinMode(ledPin,OUTPUT); // set up the LED pin as an output
pinMode(switchPin,INPUT); // set up the switch pin as an input
}
void loop()
{
// read in the current state of the switch
switchState = digitalRead(switchPin);
if(switchState == HIGH) // if the switch is pressed...
{
digitalWrite(ledPin,HIGH); // turn the LED on
}
else // if the switch is NOT pressed...
{
digitalWrite(ledPin,LOW); // turn the LED off
}
// rinse and repeat
}
Week Two
Print the analog readings from a potentiometer
// The following example code will read in the analog voltage from
// a potentiometer and print the values (0 - 1023) to your serial monitor
int potPin = 0; // the potentiometer is connected on analog pin 0
int analogValue = 0; // a variable to store the analog values
void setup()
{
Serial.begin(9600); // configures serial communication at 9600 bps (bits per second)
}
void loop()
{
analogValue = analogRead(potPin); // reads in the analog voltage and saves it to a variable
Serial.println(analogValue); // prints the analog value from the Arduino to your computer's serial monitor
delay(10); // pause 10 milliseconds, this will keep your Arduino from slamming your computer with messages
}
LED Light Dimmer
// The following example code will read in the analog voltage from
// a potentiometer and send it out to an LED on an analog out pin.
// The effect is that the LED will change brightness as your turn
// the knob of the potentiometer.
int potPin = 0; // the potentiometer is connected on analog pin 0
int ledPin = 3; // the LED is connected on digital PWM pin 3
int potValue = 0; // a variable to store the analog value of the pot
void setup()
{
Serial.begin(9600); // configures serial communication at 9600 bps (bits per second)
pinMode(ledPin, OUTPUT); // set the LED pin as an output
}
void loop()
{
potValue = analogRead(potPin); // reads in the analog voltage and saves it to a variable
Serial.println(potValue); // prints the analog value from the Arduino to your computer's serial monitor
analogWrite(ledPin, potValue / 4); // sends out an analog voltage related to the analog input voltage from the pot
// The analog input range is 0 - 1023 and the analog output range is 0 - 255 so you must divide by 4 for scaling
delay(10); // pause 10 milliseconds, this will keep your Arduino from slamming your computer with messages
}
Week Three
Week Four
Tone Generator
// The following example code will read in an analog reading from a potentiometer
// and use the reading to control the timing in which a small 8-ohm speaker is pulsed
int potPin = 0; // the potentiometer is connected to analog pin 0
int speakerPin = 2; // the speaker is connected to digital pin 2
int analogValue = 0;
void setup()
{
pinMode(speakerPin,OUTPUT); // define the speaker pin as an output, obviously
}
void loop()
{
analogValue = analogRead(potPin); // read an analog value fro the potentiometer
analogValue = map(analogValue, 0, 1023, 1912, 3830); // map the analog input range 0 - 1023 to the range that defines the frequency timing for an octave of notes
digitalWrite(speakerPin,HIGH); // turn the speaker pin on
delayMicroseconds(analogValue); // delay for a certain amount of microseconds
digitalWrite(speakerPin,LOW); // turn the speaker pin off
delayMicroseconds(analogValue); // delay for the same amount of microseconds
// funny enough, turning the speaker pin on and off very quickly at a certain rate will cause the speaker to generate a tone
}
Week Five
Week Six
Week Seven
Week Nine
Week Ten
Configuring an Xbee: The Arduino Method
void setup()
{
}
void loop()
{
delay(1);
}
Week Twelve
|