{"id":40395,"date":"2017-06-29T10:04:20","date_gmt":"2017-06-29T10:04:20","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=40395"},"modified":"2019-04-02T10:23:28","modified_gmt":"2019-04-02T10:23:28","slug":"gesture-slider-swiper-with-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/gesture-slider-swiper-with-arduino\/","title":{"rendered":"Gesture Slider Swiper with Arduino"},"content":{"rendered":"<p>In this post you\u2019re going to learn how to set an Arduino UNO board as a USB HID keyboard and use gestures to swipe slides during a presentation. <!--more--><\/p>\n<div style=\"padding: 10px; line-height: 1.2; text-align: center; background-color: #f5f5f5;\">This project was written by&nbsp;<a href=\"https:\/\/www.linkedin.com\/in\/emmanuel-odunlade-2431b2b0\/\" target=\"_blank\" rel=\"noopener noreferrer\">Emmanuel Odunlade<\/a> and edited by <a href=\"https:\/\/randomnerdtutorials.com\/about\/\">Rui Santos<\/a>.<\/div>\n<h2>How it works<\/h2>\n<p>Here&#8217;s a&nbsp;figure&nbsp;that describes exactly&nbsp;how everything&nbsp;works together:<\/p>\n<p><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-40396\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/how-it-works.png?resize=755%2C573&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"755\" height=\"573\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/how-it-works.png?w=755&amp;quality=100&amp;strip=all&amp;ssl=1 755w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/how-it-works.png?resize=300%2C228&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 755px) 100vw, 755px\" \/><\/p>\n<p>If you like Arduino projects&nbsp;and you want to build more, we recommend getting the&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/arduino-step-by-step-projects\/\" target=\"_blank\" rel=\"noopener noreferrer\">Arduino Step-by-step Projects \u2013 Build 25 Projects course<\/a>.<\/p>\n<h2>Arduino UNO or Mega as a USB HID keyboard<\/h2>\n<p>The Arduino can be configured to emulate a keyboard and perform useful tasks. Attaching a pushbutton to an Arduino digital pin and with a button press, <a href=\"https:\/\/randomnerdtutorials.com\/teensy-username-and-password-auto-filler\/\">you can write a password<\/a>, copy+paste, up\/down volume, up\/down screen brightness, etc\u2026<\/p>\n<p>Although boards like the Arduino UNO can\u2019t do this by default if you flash a new bootloader, you can make the Arduino UNO board turn into a USB HID keyboard.<\/p>\n<p>An Arduino Uno\/Mega has two microcontrollers: ATmega328 and 16u2. The 16u2 is normally used for USB to Serial communication.&nbsp;We can also use it as standalone AVR Microcontroller with (or without) USB functions as well.<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter wp-image-40400\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/16u.jpg?resize=517%2C467&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"517\" height=\"467\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/16u.jpg?w=638&amp;quality=100&amp;strip=all&amp;ssl=1 638w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/16u.jpg?resize=300%2C271&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 517px) 100vw, 517px\" \/><\/p>\n<p>HoodLoader2 gives you the option to reprogram the 16u2 of a normal Arduino Uno\/Mega R3 with custom sketches. This means you can use the 16u2 as a normal USB AVR like an Arduino Leonardo board. You have a full compatible USB-HID core.<\/p>\n<p>You can prepare your Arduino UNO or Arduino Mega to act as a keyboard with the <a href=\"https:\/\/github.com\/NicoHood\/HoodLoader2\">HoodLoader2<\/a> bootloader by following the instructions in their <a href=\"https:\/\/github.com\/NicoHood\/HoodLoader2\/wiki\/Overview\">official Wiki page<\/a>.<\/p>\n<p>Supported Arduino boards using Arduino IDE 1.6.7 or higher:<\/p>\n<ul>\n<li>Uno (needs HoodLoader2 bootloader)<\/li>\n<li>Mega (needs HoodLoader2 bootloader)<\/li>\n<li>Leonardo<\/li>\n<li>(Pro) Micro<\/li>\n<\/ul>\n<h2>Parts required<\/h2>\n<p>Here\u2019s a complete list of the components you need for this project:<\/p>\n<ul>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/compatible-arduino-uno-r3-board\/\" target=\"_blank\" rel=\"noopener noreferrer\">Arduino UNO<\/a>, Mega, Leonardo, ProMicro or any other 8u2\/16u2\/at90usb8\/162\/32u2\/32u4 compatible board<\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/ultrasonic-sensor-hc-sr04\/\" target=\"_blank\" rel=\"noopener noreferrer\">2x Ultrasonic sensor &#8211; HC-SR04&nbsp;<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/3mm-5mm-leds-kit-storage-box\/\" target=\"_blank\" rel=\"noopener noreferrer\">2x LEDs&nbsp;<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\" rel=\"noopener noreferrer\">2x 220 ohm resistors&nbsp;<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noopener noreferrer\">Breadboard&nbsp;<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noopener noreferrer\">Jumper wires&nbsp;<\/a><\/li>\n<\/ul>\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter wp-image-40399\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/demonstration.jpg?resize=484%2C382&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"484\" height=\"382\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/demonstration.jpg?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/demonstration.jpg?resize=300%2C237&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 484px) 100vw, 484px\" \/><\/p>\n<h2>Uploading code<\/h2>\n<p>To use your Arduino as a keyboard, you need to install the HID Library.<\/p>\n<p><strong>Installing the HID Library<\/strong><\/p>\n<ol>\n<li><a href=\"https:\/\/github.com\/NicoHood\/HID\/archive\/master.zip\">Click here to download the HID<\/a> library. You should have a .zip folder in your Downloads folder<\/li>\n<li>Unzip the .zip folder and you should get <strong>HID-master<\/strong> folder<\/li>\n<li>Rename your folder from <del><strong>HID-master<\/strong><\/del> to <strong>HID<\/strong><\/li>\n<li>Move the <strong>HID<\/strong> folder to your Arduino IDE installation libraries folder<\/li>\n<li>Finally, re-open your Arduino IDE<\/li>\n<\/ol>\n<p>Copy the following code to your Arduino IDE and upload it to your Arduino board:<\/p>\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\"> \/*\n  * Author: Emmanuel Odunlade \n  * Complete Project Details http:\/\/randomnerdtutorials.com\n  *\/\n \n#include &lt;HID.h&gt;\/\/ include the library \n\nint swipe = 0;\/\/slide\nboolean left = false;\nboolean right = false;\nint maxD = 5;\/\/(in cm) maximum distance from ultrasonic at which the obstruction will be considered a gesture\nlong int lastTouch = -1;\nint resetAfter = 1000;\/\/ms\nint afterslideDelay = 500;\/\/\nint slideleft_Begin = -1;\nint slideNone = 0;\nint slideright_Begin =1;\n\n\/\/Declaring the connected pins\nint lLed = 7;\nint rLed = 6;\nconst int lEcho = 2;\nconst int lTrig = 3;\nconst int rEcho = 4;\nconst int rTrig = 5;\n\nvoid setup(){\n  pinMode(lLed,OUTPUT);\n  pinMode(rLed,OUTPUT);\n  pinMode(rEcho,INPUT);  \n  pinMode(rTrig,OUTPUT);\n  pinMode(lEcho,INPUT);\n  pinMode(lTrig,OUTPUT);\n  \n  Serial.begin(9600);\n    \/\/ Sends a clean report to the host. This is important because\n  \/\/ the 16u2 of the Uno\/Mega is not turned off while programming\n  \/\/ so you want to start with a clean report to avoid strange bugs after reset.\n  pressRawKeyboard(0, 0);\n}\n\n\/\/get distance\n\/\/echo is input, trigger is output\nunsigned long measureD(int input, int output){\n  digitalWrite(output, HIGH);\n  delayMicroseconds(10); \n  digitalWrite(output, LOW);\n  long range= pulseIn(input, HIGH);\n  int distance= range \/ 29 \/ 2;\/\/ to get distance in cm\n  return distance;\n}\n\n\/\/\nboolean act (int input, int output, int led){\n  int d = measureD(input,output);\n  boolean pinActivated = false;\n  if (d &lt; maxD){\n    digitalWrite(led,HIGH);\n    pinActivated = true;\n  }\n  else{\n    digitalWrite(led,LOW);\n    pinActivated = false;\n  }\n  return pinActivated;\n}\n\nvoid slideNow(char directn){\n  if ('R' == directn)\n  {\n    \/\/ press the right rrow key\n    \/\/Serial.println(&quot;F&quot;);\n    pressRawKeyboard(0,RAW_KEYBOARD_RIGHT_ARROW); \/\/modifiers + key\n    pressRawKeyboard(0, 0); \/\/ release! Important\n  }\n  if ('L' == directn)\n  {\n    \/\/press the left arrow key\n    \/\/Serial.println(&quot;B&quot;); for debug\n    pressRawKeyboard(0,RAW_KEYBOARD_LEFT_ARROW); \/\/ \/\/modifiers + key\n    pressRawKeyboard(0, 0); \/\/ release! Important\n  }\n  delay(afterslideDelay);\n  swipe = slideNone;\n}\n\nvoid pressRawKeyboard(uint8_t modifiers, uint8_t key){\n  uint8_t keys[8] = { \n    modifiers, 0, key, 0, 0, 0, 0, 0    }; \/\/modifiers, reserved, key[0]\n  HID_SendReport(HID_REPORTID_KeyboardReport, keys, sizeof(keys));\n}\n\n\nvoid loop(){\n    left = act(lEcho,lTrig,lLed);\n    right = act(rEcho,rTrig,rLed);\n    if (left || right){\n        lastTouch = millis();\n    }\n    if (millis() - lastTouch &gt; resetAfter){\n      swipe = 0;\n      \/\/serial.println(@reset slide&amp; timer);\n    }\n    if (swipe &gt;= slideNone){\n      if ((left)&amp;&amp;(!right)){\n        swipe = slideright_Begin;\n      }\n      if ((right)&amp;&amp;(swipe == slideright_Begin)){\n        slideNow('R');\n      }\n    }\n    if (swipe &lt;= slideNone ){\n      if ((right)&amp;&amp;(!left)){\n        swipe = slideleft_Begin;\n      }\n      if ((left) &amp;&amp; (swipe == slideleft_Begin)){\n        slideNow('L');\n      }\n    }\n    delay(50);\n }\n \n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/RNT-guest-posts\/raw\/master\/Emmanuel_Odunlade\/Slider_Swiper.ino\" target=\"_blank\">View raw code<\/a><\/p>\n<h2>Schematics<\/h2>\n<p>Wire your circuit accordingly to the schematic below:<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/SlideSwipe_f.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-47639\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/SlideSwipe_f.png?resize=701%2C581&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"701\" height=\"581\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/SlideSwipe_f.png?w=1755&amp;quality=100&amp;strip=all&amp;ssl=1 1755w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/SlideSwipe_f.png?resize=300%2C249&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/SlideSwipe_f.png?resize=768%2C637&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/SlideSwipe_f.png?resize=1024%2C849&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 701px) 100vw, 701px\" \/><\/a><\/p>\n<p>Now, if you move a hand in front of the left ultrasonic sensor, your Arduino sends a Left keyboard command resulting in moving the presentation to the previous slide.<\/p>\n<h2>Wrapping Up<\/h2>\n<p>I have always been fascinated by natural user interfaces, the idea of things like the sixth sense (which I hope to share a tutorial on someday) and generally all things that makes telekinesis look like a possibility.<\/p>\n<p>Thus I believe there are several updates that could be made to this project. Instead of using ultrasonic sensors and controlling sides what if we attached an Electroencephalogram (EEG) headset and then do more complex stuff like draw shapes and achieve all this within the simplicity frame the Arduino provides.<\/p>\n<p>So the possibilities are endless. Hack it up!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post you\u2019re going to learn how to set an Arduino UNO board as a USB HID keyboard and use gestures to swipe slides during a presentation.<\/p>\n","protected":false},"author":1,"featured_media":40396,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[303,2,267,264,215,10],"tags":[],"class_list":["post-40395","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-0-arduino","category-arduino","category-arduino-project","category-project","category-sensors","category-a-tutorials"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/how-it-works.png?fit=755%2C573&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/40395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=40395"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/40395\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/40396"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=40395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=40395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=40395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}