Making Things Interactive

March 6, 2008

Mid-Term Project: Pushpin

Filed under: 7: Mid-Term Project,Assignments,Nadeem Haidary — nhaidary @ 12:11 am

The pushpin is fully assembled and working. A button on the front changes the color of an LED on the back of the pushpin, reflecting the light onto the wall. When you tilt the pushpin 90 degrees, a black square turns red, indicating that you can record audio. Tilting the pushpin back stops the recording and plays back the audio (as proof that it was recorded). Finally, when you click on the screen, it stores those audio files as .wav on to your computer. The device uses both Arduino and Processing software.

Labels
pushpin-solo_web.jpg
pushpin-on-_web.jpg
pushpinwiring_revised.jpg

Arduino:

 /*
 *   PUSHPINS
 *   Midterm project
 *   Spring 2008
 *
 *   Nadeem Haidary
 */

//LIBRARIES

//GLOBAL VARIABLES
//pins
int switchPin = 3;               // switch is connected to pin 3
int tiltPin = 2;                 // tilt sensor is connected to pin 2
int ledPin1 = 12;                // red LED is connected to pin 12
int ledPin2 = 11;                // green LED is connected to pin 11
int ledPin3 = 10;                // blue LED is connected to pin 10 ???

//reading the LED switch
int Sval;                        // variable for reading the switchPin status
int Sval2;                       // variable for reading the delayed switchPin status
int SbuttonState;                // variable to hold the swithcPin button state

//reading the tilt switch
int Tval;                        // variable for reading the tiltPin status
int Tval2;                       // variable for reading the delayed tiltPin status
int TbuttonState;                // variable to hold the tiltPin button state 

int lightMode = 0;               // What mode is the LED in?

//---SETUP-------------------------------------------------------------->

void setup() {
  pinMode(switchPin, INPUT);     // Set the switch pin as input
  pinMode(tiltPin, INPUT);       // Set the tilt switch as input

  pinMode(ledPin1, OUTPUT);      // Set the red LED pin as output
  pinMode(ledPin2, OUTPUT);      // Set the green LED pin as output
  pinMode(ledPin3, OUTPUT);      // Set the blue LED pin as output

  Serial.begin(9600);            // Set up serial communication at 9600bps

  SbuttonState = digitalRead(switchPin);     // read the initial state of the LED switch
  TbuttonState = digitalRead(tiltPin);       // read the initial state of the tilt switch
}

//---LOOP--------------------------------------------------------------->

void loop(){
  LEDchanger();                              // Function: reads switch to change LED color
  tiltRecorder();                            // Function: reads tilt, data to Processing
}

//---FUNCTIONS----------------------------------------------------------->

void LEDchanger() {
  Sval = digitalRead(switchPin);             // Read input value and store it in val
  delay(10);                                 // Wait before double-checking
  Sval2 = digitalRead(switchPin);            // Read the input again to check for bounces
  if (Sval == Sval2) {                       // If there readings are the same
    if (Sval != SbuttonState) {              // and the button state has changed
      if (Sval == LOW) {                     // check if the button is pressed
        if (lightMode == 0) {                // if LED is off
          lightMode = 1;                     // turn red LED on
        }
        else {
          if (lightMode == 1) {              // if red LED is on
            lightMode = 2;                   // switch to yellow LED
          }
          else {
            if (lightMode == 2) {            // if yellow LED is on
              lightMode = 3;                 // switch to green LED
            }
            else {
              if (lightMode == 3) {          // if green LED is on
                lightMode = 0;               // turn LED off
              }
            }
          }
        }
      }
    }
    SbuttonState = Sval;                     // save the new state of the switch
  }
  // Now do whatever the lightMode indicates
  if (lightMode == 0) {                      // LED is off
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
  }
  if (lightMode == 1) {                      // red
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
  }
  if (lightMode == 2) {                      // yellow
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
  }
  if (lightMode == 3)  {                     // green
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, HIGH);
  }
}  

void tiltRecorder() {

  Tval = digitalRead(tiltPin);               // Reads tilt sensor
    //Serial.println(Tval);
  if (Tval == 1) {                           // If switch is ON
    Serial.print(1, BYTE);                   // send 1 to Processing
  }
  else {                                     // If the switch is not ON,
    Serial.print(0, BYTE);                   // send 0 to Processing
  }
  delay(100);                                // Wait 100 milliseconds
}

Processing:

/**
*
* PUSHPINS
* Nadeem Haidary
* uses the serial and sonia Processing libraries
*
*/

//LIBRARIES
import pitaru.sonia_v2_9.*; // Import the Sonia sound library for recording, playing, and saving audio
import processing.serial.*; // Import the Serial library to communicate with Arduino

//GLOBAL VARIABLES & OBJECTS
//serial
Serial port; // Create object from Serial class
int val; // Data received from the serial port

//audio
int sampleCounter = 0; // the current sample we are recording/playing
int numSamples = 100; // the total number of samples we can use
Sample[] mySampleObj = new Sample[numSamples]; // The Sonia Sample object which we’ll record into
int startRecord = 0; // used to stop the loop from rerecording every frame
int startPlay = 1; // used to stop the loop from replaying every frame

//—SETUP————————————————————–>

void setup()
{
size(600, 600);
frameRate(10);
println(Serial.list()); // Set up the serial communication
port = new Serial(this, Serial.list()[1], 9600); // Open the port that the board is connected to (in this case COM4 = [1])
// and use the same speed (9600 bps)
Sonia.start(this); // Set up sound library
LiveInput.start(); // set up audio recording
int recTimeSec = 10; // Maximum recordable time
for (int i = 0; i < numSamples; i++) { // create 100 samples to record mySampleObj[i] = new Sample (44100*recTimeSec); } } //---LOOP--------------------------------------------------------------->

void draw() {
smooth();
noStroke();

if (0 < port.available()) { // If data is available, val = port.read(); // read it and store it in val } //println(val); background(255); // Set background to white if (val == 0) { // If the serial value is 0, fill(0); // set fill for rectangle to black if (startPlay == 0) { // Start playing (but prevent looping) LiveInput.stopRec(mySampleObj[sampleCounter]); // stop recording mySampleObj[sampleCounter].play(); // play back last recording println("PLAY"); // print PLAY sampleCounter++; // increment to next available sample startRecord = 0; // enable recording startPlay = 1; // disable playing (for loop) } } else { // If the serial value is not 0, fill(255, 30, 30); // set fill to red if (startRecord == 0) { // Start recording (but prevent looping) LiveInput.startRec(mySampleObj[sampleCounter]); // Record LiveInput data into the Sample object. // The recording will automatically end when all of the Sample's frames are filled with data. println("RECORD"); // print RECORD startPlay = 0; // enable playing startRecord = 1; // disable recording (for loop) } } rect(100, 100, 400, 400); // draw square } //---FUNCTIONS----------------------------------------------------------->

void mousePressed() { // If the mouse is clicked

for (int i = 0; i < sampleCounter; i++){ // go through all the samples that have been recorded mySampleObj[i].saveFile("test" + (i + 1)); // and save them (takes some time) println("saving file" + (i + 1)); // let us know when you saved each one } LiveInput.stop(); // stop audio recording Sonia.stop(); // stop Sonia exit(); // quit the Processing applet } [/sourcecode]

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: