Friday, February 15, 2008

Lab 4 - Servo; Analog Output

I'm looking heavily into upgrading my documentation capabilities with a better digital camera, having enlisted advice from Rob, ITP students, my Dad, and consumer reports online. I can't decide whether to go really high-end or just find a better portable camera (money may make this choice for me). Anyway, for kicks I checked out the worst camera from the ER for this lab and it still worked better than mine, plus I tried a couple short movies with it which you'll also find below. Today I'll go to B&H to try out some cameras.



By now I understand the basics behind preparing a simple circuit on a breadboard, and following the photos and schematics is never that hard. Still, it took me a minute to realize the servo outputs can't go directly into the Arduino but need a bypass first on the breadboard - this involves tampering with the 3 header pins with needle-nose pliers to get them long enough to seat. I color coded the analog output leads to match the servo's leads, and used one blue lead in series with my photoresistor, since I didn't own an FSR with 2 entwined leads (as shown in the lab demo picture). I followed the code exactly and it did produce physical results, a 180 degree turn with every pinch or light variation on the resistor, however the serial monitor did not display any data. Also, the motor did not respond well to a thermistor, so that and the serial issue together leave some unanswered questions.

But look at my movie!



For kicks I tried the $15 stretch sensor I'd bought a few days back - I didn't bother to solder leads to it but if I use this in a project down the road then I probably should. For lab purposes, the servo responded fine to my stretch sensor.



The main issue I'm having, and with all the labs is, what can I do with all this? I'm not conjuring anything original like some of the students have shared, and wondering when my stroke of inspiration will begin. I'm not even quite sure how to attach other blades to the servo and what kinds of things it can power, so I'll have to read some forums online I suppose. Not being an ITP student, I'm not as "immersed" in the culture here as much as some, so I took it upon myself to check out some of the advanced projects currently on the floor... knowing now how much effort goes into one little lab, I'm amazed by the things I've seen: The inkjet cartridge guitar string machine, the scantron test robot, the Torrini magician documentary box, and a seesaw with an Arduino on it that I didn't get to see work but seems really interesting too.

When it gets to project time, I would like to incorporate materials for both this class and my Adv MAX/MSP class. On the Arduino site I found an arduino2MAX object, so once I get more comfortable with MAX I should start thinking of a creative sensor to trigger a patch with.

Wednesday, February 13, 2008

Lab 3 - Analog Input

I'm killing time waiting to get some help... the first stage is already giving me trouble because the Arduino program on upload reads "programmer is not responding". Kind of surprised as this one looked fairly straightforward, and the code was already written for me?? Wait, I unplugged and replugged the USB cable, and remembered to hold down reset before uploading, and now the program loads. But, no light on the LED. Tried another LED, no luck. OMG, would you believe that I switched the leads in the power and ground pins on the Arduino? Now it lights, and the POT dims the LED at will.



On my second task, looks like I'll have to write the program myself. I'm still very much at the stage of "following recipes". Yes, I understand it later if it all works and I check as to why, but I'm not thinking ahead toward other possibilities at this point. I've just read that the 2 variable resistors I'll use will need to have their voltage divided prior to analog input, so I've set up the breadboard accordingly and will now have to go out and buy some flex or some other sensors. Back shortly.



Now I've come armed with photocells, thermistors and a stretch sensor (and bought the basic took kit I never had before), and have decided to start with comparing one each of the first two choices. I varied the previous code by doubling certain commands to address a second variable, renamed all my variables and... voila! The lights are pretty much completely on, and there's no visible indication that my hand pressure or the flash on my camera is affecting the LEDs.

I sought Jeff's assistance and he showed me how to run the serial monitor to see the printed values, and edit the code so the printouts read in clear columns and compute a little more slowly, for easier reading. Some variations were noticeable, but he said that in order to really see the difference in the LEDs the resultant values would have to be scaled to a range appropriate for outputting voltage, namely 0-255. That required making sense of the math and writing a good formula:

OUT = 255/(INmax - INmin) * (IN - INmin)

... where OUT is the desired voltage value to send to the LEDs, IN is the initial reading, INmin is the initial minimum value and INmax the initial maximum value. I re-wrote the code as follows and it helped my variable resistors' sensitivity much better:

int photoPin = 0; // Analog input pin that the photocell is attached to
int thermoPin = 1; // Analog input pin that the thermistor is attached to
int photoValue = 0; // value read from the photocell
int thermoValue = 0; // value read from the thermistor
int ledA = 9; // PWM pin that the LEDa is on. n.b. PWM 0 is on digital pin 9
int ledB = 10; // PWM pin that the LEDb is on. n.b. PWM 0 is on digital pin 10
int P,T;

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
pinMode(photoPin, OUTPUT);
pinMode(thermoPin, OUTPUT);
}

void loop() {
photoValue = analogRead(photoPin); // read the photocell value
thermoValue = analogRead(thermoPin); // read the thermistor value

Serial.print("photovalue; "); // print the photocell value back to the debugger pane
Serial.print(photoValue); // print the photocell value back to the debugger pane
Serial.print(" thermovalue; "); // print the thermistor value back to the debugger pane
Serial.println(thermoValue); // print the thermistor value back to the debugger pane

P = 5*(photoValue-100)/6;
T = 8*(thermoValue-90)/1;

analogWrite(ledA, P); // PWM the LED with the photocell value (divided by 4 to fit in a byte)
analogWrite(ledB, T); // PWM the LED with the thermistor value (divided by 4 to fit in a byte)

delay(100); // wait 100 milliseconds before the next loop
delay(100); // wait 100 milliseconds before the next loop
}

Scaling to a range of values is something I'll need for MAX/MSP and other applications so it was good to get clear with that in my head and in the code. To make a truly user-friendly luv-o-meter, a step I'm not yet prepared to take, I would want many LEDs arranged in an aesthetically pleasing design and also a video display that outputs the scaled values as a dial reading with a meter hand, and each chunk out of the total 255 represents a different level of Don Juan-ness with some silly appellation and graphic for it. I wonder if this is what Jitter is for - in that case it would probably not require an extra microcontroller and out connection. But if it's for market sale and needs to be portable, then maybe it would require a second microcontroller, or some other step that would take the place of needing a whole PC to do the work.



I think that of the two, the thermistor makes the most sense for a luv-o-meter. For a test of strength, some giant FSR or stretch sensor may be appropriate.

Friday, February 1, 2008

PhysComp Lab 2 - The Arduino

Today this handheld device, in one fell swoop, was to help me create a blinker, a switchlight and a combo lock-driven light, and it supplied USB power to my breadboard, rendering the DC power unnecessary for this lab.

Still, I kept the LED on the far end of my board to indicate that the board was indeed powered when I plugged in the Arduino. I also put an LED in the digital output and ground of my Arduino to make sure power worked in that as well.



Once power was determined on all fronts, I created an LED circuit much like in the first lab, but also wrote a simple program to create a blinker with .5 sec delay.





void setup() {
pinMode(13, OUTPUT);
}

void loop() {
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}


I was now ready to try a toggle switch light with the Arduino. This was done in the 1st lab without code, but it's more satisfying to make something work with it.



void setup() {
pinMode(13, OUTPUT);
pinMode(2, INPUT);
}

void loop() {
// check the digial input. If it's high, do something:
if (digitalRead(2) == HIGH) {
// set the digital output HIGH (turn on the LED):
digitalWrite(13, HIGH);
}
else {
// turn off the LED:
digitalWrite(13, LOW);
}
}


I furthered the code in a 3rd application by adding one LED and allowing for the switch between one light being on while the other was off, and vice versa:


void setup() {
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(2, INPUT);
}

void loop() {
// check the digial input. If it's high, do something:
if (digitalRead(2) == HIGH) {
// set the digital output HIGH (turn on the LED):
digitalWrite(13, HIGH);
}
else {
// turn off the LED:
digitalWrite(13, LOW);
}
// check the digial input. If it's high, do something:
if (digitalRead(2) == HIGH) {
// set the digital output HIGH (turn on the LED):
digitalWrite(12, LOW);
}
else {
// turn off the LED:
digitalWrite(12, HIGH);
}
}




Finally, my combo application was a simple recursion in the code - an "if" statement inside an "if" statement - that allowed the 1st switch pressed followed by the 2nd to cause the LED to light up.

void setup() {
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(2, INPUT);
pinMode(3, INPUT);
}

void loop() {
// check the digial input. If it's high, do something:
if (digitalRead(2) == HIGH) {
if (digitalRead(3) == HIGH)
{
// set the digital output HIGH (turn on the LED):
digitalWrite(13, HIGH);}
}
else {
// turn off the LED:
digitalWrite(13, LOW);
}
}




The venture was a success, and I was pleased to realize this could imply much more than powering lights. Motors and sound cues could be the stuff that dreams are made of from here on out.

PhysComp Lab 1 - Basic Circuitry

After barely skating past the basics of "up and running" (Add/Drop, parts purchase, preliminary readings, close observation in class, signing for listservs, trainings and duties, and pulling my hair out), I delved right in and attempted to solder wires to my toggle switch, pot and DC power. Wait, not so fast. I found and met the people I need to meet to acquaint me with the shop, and help me to get started. Quite a friendly bunch, really. Why they have time for me is anybody's guess.

So, I discovered more than one soldering technique, and also that there are some irons that work better than others. I did touch the tip to the solder, I must say, it got a good gob going and then I spread it around the joints. By the 4th or 5th try I was an expert, at least until tomorrow when I forget everything (muscle memory in me is malaise).

That part took all my limbs and then some helping hands, so no good photos here. But then I prepared the breadboard with power and ground from the DC power and again from the voltage regulator. I tested the presence of power in the board with an LED on the other side, with ground and 220 ohm resistor and.... voila! It lit up.



I tested the regulator for 5 volts and discovered that the multimeters in the shop also possess varying levels of reliability. But I got one finally to read what I wanted.

So, for my 1st LED circuit. It was like following a recipe once and then realizing there are subtle variations you can adjust according to taste, or in this case, location on the board. As long as positive flowed to negative and the resistance was in place, I was good to go.



Once this worked out, trying 2 LEDs in series was not so bad. I even checked the voltage and got around half the desired total on each LED. Life was good.



Life got a little trickier when I moved to 3 LEDs in parallel. Sure, they worked, and the lights went dimmer as guessed, but somehow there was no reading for current on the multimeter. One resident suggested the meter itself wasn't working, but it happened on a couple of them.



The most fun part of the lab was getting the pot involved. Here one could conjure immediate applicable ramifications for such a device, most obviously, light dimmers. Not sure if that is really in the domain of resistors or switches, but there you go nonetheless. In one day I became a convert to electronics hobbyism. One year down the road and I may even become handy around the house, much to my wife's pleasure.