{"id":4551,"date":"2010-11-26T00:35:19","date_gmt":"2010-11-25T23:35:19","guid":{"rendered":"http:\/\/www.pesadillo.com\/pesadillo\/?p=4551"},"modified":"2010-11-14T22:36:11","modified_gmt":"2010-11-14T21:36:11","slug":"arduino-control-de-servo-recordar-posiciones","status":"publish","type":"post","link":"https:\/\/www.pesadillo.com\/pesadillo\/?p=4551","title":{"rendered":"Arduino: Control de servo. Recordar posiciones."},"content":{"rendered":"<p>Arduino.<br \/>\nUtilizaci\u00f3n de un servo como entrada y salida de datos con Arduino. Este codigo permite grabar una serie de posiciones de un servo mediante un pulsador y luego este, repetir\u00e1 los movimientos almacenados.<br \/>\n<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" width=\"480\" height=\"385\" codebase=\"http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=6,0,40,0\"><param name=\"allowFullScreen\" value=\"true\" \/><param name=\"allowscriptaccess\" value=\"always\" \/><param name=\"src\" value=\"http:\/\/www.youtube.com\/v\/g94gQb68H_Y?fs=1&amp;hl=es_ES\" \/><param name=\"allowfullscreen\" value=\"true\" \/><embed type=\"application\/x-shockwave-flash\" width=\"480\" height=\"385\" src=\"http:\/\/www.youtube.com\/v\/g94gQb68H_Y?fs=1&amp;hl=es_ES\" allowfullscreen=\"true\" allowscriptaccess=\"always\"><\/embed><\/object><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n\/******************************************************************************************************\r\n *  Using a servo for input and output\r\n *\r\n * by Matt Mets\r\n * Created 9 Feb. 2008\r\n *\r\n * This demonstrates using the servo motor as an input device to record its own positions, which can\r\n * then be played back to animate whatever you are controlling.\r\n *\r\n * Servo control code thanks to:\r\n *    Servo control from an analog input\r\n *    http:\/\/itp.nyu.edu\/physcomp\/Labs\/Servo\r\n *    by Tom Igoe\r\n *    additions by Carlyn Maw\r\n *    Created 28 Jan. 2006\r\n *    Updated 7 Jun. 2006\r\n *\r\n *  The minimum (minPulse) and maxiumum (maxPuluse) values\r\n *  will be different depending on your specific servo motor.\r\n *  Ideally, it should be between 1 and 2 milliseconds, but in practice,\r\n *  0.5 - 2.5 milliseconds works well for me.\r\n *  Try different values to see what numbers are best for you.\r\n *\r\n *  This program uses the millis() function to keep track of when the servo was\r\n *  last pulsed.  millis() produces an overflow error (i.e. generates a number\r\n *  that's too big to fit in a long variable) after about 5 days. if you're\r\n *  making a program that has to run for more than 5 days, you may need to\r\n *  account for this.\r\n ******************************************************************************************************\/\r\n\r\n\/***** Variable Definitions ***************************************************************************\/\r\nint servoPin =        4;      \/\/ Servo motor control line\r\nint analogPin =       1;      \/\/ Line from servo potentimeter\r\nint recordButtonPin = 7;      \/\/ Record button\r\nint playButtonPin =   10;     \/\/ Playback button\r\nint statusPin =       13;     \/\/ Status LED\r\n\r\n\/\/ Adjust these to fit your servo\r\nint minPulse = 500;    \/\/ Minimum servo position (us)\r\nint maxPulse = 2200;  \/\/ Maximum servo position (us)\r\nint refreshTime = 20; \/\/ the time needed in between pulses (ms)\r\n\r\n\/\/ Measure the minimum and maximum output voltages of the potentiometer and record them here.\r\n\/\/ Note: These should be measured over the full range of the servos motion.\r\n#define CALIBRATION_LOW_VOLTAGE   0.40  \/\/ Minimum potentiometer voltage (V)\r\n#define CALIBRATION_HIGH_VOLTAGE  2.00  \/\/ Maximum potentiometer voltage (V)\r\n\r\n\/\/ These are derived from the measured voltages above, are are computed during the setup routine\r\nint inputOffset = 0;    \/\/ Input offset (ADC counts)\r\nfloat inputScale = 0;   \/\/ Input scale (%)\r\n\r\n#define MAX_POSITIONS 50           \/\/ Maximum number of positions that can be stored\r\nint positionTable&#x5B;MAX_POSITIONS];  \/\/ Table to hold each position\r\nint positionCount = 0;             \/\/ Number of positions recorded\r\n\r\n\/***** Functions ************************************************************************************\/\r\nvoid setup()\r\n{\r\n  pinMode(analogPin, INPUT);\r\n  pinMode(servoPin, INPUT);\r\n  pinMode(recordButtonPin, INPUT);\r\n  pinMode(playButtonPin, INPUT);\r\n  pinMode(statusPin, OUTPUT);\r\n\r\n  \/\/ Calculate the input offset and scale factors, based on hand-measured data\r\n  \/\/ 204 = counts per volt\r\n  inputOffset = CALIBRATION_LOW_VOLTAGE * 204;\r\n  inputScale = 1024\/((CALIBRATION_HIGH_VOLTAGE - CALIBRATION_LOW_VOLTAGE) * 204);\r\n\r\n  \/\/ These are because I don't have a proper voltage rail on my proof of concept board, so I am using\r\n  \/\/ pin 3 as a 5V source.  You probably don't need these.\r\n  pinMode(3, OUTPUT);         \/\/ This pin powers some other pins so i dont have to wire...\r\n  digitalWrite(3, HIGH);\r\n\r\n  Serial.begin(9600);\r\n}\r\n\r\n\/\/ Sequence each recorded position\r\nvoid doPlayBack()\r\n{\r\n  int position;\r\n  long startTime;\r\n  long lastPulse = 0;    \/\/ the time in milliseconds of the last pulse\r\n\r\n  \/\/ play back each step for a second\r\n  for(position = 0; position &lt; positionCount; position++)\r\n  {\r\n    startTime = millis();\r\n    while(millis() - startTime &lt; 1000)\r\n    {\r\n      \/\/ pulse the servo again if rhe refresh time (20 ms) have passed:\r\n      if (millis() - lastPulse &gt;= refreshTime)\r\n      {\r\n        digitalWrite(servoPin, HIGH);   \/\/ Turn the motor on\r\n        delayMicroseconds(positionTable&#x5B;position]);       \/\/ Length of the pulse sets the motor position\r\n        digitalWrite(servoPin, LOW);    \/\/ Turn the motor off\r\n        lastPulse = millis();           \/\/ save the time of the last pulse\r\n      }\r\n    }\r\n  }\r\n}\r\n\r\nvoid loop()\r\n{\r\n  int i;\r\n  long analogValue;    \/\/ the value returned from the analog sensor\r\n\r\n  \/\/ When the record button is pressed, try to record the current sensor position\r\n  if(digitalRead(recordButtonPin) == LOW)\r\n  {\r\n    if(positionCount &lt; MAX_POSITIONS)\r\n    {\r\n      \/\/ There is room to store the positions, so do so.\r\n      analogValue = analogRead(analogPin);                    \/\/ Read the analog input\r\n      Serial.println(analogValue);\r\n      analogValue = (analogValue - inputOffset) * inputScale; \/\/ Scale the compressed input to a full range\r\n      analogValue = analogValue*(maxPulse - minPulse)\/1024 + minPulse;  \/\/ Precompute the pulse length\r\n\r\n      positionTable&#x5B;positionCount++] = analogValue;           \/\/ Store in the table\r\n      Serial.println(analogValue);\r\n\r\n      \/\/ Debounce the input button\r\n      digitalWrite(statusPin, HIGH);\r\n      while(digitalRead(recordButtonPin) == LOW)\r\n        delay(200);\r\n      digitalWrite(statusPin, LOW);\r\n    } else {\r\n      \/\/ There is no more room to record positions, so flash the LED in protest.\r\n      for(i = 0; i &lt; 5; i++)\r\n      {\r\n        digitalWrite(statusPin, HIGH);\r\n        delay(100);\r\n        digitalWrite(statusPin, LOW);\r\n        delay(100);\r\n      }\r\n    }\r\n  }\r\n\r\n  \/\/ User wants to play back the sequence\r\n  if(digitalRead(playButtonPin) == LOW) {\r\n    digitalWrite(statusPin, HIGH);\r\n    doPlayBack();\r\n    digitalWrite(statusPin, LOW);\r\n  }\r\n}\r\n<\/pre>\n<p>Fuente: <a href=\"http:\/\/www.cibomahto.com\/2008\/02\/thing-a-day-day-9-servo-as-input-device\/\" target=\"_blank\">http:\/\/www.cibomahto.com\/2008\/02\/thing-a-day-day-9-servo-as-input-device\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Arduino. Utilizaci\u00f3n de un servo como entrada y salida de datos con Arduino. Este codigo permite grabar una serie de posiciones de un servo mediante un pulsador y luego este, repetir\u00e1 los movimientos almacenados. \/****************************************************************************************************** * Using a servo for input and output * * by Matt Mets * Created 9 Feb. 2008 * * This demonstrates using the servo [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[48,69,23],"class_list":["post-4551","post","type-post","status-publish","format-standard","hentry","category-electronica","tag-arduino","tag-electronica","tag-videos"],"_links":{"self":[{"href":"https:\/\/www.pesadillo.com\/pesadillo\/index.php?rest_route=\/wp\/v2\/posts\/4551","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pesadillo.com\/pesadillo\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pesadillo.com\/pesadillo\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pesadillo.com\/pesadillo\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pesadillo.com\/pesadillo\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4551"}],"version-history":[{"count":3,"href":"https:\/\/www.pesadillo.com\/pesadillo\/index.php?rest_route=\/wp\/v2\/posts\/4551\/revisions"}],"predecessor-version":[{"id":4562,"href":"https:\/\/www.pesadillo.com\/pesadillo\/index.php?rest_route=\/wp\/v2\/posts\/4551\/revisions\/4562"}],"wp:attachment":[{"href":"https:\/\/www.pesadillo.com\/pesadillo\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pesadillo.com\/pesadillo\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pesadillo.com\/pesadillo\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}