{"id":483,"date":"2017-01-26T20:20:02","date_gmt":"2017-01-26T20:20:02","guid":{"rendered":"https:\/\/zhutarduino.wordpress.com\/?page_id=483"},"modified":"2020-10-12T16:38:48","modified_gmt":"2020-10-12T16:38:48","slug":"how-to-use-a-usb-mouse-to-contol-servos-with-a-arduino-board","status":"publish","type":"page","link":"https:\/\/thezhut.com\/?page_id=483","title":{"rendered":"How to use a USB mouse to contol servos with a ARDUINO board"},"content":{"rendered":"<h1>This tutorial will show you how to controll servos with a usb mouse and host shield.<\/h1>\n<h1>Click on the video below for a full tutorial..<\/h1>\n<p><iframe loading=\"lazy\" title=\"How to use a USB mouse to contol servos with a ARDUINO board\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/zQ1yV3XlRLI?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h1><span style=\"color: #ff0000;\"><strong><a style=\"color: #ff0000;\" href=\"https:\/\/www.facebook.com\/zarduino\/\">Check us out on Facebook<\/a><\/strong><\/span><\/h1>\n<h1>PARTS LIST:<\/h1>\n<p>#affiliate links#<\/p>\n<p><a href=\"https:\/\/www.amazon.com\/gp\/search\/ref=as_li_qf_sp_sr_il_tl?ie=UTF8&amp;tag=zhut01-20&amp;keywords=usb mouse&amp;index=aps&amp;camp=1789&amp;creative=9325&amp;linkCode=xm2&amp;linkId=d18d2355d630442d76ef8518c116587c\" target=\"_blank\" rel=\"noopener noreferrer\">USB MOUSE<\/a><\/p>\n<p><a href=\"https:\/\/www.amazon.com\/gp\/search\/ref=as_li_qf_sp_sr_il_tl?ie=UTF8&amp;tag=zhut01-20&amp;keywords=usb host shield&amp;index=aps&amp;camp=1789&amp;creative=9325&amp;linkCode=xm2&amp;linkId=f9ffdbcde71a487a9e06117d75c736e1\" target=\"_blank\" rel=\"noopener noreferrer\">USB HOST SHIELD<\/a><\/p>\n<p><a href=\"https:\/\/www.amazon.com\/gp\/search\/ref=as_li_qf_sp_sr_il_tl?ie=UTF8&amp;tag=zhut01-20&amp;keywords=arduino board&amp;index=aps&amp;camp=1789&amp;creative=9325&amp;linkCode=xm2&amp;linkId=1c83640c8ebd68500ed6754d98b38ac2\" target=\"_blank\" rel=\"noopener noreferrer\">ARDUINO BOARD<\/a><\/p>\n<p><a href=\"https:\/\/www.amazon.com\/gp\/search\/ref=as_li_qf_sp_sr_il_tl?ie=UTF8&amp;tag=zhut01-20&amp;keywords=servo&amp;index=aps&amp;camp=1789&amp;creative=9325&amp;linkCode=xm2&amp;linkId=d794011a0c9060e78f3e8ec868ede439\" target=\"_blank\" rel=\"noopener noreferrer\">SERVOS<\/a><\/p>\n<h1>THE CODE:<\/h1>\n<p>#include &lt;hidboot.h&gt;<br \/>\n#include &lt;usbhub.h&gt;<br \/>\n#include &lt;Servo.h&gt;<\/p>\n<p>Servo s1; \/\/ define servo names<br \/>\nServo s2;<br \/>\n\/\/ Satisfy the IDE, which needs to see the include statment in the ino too.<br \/>\n#ifdef dobogusinclude<br \/>\n#include &lt;spi4teensy3.h&gt;<br \/>\n#include &lt;SPI.h&gt;<br \/>\n#endif<br \/>\nint x; \/\/ mouse x<br \/>\nint y; \/\/ mouse y<br \/>\nint y1;<br \/>\nint x1;<br \/>\nclass MouseRptParser : public MouseReportParser<br \/>\n{<br \/>\nprotected:<br \/>\nvoid OnMouseMove (MOUSEINFO *mi);<br \/>\nvoid OnLeftButtonUp (MOUSEINFO *mi);<br \/>\nvoid OnLeftButtonDown (MOUSEINFO *mi);<br \/>\nvoid OnRightButtonUp (MOUSEINFO *mi);<br \/>\nvoid OnRightButtonDown (MOUSEINFO *mi);<br \/>\nvoid OnMiddleButtonUp (MOUSEINFO *mi);<br \/>\nvoid OnMiddleButtonDown (MOUSEINFO *mi);<br \/>\n};<br \/>\nvoid MouseRptParser::OnMouseMove(MOUSEINFO *mi)<br \/>\n{<br \/>\nSerial.print(&#8220;x= &#8220;);<br \/>\nx = (x + mi-&gt;dX);<br \/>\nSerial.println(x);<br \/>\nx = constrain(x, -2000, 2000);<br \/>\nx1 = x;<\/p>\n<p>x1 = map(x1, -2000, 2000, 5, 175);<br \/>\ns1.write(x1);<br \/>\nSerial.print(&#8220;y= &#8220;);<br \/>\ny = (y + mi-&gt;dY);<br \/>\nSerial.println(y);<br \/>\ny = constrain(y, -2000, 2000);<br \/>\ny1 = y;<\/p>\n<p>y1 = map(y1, -2000, 2000, 5, 175);<br \/>\ns2.write(y1);<br \/>\n};<br \/>\nvoid MouseRptParser::OnLeftButtonUp (MOUSEINFO *mi)<br \/>\n{<br \/>\nSerial.println(&#8220;L Butt Up&#8221;);<br \/>\n};<br \/>\nvoid MouseRptParser::OnLeftButtonDown (MOUSEINFO *mi)<br \/>\n{<br \/>\nSerial.println(&#8220;L Butt Dn&#8221;);<br \/>\nx = 5;<br \/>\ny = 5;<br \/>\ns1.write(5);<br \/>\ns2.write(5);<br \/>\n};<br \/>\nvoid MouseRptParser::OnRightButtonUp (MOUSEINFO *mi)<br \/>\n{<br \/>\nSerial.println(&#8220;R Butt Up&#8221;);<br \/>\n};<br \/>\nvoid MouseRptParser::OnRightButtonDown (MOUSEINFO *mi)<br \/>\n{<br \/>\nSerial.println(&#8220;R Butt Dn&#8221;);<br \/>\nx = 175;<br \/>\ny = 175;<br \/>\ns1.write(175);<br \/>\ns2.write(175);<br \/>\n};<br \/>\nvoid MouseRptParser::OnMiddleButtonUp (MOUSEINFO *mi)<br \/>\n{<br \/>\nSerial.println(&#8220;M Butt Up&#8221;);<br \/>\n};<br \/>\nvoid MouseRptParser::OnMiddleButtonDown (MOUSEINFO *mi)<br \/>\n{<br \/>\nSerial.println(&#8220;M Butt Dn&#8221;);<br \/>\nx = 90;<br \/>\ny = 90;<br \/>\ns1.write(90);<br \/>\ns2.write(90);<br \/>\n};<\/p>\n<p>USB Usb;<br \/>\nUSBHub Hub(&amp;Usb);<br \/>\nHIDBoot&lt;USB_HID_PROTOCOL_MOUSE&gt; HidMouse(&amp;Usb);<\/p>\n<p>uint32_t next_time;<\/p>\n<p>MouseRptParser Prs;<\/p>\n<p>void setup()<br \/>\n{<br \/>\nSerial.begin(9600);<br \/>\ns1.attach(44); \/\/ attaches the servo on pin 44 to the servo<br \/>\ns2.attach(46); \/\/ attaches the servo on pin 46 to the servo<br \/>\ns1.write(90);<br \/>\ns2.write(90);<br \/>\n#if !defined(__MIPSEL__)<br \/>\nwhile (!Serial); \/\/ Wait for serial port to connect &#8211; used on Leonardo, Teensy and other boards with built-in USB CDC serial connection<br \/>\n#endif<br \/>\nSerial.println(&#8220;Start&#8221;);<\/p>\n<p>if (Usb.Init() == -1)<br \/>\nSerial.println(&#8220;OSC did not start.&#8221;);<\/p>\n<p>delay( 200 );<\/p>\n<p>next_time = millis() + 5000;<\/p>\n<p>HidMouse.SetReportParser(0, &amp;Prs);<br \/>\n}<\/p>\n<p>void loop()<br \/>\n{<br \/>\nUsb.Task();<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial will show you how to controll servos with a usb mouse and host shield. Click on the video below for a full tutorial.. Check us out on Facebook PARTS LIST: #affiliate links# USB MOUSE USB HOST SHIELD ARDUINO &hellip; <a href=\"https:\/\/thezhut.com\/?page_id=483\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":10,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":"","_links_to":"","_links_to_target":""},"class_list":["post-483","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/pages\/483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/thezhut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=483"}],"version-history":[{"count":3,"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/pages\/483\/revisions"}],"predecessor-version":[{"id":1610,"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/pages\/483\/revisions\/1610"}],"up":[{"embeddable":true,"href":"https:\/\/thezhut.com\/index.php?rest_route=\/wp\/v2\/pages\/10"}],"wp:attachment":[{"href":"https:\/\/thezhut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}