This reads in different resistance, and display different color rectangle.

Arduino

int analPin = 1;                       // Analog pin connected to pin 1
analPin

void setup() {
  pinMode(switchPin, INPUT);             // Set pin 0 as an input
  Serial.begin(9600);                    // Start serial communication at 9600 bps
}

void loop() {

  Serial.print(analogRead(analPin, Byte));
  delay(100);                            // Wait 100 milliseconds
}

*/

Processing side


import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port

void setup() 
{
  size(200, 200);
  // I know that the first port in the serial list on my mac
  // is always my  FTDI adaptor, so I open Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
}

void draw()
{
  if ( myPort.available() > 0) {  // If data is available,
    val = myPort.read();         // read it and store it in val
  }
  background(255);             // Set background to white
  if (val <500) {              // If the serial value is 0,
    fill(0);                   // set fill to black
  } 
  else {                       // If the serial value is not 0,
    fill(204);                 // set fill to light gray
  }
  rect(50, 50, 100, 100);
}
Advertisements

Exercise 03 – iBlast

Coming Spring 2009!

The idea is  to broadcast a rather personal devise, an iPod, to the general public. The device literally creates a “social circle” of reception that requires the audience to be in proximity with the source.

radio_01_modradio_02_mod1

radio_03_mod1

I emptied out the cassette compartment for the Ipod, and embedded a radio transmitter kit inside.

Song-Playing Shirt

For exercise three, I used a buzzer embedded in a shirt to play two holiday-themed songs, each activated by a button.  The buzzer plays songs by using an array of notes and an array of beats.  The notes are found by converting standard pitch notes into frequencies and then into periods using the formula period = 1/frequency.

Video:

http://www.vimeo.com/2049436

Code (adapted from buzzer code on Arduino website):

// TONES  ==========================================
// Start by defining the relationship between
//       note, period, &  frequency.
#define  lowa  4545    // 220 Hz
#define  lowb  4048    // 247 Hz
#define  c     3830    // 261 Hz
#define  d     3400    // 294 Hz
#define  e     3038    // 329 Hz
#define  f     2865    // 349 Hz
#define  g     2550    // 392 Hz
#define  a     2272    // 440 Hz
#define  as    2145    // 466 Hz  – calculated using period = 1/frequency
#define  b     2028    // 493 Hz
#define  C     1912    // 523 Hz
// Define a special note, ‘R’, to represent a rest
#define  R     0

// SETUP ============================================
// Set up speaker on a PWM pin (digital 9, 10 or 11)
int speakerOut = 9;
int button1 = 2;
int button2 = 4;
int state1 = LOW;
int state2 = LOW;
int reading1;
int previous1 = LOW;
int reading2;
int previous2 = LOW;

long time = 0;
long debounce = 200;

// Do we want debugging on serial out? 1 for yes, 0 for no
int DEBUG = 1;
int counter = 0;
int plays = 1;

void setup() {
pinMode(speakerOut, OUTPUT);
pinMode(button1, INPUT);
pinMode(button2, INPUT);
if (DEBUG) {
Serial.begin(9600); // Set serial out if we want debugging
}
}

// MELODY and TIMING  =======================================
//  melody[] is an array of notes, accompanied by beats[],
//  which sets each note’s relative length (higher #, longer note)
int melody[] = {
C,  b,  g,  C,  b,   e,  R,  C,  c,  g, a, C };
int happybirthday[] = {
c,c,d,c,f,e,R, c,c,d,c,g,f,R, c,c,C,a,f,e,d,R, as,as,a,f,g,f,R,R};
int heartandsoul[] = {
c,c,c, c,lowb,lowa,lowb,c,d, e,e,e, e,d,c,d,e,f, g,c, a,g,f,e,d,c,R,R };
int beats[]  = {
16, 16, 16,  8,  8,  16, 32, 16, 16, 16, 8, 8 };
int beatsHB[] = {
16,16,32,32,32,32,16, 16,16,32,32,32,32,16, 16,16,32,32,32,32,16, 16,16,32,32,32,32,16,16};
int beatsHS[] = {
32,32,96, 16,16,16,16,16,32, 32,32,96, 16,16,16,16,16,32, 64,96, 16,16,16,32,32,96, 32,32};
//int MAX_COUNT = sizeof(melody) / 2; // Melody length, for looping.
int MAX_COUNT = sizeof(heartandsoul) / 2;

// Set overall tempo
long tempo = 10000;
// Set length of pause between notes
int pause = 1000;
// Loop variable to increase Rest length
int rest_count = 100; //<-BLETCHEROUS HACK; See NOTES

// Initialize core variables
int tone = 0;
int beat = 0;
long duration  = 0;

// PLAY TONE  ==============================================
// Pulse the speaker to play a tone for a particular duration
void playTone() {
long elapsed_time = 0;
if (tone > 0) { // if this isn’t a Rest beat, while the tone has
//  played less long than ‘duration’, pulse speaker HIGH and LOW
while (elapsed_time < duration) {

digitalWrite(speakerOut,HIGH);
delayMicroseconds(tone / 2);

// DOWN
digitalWrite(speakerOut, LOW);
delayMicroseconds(tone / 2);

// Keep track of how long we pulsed
elapsed_time += (tone);
}
}
else { // Rest beat; loop times delay
for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count
delayMicroseconds(duration);
}
}
}

void loop() {

reading1 = digitalRead(button1);
reading2 = digitalRead(button2);

if(reading1 == LOW) {
if(counter < plays) {
for (int i=0; i<MAX_COUNT; i++) {
//tone = melody[i];
tone = happybirthday[i];
//tone = heartandsoul[i];
//beat = beats[i];
beat = beatsHB[i];
//beat = beatsHS[i];

duration = beat * tempo; // Set up timing

playTone();
// A pause between notes…
delayMicroseconds(pause);

if (DEBUG) { // If debugging, report loop, tone, beat, and duration
Serial.print(i);
Serial.print(“:”);
Serial.print(beat);
Serial.print(” “);
Serial.print(tone);
Serial.print(” “);
Serial.println(duration);
}
}
counter = counter + 1;
}
}
else if (reading2 == LOW) {
if(counter < plays) {
for (int i=0; i<MAX_COUNT; i++) {
//tone = melody[i];
//tone = happybirthday[i];
tone = heartandsoul[i];
//beat = beats[i];
//beat = beatsHB[i];
beat = beatsHS[i];

duration = beat * tempo; // Set up timing

playTone();
// A pause between notes…
delayMicroseconds(pause);

if (DEBUG) { // If debugging, report loop, tone, beat, and duration
Serial.print(i);
Serial.print(“:”);
Serial.print(beat);
Serial.print(” “);
Serial.print(tone);
Serial.print(” “);
Serial.println(duration);
}
}
counter = counter + 1;
}
}
else {
//do nothing
}
counter = 0;
}

Teach Me Kitty

The Teach Me Kitty is a toy designed to teach kids how to interact with cats without an actual cat.  Young children can be rough on pets before they learn the correct way to act with them.  This toy would hopefully spare the live animal that learning process.

Teach Me Kitty has 3 inputs and 2 outputs.  There is a button behind the left ear so that the user can “scratch” behind the ear.  There is a pressure sensor in the tail to sense when the user is “pulling” the tail.  And last, there is a tilt sensor in the body to sense when the user is “playing” with the toy.  The outputs are leg movements, run by a servo, and a noise ouput via a buzzer.

The servo has 4 rods, each of which goes down a leg.  When the leg movement begins, the servo rotates 90 degrees, waits, and rotates back.  This continues until the movement is over which creates a kind of twitching motion of the legs.  There are 2 different noises made by the buzzer, an angry noise and a purr.

Creating the toy’s personality was done by implementing a finite state machine.  The toy has 4 moods (or states): playful, tired, angry, and asleep. Each of these moods has some output.  When its playful, it wiggles its legs.  When it’s angry, it makes an angry noise.  When it’s tired it purrs and when it’s asleep it doesn’t do anything.

A tail pull is always negative input so pulling its tail puts the toy in the angry state.  Any tilt feedback while it’s angry is also negative, making it continue being angry.  Ear scratch feedback while angry is also usually negative (cats don’t like to be played with when they’re angry) but sometimes it will randomly become happy again.

An ear scratch is generally positive input.  Sometimes the toy will randomly get angry with the user after an ear scratch.  The toy can also get tired after being playful for a while.  After being tired for a while, the toy goes to sleep.

teach-me-kitty-code

Robotic Arm

The robotic arm

The robotic arm

Glad to move away from the clothing domain, I built a simple single degree of freedom robotic arm. All of the materials used to make the arm were scavenged from various scrap bins laying around the CMU Robotics Club. I also used this excercise to practice a little “improvisational building”, that is, I had no predefined design or plan of what this arm was going to look like, rather, I just started building and the form of the arm evolved as I built.  The robot uses two servos, one to control the position of the arm, and the other to control the opening and closing of the gripper.

The arm has two modes, which are selected by throwing a 3 postion switch to the left or right (the middle position is neutral). Flipping the switch to the right puts the arm in automatic mode. Explaining the automatic mode requires a bit of additional information about the area the robotic arm operates in. There is a small wooden block with a rounded recess for placing a small copper ball into. This rounded recess contains two wires, creating an open switch. When the copper ball is placed in the recess, the circuit is completed and the switch activated. The code running the robot then moves the robotic arm into a position so it can grab the ball from the block. After moving into position and pausing for a second, the gripper closes and grabs the ball. Then , the arm moves to a position to drop the ball into a square box. After dropping the ball, the arm returns to an idle position, and then conintues reading the checking the ball switch to see if another ball has set on the block. The robot will continuously pick and place the balls as long as it is fed them.

Flipping the mode selector switch to the left activates the manual control mode, giving the user control over the arm. Pressing the push button on the control panel toggles the gripper between the open and closed positions. Turing the knob, which is connected to a potentiometer, sets the position of the arm.

The code to operate the arm can be viewed here (.txt). It uses the ServoTimer1 library for controlling the servos.

Here is a short video demonstrating the robot’s capabilities:

Exercise Three

Use what you have learned so far to build a toy — any age group, any type, etc. — that employs sensors, actuators, and externally controlled power (using a relay or transistor). It can also be wearable, if you wish to continue the theme of the first month’s exercises.  If it makes sense, make your toy have memory, or state.

Present in class Thursday October 16.