{"id":263,"date":"2023-01-12T18:04:38","date_gmt":"2023-01-12T18:04:38","guid":{"rendered":"https:\/\/trewest.dev\/blog\/?p=263"},"modified":"2023-12-05T09:34:34","modified_gmt":"2023-12-05T09:34:34","slug":"project-arduino-nes-controller","status":"publish","type":"post","link":"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/12\/project-arduino-nes-controller\/","title":{"rendered":"Project: Arduino NES Controller"},"content":{"rendered":"<div class=\"wp-post-series-box series-project-overviews wp-post-series-box--expandable\">\n\t\t\t<input id=\"collapsible-series-project-overviews69d5161e947a6\" class=\"wp-post-series-box__toggle_checkbox\" type=\"checkbox\">\n\t\n\t<label\n\t\tclass=\"wp-post-series-box__label\"\n\t\t\t\t\tfor=\"collapsible-series-project-overviews69d5161e947a6\"\n\t\t\ttabindex=\"0\"\n\t\t\t\t>\n\t\t<p class=\"wp-post-series-box__name wp-post-series-name\">\n\t\t\tThis is post 7 of 21 in the series <em>&ldquo;Project Overviews&rdquo;<\/em>\t\t<\/p>\n\t\t\t\t\t<div class=\"wp-post-series-box__description wp-post-series-description\">\n\t\t\t\t<p>A series of posts detailing and highlighting projects that I&#8217;ve made.<\/p>\n\t\t\t<\/div>\n\t\t\t<\/label>\n\n\t\t\t<div class=\"wp-post-series-box__posts\">\n\t\t\t<ol>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/10\/project-bugtrakt\/\">Project: Bugtrakt<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/10\/project-imgalib\/\">Project: Imgalib<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/10\/project-go-fish\/\">Project: Go Fish!<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/11\/project-trewest-dev\/\">Project: trewest.dev<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/11\/arduino-light-effects\/\">Arduino Light effects<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/11\/project-mini-stacker\/\">Project: Mini Stacker<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><span class=\"wp-post-series-box__current\">Project: Arduino NES Controller<\/span><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/12\/project-metroidvania-like\/\">Project: Metroidvania-Like<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/12\/project-susminer\/\">Project: Susminer<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/12\/project-farming-demo\/\">Project: Farming Demo<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/13\/project-vrc-basement\/\">Project: VRC Basement<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/13\/project-hash-clear\/\">Project: Hash Clear<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/13\/project-titan-help\/\">Project: Titan Help<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/13\/project-computer-craft-scripts\/\">Project: Computer Craft Scripts<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/15\/project-esp8266-plant-watering\/\">Project: ESP8266 Plant Watering<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/15\/project-golf-demo\/\">Project: Golf Demo<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/01\/15\/project-ghost-holic\/\">Project: Ghost Holic<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2023\/03\/27\/project-multi-chat-viewer\/\">Project: Multi-Chat Viewer<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2024\/08\/22\/project-interview-simulator\/\">Project: Interview Simulator<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2024\/10\/18\/project-healers-journey\/\">Project: Healer&#8217;s Journey<\/a><\/li>\n\t\t\t\t\t\t\t\t\t<li><a href=\"https:\/\/trewest.dev\/blog\/index.php\/2024\/10\/18\/project-biota\/\">Project: Biota<\/a><\/li>\n\t\t\t\t\t\t\t<\/ol>\n\t\t<\/div>\n\t<\/div>\n\n<p>Status: Complete<br>Type: Microcontroller<br>Technology: C++, PlatformIO, Arduino <\/p>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/trewest.dev\">Return to Portfolio<\/a><\/div>\n<\/div>\n\n\n\n<p>While learning Arduino I wanted to see if I could use a Nintendo Entertainment System controller to interface, and as it turns out there&#8217;s good diagrams of the pinout the controller uses, and since it&#8217;s keyed it&#8217;s very easy to understand.  <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"480\" height=\"320\" src=\"https:\/\/trewest.dev\/blog\/wp-content\/uploads\/2023\/01\/image-24.png\" alt=\"\" class=\"wp-image-264\" srcset=\"https:\/\/trewest.dev\/blog\/wp-content\/uploads\/2023\/01\/image-24.png 480w, https:\/\/trewest.dev\/blog\/wp-content\/uploads\/2023\/01\/image-24-300x200.png 300w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/figure>\n\n\n\n<p>And turns out I had no idea what I was looking at, so after learning about shift registers I had to write my own code to push through it&#8230; but it did work!  After some effort figuring out how the data was mapped in serial.  Sadly though I don&#8217;t have much more to write about it though, since the code&#8217;s just reading a data pulses and interpreting them as input.  <\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"1080\" style=\"aspect-ratio: 1080 \/ 1080;\" width=\"1080\" controls src=\"https:\/\/trewest.dev\/blog\/wp-content\/uploads\/2023\/01\/20211121-143247-1.mp4\"><\/video><\/figure>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"1080\" style=\"aspect-ratio: 1080 \/ 1080;\" width=\"1080\" controls src=\"https:\/\/trewest.dev\/blog\/wp-content\/uploads\/2023\/01\/20211121-143401-11.mp4\"><\/video><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;Arduino.h&gt;\n#include &lt;FastLED.h&gt;\n\nint clk = 4;\nint latch = 6;\nint data= 8;\n\nbool a = false;\nbool b = false;\nbool start = false;\nbool select = false;\nint pad = 0;\n\nint red= 2;\nint yellow = 3;\nint green = 11;\nint blue = 12;\n\nint pos = 60;\nCRGB color = CRGB(0,255,0);\n\n#define STRIP_SIZE 120\nCRGB leds &#91;STRIP_SIZE];\n\n\nvoid setup() {\n  \/\/ put your setup code here, to run once:\n\n  \/\/Begin serial output\n  Serial.begin(9600);\n\n  FastLED.addLeds&lt;NEOPIXEL, 7&gt;(leds, 120);\n\n  pinMode(data, INPUT);\/\/Getting our data input\n  pinMode(clk, OUTPUT); \/\/ Clock output\n  pinMode(latch, OUTPUT);\n\n  pinMode(red, OUTPUT);\n  pinMode(green, OUTPUT);\n  pinMode(yellow, OUTPUT);\n  pinMode(blue, OUTPUT);\n\n  \n}\n\nvoid printBin(byte inByte) \n\/\/if you try print 00011101 you will get 1101 as the leading zeros don't print\n\/\/this prints a bin number with all the leading zeros and a space between nibbles\n{\n  for (int b = 7; b &gt;= 4; b--) {\n      Serial.print(bitRead(inByte, b));  \n      Serial.print(\" \"); \n      }\n  for (int b = 3; b &gt;= 0; b--) {\n      Serial.print(bitRead(inByte, b));  \/\/now prints:  0001 1101\n  }\n  Serial.println(); \/\/needs a CR at end\n}\n\nbool interpretInput(int num) \n{\n  if (num == 0) {return true;};\n  return false;\n}\n\nvoid loop() {\n  \/\/ put your main code here, to run repeatedly:\n  \/\/Getting data from the 4021\n\n  pad = 0;\n  Serial.println(\"\\nOut:\");\n  digitalWrite(latch, LOW);\n  digitalWrite(latch,HIGH);\n  int readable =  digitalRead(data);\n  Serial.print(readable);\n  a = interpretInput(readable);\n  Serial.print(\" \");\n  digitalWrite(latch, LOW);\n  for (int i = 1; i &lt; 8; i++ ) \n  {\n    digitalWrite(clk, HIGH);\n    int readable =  digitalRead(data);\n    digitalWrite(clk, LOW);\n    Serial.print(readable);\n    switch(i) \n    {\n      case 1: b = interpretInput(readable); break;\n      case 2: select = interpretInput(readable); break;\n      case 3: start = interpretInput(readable); break;\n      case 4: if (readable == 0) {pad = 1;} break;\n      case 5: if (readable == 0) {pad = 2;} break;\n      case 6: if (readable == 0) {pad = 3;} break;\n      case 7: if (readable == 0) {pad = 4;} break;\n    }\n    delay(10);\n    Serial.print(\" \");\n  }\n\n  if (pad == 1) \n  {\n    pos++;\n  }\n  else if ( pad == 2) \n  {\n    pos--;\n  } else if (pad == 3) \n  {\n    if (color == CRGB(0,255,0)) \n    {\n      color = CRGB(0,0,255);\n    } \n    else if (color == CRGB(0,0,255)) \n    {color = CRGB(255,0,0);}\n    else if (color == CRGB(255,0,0)) {color = CRGB(0,255,0);}\n    else { CRGB::White;}\n  } else if (pad == 4) \n  {\n    if (color == CRGB(0,255,0)) \n    {\n      color = CRGB(255,0,0);\n    } \n    else if (color == CRGB(0,0,255)) \n    {color = CRGB(0,255,0);}\n    else if (color == CRGB(255,0,0)) {color = CRGB(0,0,255);}\n    else { CRGB::White;}\n  }\n\n  if (a) \n  {\n    digitalWrite(blue,HIGH);\n    Serial.print(\"A\");\n  }\n  else \n  {\n    digitalWrite(blue,LOW);\n  }\n\n  if (b) \n   {\n    digitalWrite(green,HIGH);\n  }\n  else \n  {\n    digitalWrite(green,LOW);\n  }\n\n  if (select) \n   {\n    digitalWrite(yellow,HIGH);\n  }\n  else \n  {\n    digitalWrite(yellow,LOW);\n  }  \n  if (start)\n   {\n    digitalWrite(red,HIGH);\n  }\n  else \n  {\n    digitalWrite(red,LOW);\n  }\n\n  for (int i = 0; i &lt;STRIP_SIZE; i++) \n  {\n    leds&#91;i] = CRGB::Black;\n  }\n\n  leds&#91;pos] = color;\n  FastLED.show();\n  \/\/byte incoming = shiftIn(dataIn,clockIn,LSBFIRST);\n\n  \/\/incoming = incoming &gt;&gt; 4;\n\n  \/\/printBin(incoming);\n\n}\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>While learning Arduino I wanted to see if I could use a Nintendo Entertainment System controller to interface, and as it turns out there&#8217;s good diagrams of the pinout the controller uses, and since it&#8217;s keyed it&#8217;s very easy to understand<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48,45,4,8],"tags":[26,16,15],"post_series":[53],"class_list":["post-263","post","type-post","status-publish","format-standard","hentry","category-arduino","category-c-coding","category-coding","category-project","tag-arduino","tag-c","tag-project","post_series-project-overviews"],"_links":{"self":[{"href":"https:\/\/trewest.dev\/blog\/index.php\/wp-json\/wp\/v2\/posts\/263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/trewest.dev\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trewest.dev\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trewest.dev\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/trewest.dev\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=263"}],"version-history":[{"count":3,"href":"https:\/\/trewest.dev\/blog\/index.php\/wp-json\/wp\/v2\/posts\/263\/revisions"}],"predecessor-version":[{"id":269,"href":"https:\/\/trewest.dev\/blog\/index.php\/wp-json\/wp\/v2\/posts\/263\/revisions\/269"}],"wp:attachment":[{"href":"https:\/\/trewest.dev\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trewest.dev\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trewest.dev\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=263"},{"taxonomy":"post_series","embeddable":true,"href":"https:\/\/trewest.dev\/blog\/index.php\/wp-json\/wp\/v2\/post_series?post=263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}