Analog Output Labs

The servo code was really interesting : the fake analog out strategy of pulse width modulation. I’m trying to understand it:

As I understand, a full pulse cycle of two pulses, consists of a high pulse of 5V and a low of 0V, no V, of varying lengths (in order of a few milliseconds – 1000 pulses/second frequency=1000 Hz) . The ratio of the length of the high signal to the full cycle (high and low together) is the duty cycle. If the high signal is the same length as the low signal, or 50%of the full cycle, then the duty cycle is 50%. The output effective/ average Voltage being sent through the output is the duty cycle. When the pulses are equal, a 50%voltage (2.5V) is released, so in the case of the servo this determines the location of 180 degrees, half of its full rotation.

As the low signal approaches zero, and the high pulses increase in length/consistency, the duty cycle approaches 100%, sending out 5V to the servo, thus turning the motor toward 360 degrees. As the high pulses decrease in length/consistency, and the low pulses increase in length, the duty cycle approaches 0%, which means sending 0V to the servo.

int servoPin = 2;     // output pin for servo motor
 int minPulse = 500;
// Minimum servo position, 
I don't know where these numbers come from.
 int maxPulse = 2500;  // Maximum servo position
 int pulse = 0;        // Amount to pulse the servo 
in the beginning

 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 flex/force sensor
 int analogPin = 0;    // the analog pin that the 
sensor's on, pin 0

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

 void loop() {
 //read the force sensor/flex sensor value:
  analogValue = analogRead(analogPin);
  pulse = map(analogValue,0,1023,minPulse,maxPulse);
  // convert the analog value to a range 
between minPulse and maxPulse
 And send this out to the output pin, 
to the motor..

  Pulse the servo again if the refresh time (20 ms) have passed:
  if (millis() - lastPulse >= refreshTime) {
    digitalWrite(servoPin, HIGH);   // Turn the motor on
    //length of pulse is the mapping of the analog value, 
as pulse was defined before, which would determine 
the duty cycle:
    delayMicroseconds(pulse);
    digitalWrite(servoPin, LOW);    // Turn the motor off
    lastPulse = millis();           // save the time 
of the last pulse
  }
 }

Here is the flex sensing resistor version, followed by the force sensing resistor. The flex sensor has a much smaller range, which for the application of the wagging finger makes more sense.

I could have also changed the minPulse and maxPulse values to achieve this range with the force sensor.

[wpvideo 5C4XJWEC]

[wpvideo WwcwVEU0]

Tone Control:

The first one with the photo sensors was one voltage divider with both resistors variable. The second video was the four note w/ tone control in the code. The tone control is pretty bad I realized. I think its highly dependent on the sensors, and worked really well with the full range force sensors. Since I only had two of those, I tried to use the flex sensors for the other two. They have a very small range, and the code used a sensor array, which generalized for all sensors to be full range, so those sensors gave off a continuous tone. I was playing around with changing the resistance and current to them, but finally I just made this version with the wires sticking out that react to the touch.

[wpvideo AqyfRmTv]

These notes from the code are really off, so this is really really bad:

[wpvideo plT3kaid]