Announcements

November 24, 2007
Reading for November 27th, are now posted. Enjoy!

October 2, 2007
To upload your thoughtless acts, create a new assignment page like any other lab. You'll see "Thoughtless Acts" listed as one of the assignment options.

May 24, 2008
This site has been archived and is no longer editable. Stay tuned for the next version, coming in the fall!


Message in a Bottle

Project Members: 
Aylin Selcukoglu

&uot&t

Description

Input and output coincidence. The idea behind this was connecting over distances and the idea of sending a message in a bottle. I wanted something that was along the lines of the ambient interfaces seen in lecture. There are two people involved in this interaction, the sender and the receiver. The input/output coincidence is for the receiver.

I created a "message in a bottle." I used the network library (along with apache) and created a little app that lets you send a melody which is received by the server (connected to Arduino via the serial library).

The bottle emits a blue light when there is no message. If there is a message, when the user is near the bottle it glows red (this was a little too tricky to do with a photocell, I wanted it to detect the shadow of the person but there was still too much light it was sensing...so maybe an ir sensor would be better). The receiver then opens the bottle and pressed the FSR to hear the song (I was afraid to bend my FSR too much by positioning it some other way). 

This was a little too ambitious, unfortunately. I got the server to work (when I go to http://localhost/client) but when I have someone else view it at http://192.168.1.3 (my ISP)/client the java applet doesn't load (it's just a blank screen).

Components Used

1 red and 1 blue (Light Emitting Diode)
2 10-ohm resistors (brown, black, orange, gold)
2 20-ohm resistors (red, red, brown, gold) 
1 photocell 
1 FSR 
1 piezo speaker 

Processing Code

/*
* Client
*/
 
import interfascia.*;
import processing.net.*;

// Font
PFont font;

// GUI textbox
GUIController c;
IFTextField t;
IFLabel l;

// Network client
Client cli;
String input;


void setup() {
  size(800, 225);
  background(255, 255, 255);

  // Connect to the server's IP address and port
  cli = new Client(this, "127.0.0.1", 12345); // server's IP and port
  
  font = loadFont("ArialMT-18.vlw");
  textFont(font, 18);
  fill(0, 0, 0);
  text("Send a melody using a string of notes [ c, d, e, f, g, a, b, C ] (e.g. cdedededefefef):", 27, 87);
  
  c = new GUIController(this);
  t = new IFTextField("Text Field", 25, 100, 750);
  //l = new IFLabel("Type something and press return", 25, 70);
 
  
  c.add(t);
  //c.add(l);
  
  t.addActionListener(this);
  
}

void draw() {
  
}

void actionPerformed(GUIEvent e) {
   String msg;
  
  // clear the screen 
  background(255, 255, 255); 
  textFont(font, 18);
  text("Send a melody using a string of notes [ c, d, e, f, g, a, b, C ] (e.g. cdedededefefef):", 27, 87);
 
  // if message was entered in textbox
  if (e.getMessage().equals("Completed")) {
   msg = t.getValue();
  
  // feedback
  textFont(font, 18);
  fill(0, 0, 0);
  text(msg + " sent successfully!", 27, 200);
  
  // send melody to Processing interfaced w/ Arduino
  cli.write(msg + "\n");
  }
}
 
  
/*
* Server 
*/ 
 
import processing.net.*;
import processing.serial.*;

Server s;
Client c;
String input;
int data[];
PFont font;
char val;
 
Serial port; // Create object from Serial class 

void setup() 
{
  size(800, 225);
  background(255, 255, 255);
  //stroke(0);
 // frameRate(5); // Slow it down a little
  font = loadFont("ArialMT-18.vlw");
  textFont(font, 18);
  fill(0, 0, 0);
  text("The melody sent was:", 27, 87);
  s = new Server(this, 12345); // Start a simple server on a port
  
  // Open the port that the board is connected to and use the same speed (9600 bps)
  port = new Serial(this, 9600);
 
}

void draw() 
  
  // Receive data from client
  c = s.available();

  if (c != null) {
    input = c.readString();
    input = input.substring(0, input.indexOf("\n")); // Only up to the newline
    
    // clear the screen 
    background(255, 255, 255); 
    textFont(font, 18);
    text("The melody sent was:", 27, 87);
    
    // send to serial
    port.write(input);
  
    fill(0, 0, 0);
    text(input, 27, 107);
  }
}
 
Arduino Code 
 
/* Message in a Bottle
 * ------------
 *
 * Program to play tones depending on the data coming from the serial port.
 *
 * The calculation of the tones is made following the mathematical
 * operation:
 *
 *       timeHigh = 1/(2 * toneFrequency) = period / 2
 *
 * where the different tones are described as in the table:
 *
 * note frequency period PW (timeHigh)
 * c        261 Hz        3830 1915
 * d        294 Hz        3400 1700
 * e        329 Hz        3038 1519
 * f        349 Hz        2864 1432
 * g        392 Hz        2550 1275
 * a        440 Hz        2272 1136
 * b        493 Hz        2028 1014 ;/div>
 * C        523 Hz        1912 956
 *
 * (cleft) 2005 D. Cuartielles for K3
 *
 * Updated by Tod E. Kurt <tod@todbot.com> to use new Serial. commands
 * and have a longer cycle time.
 *
 */

//int ledPin = 13;
int redPin = 10;
int bluePin = 9;
int speakerPin = 7;
int photo = 0;
int fsr = 1;

// note names and their corresponding half-periods  
byte names[] ={ 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};  
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
  
int serByte = -1;
int ledState = LOW;
int count = 0;

void setup() {
  //pinMode(ledPin, OUTPUT); 
  pinMode(speakerPin, OUTPUT);
  
  // start off being blue
  pinMode(bluePin, 255);
  pinMode(redPin, 0); 
  
  //beginSerial(9600);
  //Serial.println("ready");
}

void loop() {
  // if action in front of photocell
  // glow red
  
  // read from serial line
  
  // if FSR is pressed
  if() {
  
  digitalWrite(speakerPin, LOW);
  //serByte = Serial.read();
  //if (serByte != -1) {
    //Serial.print(serByte,BYTE);
    //ledState = !ledState;           // flip the LED state
    //digitalWrite(ledPin, ledState); // write to LED
  }
  for (count=0;count<=8;count++) {  // look for the note
    if (names[count] == serByte) {  // ahh, found it
      for( int i=0; i<50; i++ ) {   // play it for 50 cycles
        digitalWrite(speakerPin, HIGH);
        delayMicroseconds(tones[count]);
        digitalWrite(speakerPin, LOW);
        delayMicroseconds(tones[count]);
      }
    }
  }
 }
}
 

Item

clientserver


Powered by Drupal - Design by Artinet