{"id":99,"date":"2008-10-09T02:20:33","date_gmt":"2008-10-09T06:20:33","guid":{"rendered":"http:\/\/mikelberman.com\/blog\/?p=99"},"modified":"2008-10-09T02:20:33","modified_gmt":"2008-10-09T06:20:33","slug":"rattler-bot","status":"publish","type":"post","link":"http:\/\/mikelberman.com\/blog\/?p=99","title":{"rendered":"Rattler-Bot"},"content":{"rendered":"<p>My intention was to use a servo motor to rattle a snake tail necklace that I have.\u00a0 I envisioned a shoe-box marked &#8220;DANGER&#8221; that hid an ultra-sonic sensor, which upon detecting the close proximity of a curious ITP student, would rattle the tail.<\/p>\n<p><a href=\"http:\/\/mikelberman.com\/blog\/wp-content\/uploads\/rattleus.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-117\" title=\"rattleus\" src=\"http:\/\/mikelberman.com\/blog\/wp-content\/uploads\/rattleus-300x182.jpg\" alt=\"\" width=\"300\" height=\"182\" \/><\/a><\/p>\n<p>I couldn&#8217;t find my snake necklace, so I fashioned a rattle from some plastic tubing, lamp chain &amp; wood scraps (oops, Sara I still have your green screwdriver).\u00a0 This fabrication took abnormally long since the screw for the servo attachments does not like to thread tightly, and after a very long series of trials and errors, I just hot-glued the dang thing.<\/p>\n<div id=\"attachment_116\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/mikelberman.com\/blog\/wp-content\/uploads\/rattle.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-116\" class=\"size-medium wp-image-116\" title=\"rattle\" src=\"http:\/\/mikelberman.com\/blog\/wp-content\/uploads\/rattle-300x210.jpg\" alt=\"shake it like a polaroid\" width=\"300\" height=\"210\" \/><\/a><p id=\"caption-attachment-116\" class=\"wp-caption-text\">shake it like a polaroid<\/p><\/div>\n<p>Here is the code for the prototype with a potentiometer instead of the ultrasound.<\/p>\n<pre style=\"padding-left: 30px;\">int servoPin = 5;\u00a0\u00a0\u00a0\u00a0 \/\/ Control pin for servo motor<\/pre>\n<pre style=\"padding-left: 30px;\">int minPulse = 500;\u00a0\u00a0 \/\/ Minimum servo position<\/pre>\n<pre style=\"padding-left: 30px;\">int maxPulse = 2500;\u00a0 \/\/ Maximum servo position<\/pre>\n<pre style=\"padding-left: 30px;\">int pulse = 0;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Amount to pulse the servo<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0long minValue;<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0long maxValue;<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0int midValue=1400;<\/pre>\n<pre style=\"padding-left: 30px;\">\/\/ flag to determine if we should be constantly moving<\/pre>\n<pre style=\"padding-left: 30px;\">long lastSensorReading = 0;<\/pre>\n<pre style=\"padding-left: 30px;\">long lastPulse = 0;\u00a0\u00a0\u00a0 \/\/ the time in milliseconds of the last pulse<\/pre>\n<pre style=\"padding-left: 30px;\">int refreshTime = 20; \/\/ the time needed in between pulses<\/pre>\n<pre style=\"padding-left: 30px;\">int analogValue = 0;\u00a0 \/\/ the value returned from the analog sensor<\/pre>\n<pre style=\"padding-left: 30px;\">int analogPin = 0;\u00a0\u00a0\u00a0 \/\/ the analog pin that the sensor's on<\/pre>\n<pre style=\"padding-left: 30px;\">void setup() {<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0pinMode(servoPin, OUTPUT);\u00a0 \/\/ Set servo pin as an output pin<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0pulse = midValue;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Set the motor position value to the minimum<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0Serial.begin(9600);<\/pre>\n<pre style=\"padding-left: 30px;\">}<\/pre>\n<pre style=\"padding-left: 30px;\">void loop() {<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0analogValue = analogRead(analogPin);\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ read the analog input<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0minValue = 1300 - ((700*analogValue)\/1023);<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0maxValue = 1500 + ((700*analogValue)\/1023);<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0Serial.print(\"minVal: \");<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0Serial.print(minValue, DEC);<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0Serial.print(\" maxVal: \");<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0Serial.println(maxValue, DEC);<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0\/\/ pulse the servo again if the refresh time of 20 ms has passed:<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0if (millis() - lastPulse &gt;= refreshTime) {<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0\u00a0\u00a0 \/\/ check the force sensor every second<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0\u00a0\u00a0 int accel = map(analogValue, 0, 1023, 100, 500);<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0\u00a0\u00a0 if (millis() - lastSensorReading &gt;= accel) { \/\/SPEED<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Serial.println(analogValue);<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ if we are still reading a high value from the pressure sensor,<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ determine the next position of the servo motor<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ if (analogValue &gt; 400) {<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ if (pulse &gt;= 2200)<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/\u00a0\u00a0\u00a0 { pulse = 600; } \/\/ TWO POSITIONS<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ else { pulse = 2200; }<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (pulse &gt;= maxValue) { pulse = minValue; } \/\/ TWO POSITIONS<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 else { pulse = maxValue; }<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ }<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0\u00a0\u00a0\u00a0\u00a0 lastSensorReading = millis();<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0\u00a0\u00a0 \/\/Serial.println(pulse);<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0\u00a0\u00a0 \/\/ tell the servo motor to move to it's new position<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0\u00a0\u00a0 digitalWrite(servoPin, HIGH);\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Turn the motor on<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0\u00a0\u00a0 delayMicroseconds(pulse);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ servo moves to new locale<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0\u00a0\u00a0 digitalWrite(servoPin, LOW);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Turn the motor off<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0\u00a0\u00a0 lastPulse = millis();\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ save the time of the last pulse<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0\u00a0\u00a0 }<\/pre>\n<pre style=\"padding-left: 30px;\">\u00a0 }<\/pre>\n<p style=\"padding-left: 30px;\">\n","protected":false},"excerpt":{"rendered":"<p>My intention was to use a servo motor to rattle a snake tail necklace that I have.\u00a0 I envisioned a shoe-box marked &#8220;DANGER&#8221; that hid an ultra-sonic sensor, which upon detecting the close proximity of a curious ITP student, would rattle the tail. I couldn&#8217;t find my snake necklace, so I fashioned a rattle from [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,1],"tags":[],"class_list":["post-99","post","type-post","status-publish","format-standard","hentry","category-physical-computing","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/mikelberman.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/99","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/mikelberman.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mikelberman.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mikelberman.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/mikelberman.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=99"}],"version-history":[{"count":4,"href":"http:\/\/mikelberman.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/99\/revisions"}],"predecessor-version":[{"id":119,"href":"http:\/\/mikelberman.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/99\/revisions\/119"}],"wp:attachment":[{"href":"http:\/\/mikelberman.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mikelberman.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mikelberman.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}