« »

Lab 4: I live but to servo

What a cool lab. As with all of the work in pcomp so far, I understand the code and I get how to apply it, but I’m still finding it frustrating that I would never in a million years be able to figure out how to write it myself. Say for instance Tom gave us the lab with no code–I’m pretty sure I wouldn’t be able to get the motor working. I feel a little bit like I’m just beginning to learn a new musical instrument or a new language: I’m playing other people’s pieces and repeating set phrases but am still miles from being able to compose something of my own. But at least I’m playing rather than learning scales and gerunds.

Which is good I guess, since theory always seems to catch me up. When I first attempted electronics in college with Paul Horowitz, I tried to follow the electrons around their circuitous routes and my brain short-circuited. I like ITP’s get your hands dirty first approach, though a little expectation management at the outset in terms of understanding would be reassuring–you’re going to feel lost, but don’t worry, it’s a good kind of lost, that kind of thing.

Setting up this lab was not particularly difficult.

I wired in a pot as per the instructions and then tried a bunch of other sensors, including a force sensor


and a photo sensor.


Each needed some adjusting of the maximum and minimum values being passed to the map() function. The photo sensor was by far the coolest, with a kind of action at a distance feel.

The photo sensor got me thinking about my project for this week. I think I’m going to build some variation on hide-and-seek. This weekend when I was playing said game with my four-year-old cousin, and felt a pump of adrenaline every time she was about to find me. There’s a nice visceral component to the game, a primordial hunter and prey dynamic that might lend itself nicely to an interface. I wanted my project this week to explore whether it’s possible to mimic this kind of play and get a similar response from a device that hides from you, but I think the servo is too slow. It may make a good haunted house project… I am however still toying with the idea of an interface that tries to avoid the user, possibly a small box covered in a stretchy material (maybe nylon stocking?) with a plunger at one end that rises and stretches the material when a photo sensor is getting lots of light and falling into the box when someone’s hand approaches. When Tom made a switch fade an LED when he held it down, I could really feel the switch (for some reason, I felt suction). I’m hoping that the interface I’m thinking of building will likewise have an associated feel.

Hide and seek also for some reason made me think of mushrooms, popping up at night and disappearing in the morning. Which made me think of sunflowers. If my hide and seek idea doesn’t work, I may build a sunflower that follows the light.

Ah the junk shelf! I found a perfect housing for my sheepish stretcher (working title). It’s some sort of computer part packaging.


I’ve made a cam with the spindle from a CD spindle.


I’ve hooked it all up to a servo that’s connected to a photo sensor. The initial result using the lab code is too bold; I need my little plunger to be more skittish. So I’ve adjusted as follows, reversing the minPulse and maxPulse to account for my motor position (NTS: test the servo before gluing it down). It works!


I also replaced the high and low bounds for the sensor in the map function with a variable that is initialized the first time the program runs to make sure that the sheepish stretcher works in all light conditions.

int servoPin = 2;     // Control pin for servo motor
int minPulse = 2500;   // Minimum servo position
int maxPulse = 500;  // Maximum servo position
int pulse = 0;        // Amount to pulse the servo
int highValue = 0;  // Initialization value for unobstructed light
boolean intro = true;  // Switch variable for initialization

long lastPulse = 0;    // the time in milliseconds of the last pulse
int refreshTime = 20; // the time needed in between pulses

int analogValue = 0;  // the value returned from the analog sensor
int analogPin = 0;    // the analog pin that the sensor's on

void setup() {
 pinMode(servoPin, OUTPUT);  // Set servo pin as an output pin
 pulse = minPulse;           // Set the motor position value to the minimum
 Serial.begin(9600);
}

void loop() {
 if(intro == true) {   
 highValue = analogRead(analogPin);
 intro = false;
 }
 analogValue = analogRead(analogPin);      // read the analog input
 pulse = constrain(map(analogValue,highValue-60,highValue,minPulse,maxPulse), 500, 2500);
 // convert the analog value and constrain it so that the servo doesn't roll over at high values

 // pulse the servo again if the refresh time (20 ms) have passed:
 if (millis() - lastPulse >= refreshTime) {
   digitalWrite(servoPin, HIGH);   // Turn the motor on
   delayMicroseconds(pulse);       // Length of the pulse sets the motor position
   digitalWrite(servoPin, LOW);    // Turn the motor off
   lastPulse = millis();           // save the time of the last pulse
 }
}

Then I tested the effect using the stretchy book cover I bought at Duane Reade:



I put processor and all the wiring inside the bottom of the casing:


The final result is pretty sweet.


Comments

Comments are closed.