{"id":188,"date":"2010-10-11T18:53:55","date_gmt":"2010-10-11T18:53:55","guid":{"rendered":"http:\/\/www.mariarabinovich.com\/physcomp\/?p=188"},"modified":"2011-02-05T17:54:06","modified_gmt":"2011-02-05T17:54:06","slug":"analog-output-labs","status":"publish","type":"post","link":"http:\/\/www.mariarabinovich.com\/blog\/archives\/188","title":{"rendered":"Analog Output Labs"},"content":{"rendered":"

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

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)\u00a0. 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.<\/p>\n

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.<\/p>\n

int servoPin = 2;     \/\/ output pin for servo motor\r\n int minPulse = 500;<\/pre>\n
\/\/ Minimum servo position,\u00a0<\/span><\/pre>\n
I don't know where these numbers come from.<\/span><\/pre>\n
 int maxPulse = 2500;  \/\/ Maximum servo position<\/span>\r\n int pulse = 0;        \/\/ Amount to pulse the servo\u00a0<\/span><\/pre>\n
in the beginning\r\n<\/span>\r\n long lastPulse = 0;    \/\/ the time in milliseconds\u00a0<\/span><\/pre>\n
of the last pulse<\/span>\r\n int refreshTime = 20; \/\/ the time needed in between\u00a0<\/span><\/pre>\n
pulses<\/span>\r\n\r\n int analogValue = 0;  \/\/ the value returned from\u00a0<\/span><\/pre>\n
the flex\/force sensor<\/span><\/pre>\n
 int analogPin = 0;    \/\/ the analog pin that the\u00a0<\/span><\/pre>\n
sensor's on, pin 0<\/span>\r\n\r\n void setup() {\r\n  pinMode(servoPin, OUTPUT);  \/\/ Set servo\u00a0<\/span><\/pre>\n
pin as an output pin<\/span><\/pre>\n
  pulse = minPulse;          \u00a0<\/span><\/pre>\n
\/\/ Set the motor position value\u00a0<\/span><\/pre>\n
to the minimum\u00a0<\/span>(<\/span>500)<\/span><\/pre>\n
  Serial.begin(9600);\r\n }\r\n\r\n void loop() {\r\n\u00a0\/\/read the force sensor\/flex sensor value:<\/span><\/pre>\n
  <\/span>analogValue = analogRead(analogPin);<\/pre>\n
  pulse = map(analogValue,0,1023,minPulse,maxPulse);<\/pre>\n
  \/\/ convert the analog value to a range\u00a0<\/span><\/pre>\n
between minPulse and maxPulse<\/span><\/pre>\n
\u00a0A<\/span>nd send this out to the output pin,\u00a0<\/span><\/pre>\n
to the motor..<\/span>\r\n\r\n  P<\/span>u<\/span>lse the servo again if the refresh time (20 ms) have passed:<\/span><\/pre>\n
  if (millis() - lastPulse >= refreshTime) {\r\n    digitalWrite(servoPin, HIGH);   \/\/ Turn the motor on<\/span>\r\n   \u00a0\/\/length of pulse is the mapping of the analog value, <\/span><\/pre>\n
as pulse was defined before, which would determine\u00a0<\/span><\/pre>\n
the duty cycle:<\/span><\/pre>\n
    delayMicroseconds(pulse);<\/pre>\n
    digitalWrite(servoPin, LOW);    \/\/ Turn the motor off<\/span>\r\n    lastPulse = millis();           \/\/ save the time\u00a0<\/span><\/pre>\n
of the last pulse<\/span>\r\n  }\r\n }<\/pre>\n

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.<\/p>\n

I could have also changed the minPulse and maxPulse values to achieve this range with the force sensor.<\/p>\n

[wpvideo 5C4XJWEC]<\/p>\n

[wpvideo WwcwVEU0]<\/p>\n

Tone Control:<\/p>\n

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.<\/p>\n

[wpvideo AqyfRmTv]<\/p>\n

These notes from the code are really off, so this is really really bad:<\/p>\n

[wpvideo plT3kaid]<\/p>\n","protected":false},"excerpt":{"rendered":"

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 – […]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,24,1],"tags":[],"class_list":["post-188","post","type-post","status-publish","format-standard","hentry","category-labs","category-physical-computing","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.mariarabinovich.com\/blog\/wp-json\/wp\/v2\/posts\/188","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.mariarabinovich.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.mariarabinovich.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.mariarabinovich.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"http:\/\/www.mariarabinovich.com\/blog\/wp-json\/wp\/v2\/comments?post=188"}],"version-history":[{"count":11,"href":"http:\/\/www.mariarabinovich.com\/blog\/wp-json\/wp\/v2\/posts\/188\/revisions"}],"predecessor-version":[{"id":431,"href":"http:\/\/www.mariarabinovich.com\/blog\/wp-json\/wp\/v2\/posts\/188\/revisions\/431"}],"wp:attachment":[{"href":"http:\/\/www.mariarabinovich.com\/blog\/wp-json\/wp\/v2\/media?parent=188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mariarabinovich.com\/blog\/wp-json\/wp\/v2\/categories?post=188"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mariarabinovich.com\/blog\/wp-json\/wp\/v2\/tags?post=188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}