O'Sullivan & Igoe, Physical Computing.
Page 91. Digital input (NO switch connected to microcontroller digital input with 10K pull-down resistor)
/* pcomp_091
* physical computing, p91
*
* NO switch connected to microcontroller digital input
* with 10K pull-down resistor
*/
// Switch connected to digital pin 2
int switchPin = 2;
void setup()
{
Serial.begin(9600);
pinMode(switchPin, INPUT);
}
void loop()
{
Serial.print("Read switch input: ");
Serial.println(digitalRead(switchPin));
delay(100);
}
Page 99. Digital output (connected to 220 ohm resistor, LED and ground)
/* pcomp_099
* physical computing, p99
*
* Microcontroller digital output connected to 220 ohm resistor,
* LED and ground. Flashes LED.
*/
// LED connected to digital pin 8
int ledPin = 8;
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
Page 105. Analog input (photocell)
/* pcomp_105
* physical computing, p105
*
* Photocell connected from +5V to analog pin 1; analog pin 1
* also connected via 10K resistor to ground (voltage divider)
*
* Prints changing values to serial monitor
*/
int sensorPin = 1;
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.print("Read sensor input: ");
Serial.println(analogRead(sensorPin));
delay(100);
}
Page 106. Analog input (10K potentiometer)
/* pcomp_106
* physical computing, p106
*
* Center pin of 10K pot connected to analog pin 1; other
* two pins connected to +5V and ground
*
* Prints changing values to serial monitor
*/
int sensorPin = 1;
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.print("Read sensor input: ");
Serial.println(analogRead(sensorPin));
delay(100);
}
Page 107. Analog input (photocell with RC circuit)
-
N.B. I tried to get this to work with Arduino's pulseIn command but couldn't. (Probably because that is the wrong command to use in this case.) I don't understand the RC circuit well enough yet to know what to expect. I measured the resistance of my photocell at about 1.3K when well-lit on my workbench, in case that helps choose an appropriate capacitor value. The BS2 has an rctime command, but there doesn't seem to be anything comparable on Arduino. (WJT)
Page 115. Low pass filter to smooth pulse-width modulated LED output
/* pcomp_115
* physical computing, p115
*
* Digital PWM output on pin 3 sent through 220 ohm resistor, through
* LED to +5V, also through 0.1 microfarad capacitor to ground
*
*/
int ledPin = 3;
void setup()
{
}
void loop()
{
for (int i=0; i <= 255; i++) {
analogWrite(ledPin, i);
delay(10);
}
}
Page 117. Piezo speaker hooked up to PWM pin 9
/* pcomp_117
* physical computing, p117
*
* Digital PWM output on pin 9 to piezo buzzer
*
*/
// For more information about tones, see Arduino tutorial on
// playing a melody
int ctone = 3830; // 261 Hz
int dtone = 3400; // 294 Hz
int etone = 3038; // 329 Hz
int speakerPin = 9;
long elapsedtime = 0;
long duration = 500000; // play each tone for about a half second (microseconds)
void setup()
{
pinMode(speakerPin, OUTPUT);
}
void loop()
{
while(elapsedtime < duration) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(ctone / 2);
digitalWrite(speakerPin, LOW);
delayMicroseconds(ctone / 2);
elapsedtime += ctone;
}
elapsedtime = 0;
delay(500);
while(elapsedtime < duration) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(dtone / 2);
digitalWrite(speakerPin, LOW);
delayMicroseconds(dtone / 2);
elapsedtime += dtone;
}
elapsedtime = 0;
delay(500);
while(elapsedtime < duration) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(etone / 2);
digitalWrite(speakerPin, LOW);
delayMicroseconds(etone / 2);
elapsedtime += etone;
}
elapsedtime = 0;
delay(500);
}

