Intro to Physical Computing Syllabus

Research & Learning

Other Class pages

Shop Admin

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


Tom Class

Code example from September 16 afternoon class

void setup() {
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
}

void loop() {
  int sensorValue = analogRead(0);
  Serial.print("Sensor value = ");
  Serial.println(sensorValue, DEC);

  int outputValue = map(sensorValue, 250, 950, 0, 1023);

  Serial.print("\t output value = ");
  Serial.println(outputValue, DEC);

  if (sensorValue > 100) {
    digitalWrite(2, HIGH);
  } 
  else {
    digitalWrite(2, LOW);    
  }

  if (sensorValue > 300) {
    digitalWrite(3, HIGH);
  } 
  else {
    digitalWrite(3, LOW);
  }

  if (sensorValue > 800) {
    digitalWrite(4, HIGH);
  } 
  else {
    digitalWrite(4, LOW);
  }
}

Fading example from Sept 30 morning class

 /*
   Fading effect
  
  This sketch turns on an LED when the switch goes from off to on.
  The LED then fades to zero slowly, whether or not the swith is pressed.
  
  This is an example of triggering an action that continues beyond
  the switch press.
  
  Created 30 Sep 2009
  by Tom Igoe
  
  */

 int fadeLevel = 0;

 int lastSwitchState = LOW;

 void setup() {
   pinMode(3, INPUT);

   pinMode(9, OUTPUT);
   Serial.begin(9600);
 }

 void loop() {
   // read the switch:
   int switchState = digitalRead(3);

   // if the switch has changed, 
   if (switchState != lastSwitchState) {
     // see if the swtich went from off to on:
     if (switchState == HIGH) {
       Serial.println("On");
       // set the led's level to maximum:
       fadeLevel = 255; 
     }
     // save the switch state for next time thru the loop:
     lastSwitchState = switchState; 
   }


   // if the LED is on at all, make it dimmer:
   if (fadeLevel > 0) {
     fadeLevel--;
     analogWrite(9, fadeLevel); 
     delay(3);
   }
 }
#include <Tone.h>

Tone noiseMaker(9);
int noteValue[] = { NOTE_G4, NOTE_A4,  NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_FS5, NOTE_G5  };


void setup() {
  noiseMaker.begin();
}


void loop() {
  int sensorValue = analogRead(0);
  int pitch = map(sensorValue, 0, 1023, 0, 7);
  noiseMaker.play(noteValue[pitch]);
}

From the afternoon class:

#include <Tone.h>

Tone noiseMaker(9);
int noteValue[] = { 
  NOTE_G4, NOTE_A4,  NOTE_B4, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_FS5, NOTE_G5  };

int whichNote = 0;             // which of the 8 notes we're playing
int lastSwitchState = LOW;     // the state of the switch last time thru the loop
long timestamp = 0;            // the time the switch went from off to on
long duration = 0;             // how long the switch was pressed for

void setup() {
  // set the switch I/O pin to be an input:
  pinMode(3, INPUT);
  // initialize the tone library:
  noiseMaker.begin();
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the switch:
  int switchState = digitalRead(3);


  // see if the switch has changed:
  if (switchState != lastSwitchState) {
    Serial.println(switchState);
    if (switchState == HIGH) {
      //increment the note position:
      Serial.println("Switch went on!");
      timestamp = millis();

      // increment the note value if it's less than
      // the length of the array. Otherwise, set it
      // back to 0:
      if (whichNote < 7 ) {
        whichNote++;
      } 
      else {
        whichNote = 0; 
      }
    }  
    else {
      // the switch went from on to off:
      duration = millis() - timestamp;
      Serial.print("duration: " );
      Serial.println(duration);
    }


    // save the current state of the switch for the
    // next time through the loop:
    lastSwitchState = switchState;
  }

  // play a note
  noiseMaker.play(noteValue[whichNote]);
}


New version of the fade example, without delay()

int fadeLevel = 0;      // the level of the LED
int lastSwitchState = LOW;

long timeStamp = 0;
long interval = 1000;

void setup() {
  pinMode(3, INPUT);
  pinMode(9, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // read the switch:
  int switchState = digitalRead(3);

  // if the switch has changed, 
  if (switchState != lastSwitchState) {
    // see if the swtich went from off to on:
    if (switchState == HIGH) {
      Serial.println("On");
      // set the led's level to maximum:
      fadeLevel = 255; 
    }
    // save the switch state for next time thru the loop:
    lastSwitchState = switchState; 
  }


  // if the LED is on at all, make it dimmer:
  if (millis() - timeStamp > interval) {
    if (fadeLevel > 0) {
      fadeLevel = fadeLevel - 50;
      analogWrite(9, fadeLevel); 
    }
    timeStamp = millis();
  }
}


long blinkInterval = 65537;    // time between blinks
long lastTimeBlinked = 0;      // last time the LED was blinked
long toneInterval = 3000;      // time between beeps
long lastTimeToned = 0;        // last time the thing beeped


void setup() {
  // do setup stuff
}

void loop() {
  int sensorReading = analogRead(0);

  // tone every 3 seconds:
  if  (millis() - lastTimeToned > toneInterval) {
    // play tone
    // save last time toned:
    lastTimeToned = millis(); 
  }

  // blink about every 65.537 seconds
  if  (millis() - lastTimeBlinked > blinkInterval) {
    // blink LED
    // save last time blinked:
    lastTimeBlinked = millis(); 
  }
}

How to parse characters from an incoming string:




while (Serial.available() > 0) {
  // read a byte:

  int thisByte = Serial.read();

  switch (thisByte) {
  case 'A':
    // turn on the servo for A
    break;

  case 'B':
    break;
  case 'C':
    break;  
  default:
    // what to do when it's a character you don't care about:
  }
}

  Edit | View | History | Print | Recent Changes | Search Page last modified on November 25, 2009, at 10:39 AM