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.

No comments: