Intro to Physical Computing Syllabus

Research & Learning

Other Class pages

Shop Admin

ITP Help Pages
Tom's pcomp site
DanO's pcomp site


Second Week

Spring 2009 home

Code from Class

  • Analog Input
//some constants for the pin that will never change
#define VarResistor 2

//make a variable to hold the values from the analog sensor (pot, photocell, thermisitor, etc)
//no need to initialize this particular one, but I do out of habit
int AnalogValue =0;


void setup(){
  //start serial communication, this way we can see the values coming from the sensor
  beginSerial(9600);
}

void loop(){
  //read the value of the sensor or variable resistor
  //and place it in our variable named AnalogValue
  AnalogValue=analogRead(VarResistor);
  //print the value of the resistor to the serial monitor
  Serial.println(AnalogValue);
  //take a moment to let the ADC "breathe"
  delay(10);
}
  • Analog Input with fading LED
//some constants for the pins that will never change
#define VarResistor 2
#define LEDPin 10

//make a variable to hold the values from the analog sensore (pot, photocell, thermisitor, etc)
//no need to initialize this particular one, but I do out of habit
int AnalogValue =0;


void setup(){
  //declare the LEDPin to be an output
  pinMode(LEDPin, OUTPUT);
  //start serial communication, this way we can see the values coming from the sensor
  beginSerial(9600);
}

void loop(){
  //read the value of the sensor or variable resistor
  //and place it n our variable named Analog Value
  AnalogValue=analogRead(VarResistor);
  //Dim or brighten up the LED based on the value from the sensor
  //make sure to divide by 4 because the analog output only has 8 bits of information
  //even though our input is 16 bits
  analogWrite(LEDPin, AnalogValue/4);
  //print the value of the resistor to the serial monitor
  Serial.println(AnalogValue);
  //take a moment to let the ADC "breathe"
  delay(10);
}
  Edit | View | History | Print | Recent Changes | Search Page last modified on January 30, 2009, at 09:06 AM