<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[ChipMusic.org - FamiiDI: MIDI for the NES / Famicom (plus a Wii Nunchuk interface!)]]></title>
	<link rel="self" href="https://chipmusic.org:80/forums/feed/atom/topic/12998/"/>
	<updated>2013-12-23T01:25:09Z</updated>
	<generator>PunBB</generator>
	<id>https://chipmusic.org/forums/topic/12998/famiidi-midi-for-the-nes-famicom-plus-a-wii-nunchuk-interface/</id>
		<entry>
			<title type="html"><![CDATA[Re: FamiiDI: MIDI for the NES / Famicom (plus a Wii Nunchuk interface!)]]></title>
			<link rel="alternate" href="https://chipmusic.org/forums/post/195830/#p195830"/>
			<content type="html"><![CDATA[<p>All the Arduino is doing is providing a GND path, for current flowing from the pull-up. If you press a button at the same time, all you will get is another path to GND-no change. It&#039;s like a wired NOR circuit, like you have with a I2C bus, The low signal overrides the Hi signal, and two lows is still a low.<br />Yogi</p>]]></content>
			<author>
				<name><![CDATA[yogi]]></name>
				<uri>https://chipmusic.org/yogi</uri>
			</author>
			<updated>2013-12-23T01:25:09Z</updated>
			<id>https://chipmusic.org/forums/post/195830/#p195830</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: FamiiDI: MIDI for the NES / Famicom (plus a Wii Nunchuk interface!)]]></title>
			<link rel="alternate" href="https://chipmusic.org/forums/post/195815/#p195815"/>
			<content type="html"><![CDATA[<p>Can I ask though - if you have the Arduino holding a pin low to simulate a button press, and then you actually press the same button on the joypad and short it to ground, nothing is going to explode is it?! <img src="https://chipmusic.org/forums/img/smilies/hmm.png" width="15" height="15" alt="hmm" /></p>]]></content>
			<author>
				<name><![CDATA[uXe]]></name>
				<uri>https://chipmusic.org/uXe</uri>
			</author>
			<updated>2013-12-22T21:34:32Z</updated>
			<id>https://chipmusic.org/forums/post/195815/#p195815</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: FamiiDI: MIDI for the NES / Famicom (plus a Wii Nunchuk interface!)]]></title>
			<link rel="alternate" href="https://chipmusic.org/forums/post/195795/#p195795"/>
			<content type="html"><![CDATA[<div class="quotebox"><cite>uXe wrote:</cite><blockquote><p>Which makes the code:</p><div class="codebox"><pre><code>#include &lt;MIDI.h&gt;

int unmappedBend;
byte mappedBend;
static byte bitMask[] = {1, 2, 4, 8, 16, 32, 64, 128};

void setup()
{  
  pinMode(0, INPUT); // MIDI IN

  pinMode(11, INPUT); // 4021 D7 (A Button)
  pinMode(10, INPUT); // 4021 D6 (B Button)
  pinMode(9, INPUT); // 4021 D5 (Select)
  pinMode(8, INPUT); // 4021 D4 (Start)

  pinMode(A3, INPUT); // 4021 D3 (Up)
  pinMode(A2, INPUT); // 4021 D2 (Down)
  pinMode(A1, INPUT); // 4021 D1 (Left)
  pinMode(A0, INPUT); // 4021 D0 (Right)

  PORTB &amp;= ~B00001111; // pins 8, 9, 10 and 11
  PORTC &amp;= ~B00001111; // pins A0, A1, A2 and A3
  
  MIDI.begin(MIDI_CHANNEL_OMNI);
}

void loop()
{
  if (MIDI.read() &amp;&amp; (MIDI.getType() &lt; B11110000))
  {
    if (MIDI.getType() == NoteOn &amp;&amp; MIDI.getData2() &gt; 0)
    {
      if (MIDI.getData1() == 60) // C4
        DDRB |= bitMask[3]; // A
      else if (MIDI.getData1() == 62) // D4
        DDRB |= bitMask[2]; // B
      else if (MIDI.getData1() == 64) // E4
        DDRB |= bitMask[1]; // Select
      else if (MIDI.getData1() == 65) // F4
        DDRB |= bitMask[0]; // Start
      else if (MIDI.getData1() == 67) // G4
        DDRC |= bitMask[3]; // Up
      else if (MIDI.getData1() == 69) // A4
        DDRC |= bitMask[2]; // Down
      else if (MIDI.getData1() == 71) // B4
        DDRC |= bitMask[1]; // Left
      else if (MIDI.getData1() == 72) // C5
        DDRC |= bitMask[0]; // Right
    }

    else if (MIDI.getType() == NoteOff || (MIDI.getType() == NoteOn &amp;&amp; MIDI.getData2() == 0))
    {
      if (MIDI.getData1() == 60) // C4
        DDRB &amp;= ~bitMask[3]; // A
      else if (MIDI.getData1() == 62) // D4
        DDRB &amp;= ~bitMask[2]; // B
      else if (MIDI.getData1() == 64) // E4
        DDRB &amp;= ~bitMask[1]; // Select
      else if (MIDI.getData1() == 65) // F4
        DDRB &amp;= ~bitMask[0]; // Start
      else if (MIDI.getData1() == 67) // G4
        DDRC &amp;= ~bitMask[3]; // Up
      else if (MIDI.getData1() == 69) // A4
        DDRC &amp;= ~bitMask[2]; // Down
      else if (MIDI.getData1() == 71) // B4
        DDRC &amp;= ~bitMask[1]; // Left
      else if (MIDI.getData1() == 72) // C5
        DDRC &amp;= ~bitMask[0]; // Right
    }

    else if (MIDI.getType() == PitchBend)
    {
      unmappedBend = (int)((MIDI.getData1() &amp; B01111111) | ((MIDI.getData2() &amp; B01111111) &lt;&lt; 7));
      mappedBend = map(unmappedBend, 0, 16383, 165, 5); // Arkanoid Paddle range, centered at 170 (+/-80)

      // Paddle Position
      if (mappedBend &amp; bitMask[7]) // A
        DDRB |= bitMask[3];
      else
        DDRB &amp;= ~bitMask[3];
      if (mappedBend &amp; bitMask[6]) // B
        DDRB |= bitMask[2];
      else
        DDRB &amp;= ~bitMask[2];
      if (mappedBend &amp; bitMask[5]) // Select
        DDRB |= bitMask[1];
      else
        DDRB &amp;= ~bitMask[1];
      if (mappedBend &amp; bitMask[4]) // Start
        DDRB |= bitMask[0];
      else
        DDRB &amp;= ~bitMask[0];
      if (mappedBend &amp; bitMask[3]) // Up
        DDRC |= bitMask[3];
      else
        DDRC &amp;= ~bitMask[3];
      if (mappedBend &amp; bitMask[2]) // Down
        DDRC |= bitMask[2];
      else
        DDRC &amp;= ~bitMask[2];
      if (mappedBend &amp; bitMask[1]) // Left
        DDRC |= bitMask[1];
      else
        DDRC &amp;= ~bitMask[1];
      if (mappedBend &amp; bitMask[0]) // Right
        DDRC |= bitMask[0];
      else
        DDRC &amp;= ~bitMask[0];
    }      
  }
}</code></pre></div></blockquote></div><p>You&#039;r quick! Too cool! I&#039;ll get a pad wired up ASAP, in between Xmas stuff, can&#039;t waiit. <br />Yogi</p>]]></content>
			<author>
				<name><![CDATA[yogi]]></name>
				<uri>https://chipmusic.org/yogi</uri>
			</author>
			<updated>2013-12-22T17:20:26Z</updated>
			<id>https://chipmusic.org/forums/post/195795/#p195795</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: FamiiDI: MIDI for the NES / Famicom (plus a Wii Nunchuk interface!)]]></title>
			<link rel="alternate" href="https://chipmusic.org/forums/post/195794/#p195794"/>
			<content type="html"><![CDATA[<div class="quotebox"><cite>uXe wrote:</cite><blockquote><div class="quotebox"><cite>yogi wrote:</cite><blockquote><p>void setup ()<br />{<br />&nbsp; pinMode(x, INPUT); // for all the CD4021 pins<br />&nbsp; PORTB &amp;= b00000000; // Arduino pins 8, 9, 10 and 11; PB0:PB3 to Hi-Z inputs<br />&nbsp; PORTC &amp;= b00000000; // Arduino pins A0, A1, A2 and A3; PC0:PC3 to Hi-Z inputs<br />}</p></blockquote></div><p>Not to nit-pick, because I know you were just typing this off the top of your head, but I think what you mean is:</p><p>PORTB &amp;= ~B00001111;<br />PORTC &amp;= ~B00001111;</p><p>to set <em>only</em> those pins to &#039;0&#039;</p><div class="quotebox"><cite>yogi wrote:</cite><blockquote><p>DDRB = (1 &lt;&lt; PBx) // Change pin to output<br />DDRB = (0 &lt;&lt; PBx) // Change pin to Hi-Z input</p></blockquote></div><p>again:</p><p>DDRB |= (1 &lt;&lt; PBx);<br />DDRB &amp;= ~(1 &lt;&lt; PBx);</p><p>to affect <em>only</em> that pin, otherwise you are changing all of the bits on that port, and with Arduinos that can be bad when some of them are permanently assigned as reset or crystal pins...</p></blockquote></div><p>Glad you caught this,Thanks. sorry for my confusion. <br />My poor C skills are showing <img src="https://chipmusic.org/forums/img/smilies/sad.png" width="15" height="15" alt="sad" /> Let this be a lesson: &#039;Compound bitwise logic and late night posting don&#039;t mix&#039;!</p>]]></content>
			<author>
				<name><![CDATA[yogi]]></name>
				<uri>https://chipmusic.org/yogi</uri>
			</author>
			<updated>2013-12-22T17:11:44Z</updated>
			<id>https://chipmusic.org/forums/post/195794/#p195794</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: FamiiDI: MIDI for the NES / Famicom (plus a Wii Nunchuk interface!)]]></title>
			<link rel="alternate" href="https://chipmusic.org/forums/post/195788/#p195788"/>
			<content type="html"><![CDATA[<p>Which makes the code:</p><div class="codebox"><pre><code>#include &lt;MIDI.h&gt;

int unmappedBend;
byte mappedBend;
static byte bitMask[] = {1, 2, 4, 8, 16, 32, 64, 128};

void setup()
{  
  pinMode(0, INPUT); // MIDI IN

  pinMode(11, INPUT); // 4021 D7 (A Button)
  pinMode(10, INPUT); // 4021 D6 (B Button)
  pinMode(9, INPUT); // 4021 D5 (Select)
  pinMode(8, INPUT); // 4021 D4 (Start)

  pinMode(A3, INPUT); // 4021 D3 (Up)
  pinMode(A2, INPUT); // 4021 D2 (Down)
  pinMode(A1, INPUT); // 4021 D1 (Left)
  pinMode(A0, INPUT); // 4021 D0 (Right)

  PORTB &amp;= ~B00001111; // pins 8, 9, 10 and 11
  PORTC &amp;= ~B00001111; // pins A0, A1, A2 and A3
  
  MIDI.begin(MIDI_CHANNEL_OMNI);
}

void loop()
{
  if (MIDI.read() &amp;&amp; (MIDI.getType() &lt; B11110000))
  {
    if (MIDI.getType() == NoteOn &amp;&amp; MIDI.getData2() &gt; 0)
    {
      if (MIDI.getData1() == 60) // C4
        DDRB |= bitMask[3]; // A
      else if (MIDI.getData1() == 62) // D4
        DDRB |= bitMask[2]; // B
      else if (MIDI.getData1() == 64) // E4
        DDRB |= bitMask[1]; // Select
      else if (MIDI.getData1() == 65) // F4
        DDRB |= bitMask[0]; // Start
      else if (MIDI.getData1() == 67) // G4
        DDRC |= bitMask[3]; // Up
      else if (MIDI.getData1() == 69) // A4
        DDRC |= bitMask[2]; // Down
      else if (MIDI.getData1() == 71) // B4
        DDRC |= bitMask[1]; // Left
      else if (MIDI.getData1() == 72) // C5
        DDRC |= bitMask[0]; // Right
    }

    else if (MIDI.getType() == NoteOff || (MIDI.getType() == NoteOn &amp;&amp; MIDI.getData2() == 0))
    {
      if (MIDI.getData1() == 60) // C4
        DDRB &amp;= ~bitMask[3]; // A
      else if (MIDI.getData1() == 62) // D4
        DDRB &amp;= ~bitMask[2]; // B
      else if (MIDI.getData1() == 64) // E4
        DDRB &amp;= ~bitMask[1]; // Select
      else if (MIDI.getData1() == 65) // F4
        DDRB &amp;= ~bitMask[0]; // Start
      else if (MIDI.getData1() == 67) // G4
        DDRC &amp;= ~bitMask[3]; // Up
      else if (MIDI.getData1() == 69) // A4
        DDRC &amp;= ~bitMask[2]; // Down
      else if (MIDI.getData1() == 71) // B4
        DDRC &amp;= ~bitMask[1]; // Left
      else if (MIDI.getData1() == 72) // C5
        DDRC &amp;= ~bitMask[0]; // Right
    }

    else if (MIDI.getType() == PitchBend)
    {
      unmappedBend = (int)((MIDI.getData1() &amp; B01111111) | ((MIDI.getData2() &amp; B01111111) &lt;&lt; 7));
      mappedBend = map(unmappedBend, 0, 16383, 165, 5); // Arkanoid Paddle range, centered at 170 (+/-80)

      // Paddle Position
      if (mappedBend &amp; bitMask[7]) // A
        DDRB |= bitMask[3];
      else
        DDRB &amp;= ~bitMask[3];
      if (mappedBend &amp; bitMask[6]) // B
        DDRB |= bitMask[2];
      else
        DDRB &amp;= ~bitMask[2];
      if (mappedBend &amp; bitMask[5]) // Select
        DDRB |= bitMask[1];
      else
        DDRB &amp;= ~bitMask[1];
      if (mappedBend &amp; bitMask[4]) // Start
        DDRB |= bitMask[0];
      else
        DDRB &amp;= ~bitMask[0];
      if (mappedBend &amp; bitMask[3]) // Up
        DDRC |= bitMask[3];
      else
        DDRC &amp;= ~bitMask[3];
      if (mappedBend &amp; bitMask[2]) // Down
        DDRC |= bitMask[2];
      else
        DDRC &amp;= ~bitMask[2];
      if (mappedBend &amp; bitMask[1]) // Left
        DDRC |= bitMask[1];
      else
        DDRC &amp;= ~bitMask[1];
      if (mappedBend &amp; bitMask[0]) // Right
        DDRC |= bitMask[0];
      else
        DDRC &amp;= ~bitMask[0];
    }      
  }
}</code></pre></div>]]></content>
			<author>
				<name><![CDATA[uXe]]></name>
				<uri>https://chipmusic.org/uXe</uri>
			</author>
			<updated>2013-12-22T13:18:24Z</updated>
			<id>https://chipmusic.org/forums/post/195788/#p195788</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: FamiiDI: MIDI for the NES / Famicom (plus a Wii Nunchuk interface!)]]></title>
			<link rel="alternate" href="https://chipmusic.org/forums/post/195780/#p195780"/>
			<content type="html"><![CDATA[<div class="quotebox"><cite>yogi wrote:</cite><blockquote><p>void setup ()<br />{<br />&nbsp; pinMode(x, INPUT); // for all the CD4021 pins<br />&nbsp; PORTB &amp;= b00000000; // Arduino pins 8, 9, 10 and 11; PB0:PB3 to Hi-Z inputs<br />&nbsp; PORTC &amp;= b00000000; // Arduino pins A0, A1, A2 and A3; PC0:PC3 to Hi-Z inputs<br />}</p></blockquote></div><p>Not to nit-pick, because I know you were just typing this off the top of your head, but I think what you mean is:</p><p>PORTB &amp;= ~B00001111;<br />PORTC &amp;= ~B00001111;</p><p>to set <em>only</em> those pins to &#039;0&#039;</p><div class="quotebox"><cite>yogi wrote:</cite><blockquote><p>DDRB = (1 &lt;&lt; PBx) // Change pin to output<br />DDRB = (0 &lt;&lt; PBx) // Change pin to Hi-Z input</p></blockquote></div><p>again:</p><p>DDRB |= (1 &lt;&lt; PBx);<br />DDRB &amp;= ~(1 &lt;&lt; PBx);</p><p>to affect <em>only</em> that pin, otherwise you are changing all of the bits on that port, and with Arduinos that can be bad when some of them are permanently assigned as reset or crystal pins...</p>]]></content>
			<author>
				<name><![CDATA[uXe]]></name>
				<uri>https://chipmusic.org/uXe</uri>
			</author>
			<updated>2013-12-22T11:07:08Z</updated>
			<id>https://chipmusic.org/forums/post/195780/#p195780</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: FamiiDI: MIDI for the NES / Famicom (plus a Wii Nunchuk interface!)]]></title>
			<link rel="alternate" href="https://chipmusic.org/forums/post/195742/#p195742"/>
			<content type="html"><![CDATA[<div class="quotebox"><cite>uXe wrote:</cite><blockquote><p>Although reading in the buttons would have its advantages too - you could write code to implement &#039;turbo&#039; modes etc!</p></blockquote></div><p>First thoughts could be:<br />void setup ()<br />{<br />&nbsp; pinMode(x, INPUT); // for all the CD4021 pins<br />&nbsp; PORTB &amp;= b00000000; // Arduino pins 8, 9, 10 and 11; PB0:PB3 to Hi-Z inputs<br />&nbsp; PORTC &amp;= b00000000; // Arduino pins A0, A1, A2 and A3; PC0:PC3 to Hi-Z inputs<br />}<br />to write a button to NES, with 0x00 loaded into the output port latch<br />&nbsp; pinMode (pin, OUTPUT) ; // &#039;button&#039; press<br />or DDRB = (1&lt;&lt;PBx) // Change pin to output<br />and then to release it<br />&nbsp; pinMode (pin, INPUT) ; // &#039;button&#039; release<br />or DDRB = (0 &lt;&lt; PBx) // Change pin to Hi-Z input</p><p>To read from a pad button<br />&nbsp; PORTx |= _BV[x] ; // while in input mode, set bit<br />read pin and store the bit<br />&nbsp; PORTx &amp;= ~_BV[x] ;&nbsp; //&nbsp; back to Hi-Z, clr bit<br />EDIT- After thinking over the above snippet, there shouldn&#039;t be a problem just reading the pin; Hi-Z is only relevant to not loading a shared connection. With the external pull-up, the button should only be one of two states, and pose no problem just reading the pin.</p><p>Internal pull-ups are &quot;between 20K to 50K&quot;, so that would be ~38K in parallel with 38K=19K of pull-up, should be good. We&#039;ll see<br />Yogi</p>]]></content>
			<author>
				<name><![CDATA[yogi]]></name>
				<uri>https://chipmusic.org/yogi</uri>
			</author>
			<updated>2013-12-22T00:05:29Z</updated>
			<id>https://chipmusic.org/forums/post/195742/#p195742</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: FamiiDI: MIDI for the NES / Famicom (plus a Wii Nunchuk interface!)]]></title>
			<link rel="alternate" href="https://chipmusic.org/forums/post/195736/#p195736"/>
			<content type="html"><![CDATA[<p>Although reading in the buttons would have its advantages too - you could write code to implement &#039;turbo&#039; modes etc!</p>]]></content>
			<author>
				<name><![CDATA[uXe]]></name>
				<uri>https://chipmusic.org/uXe</uri>
			</author>
			<updated>2013-12-21T22:08:36Z</updated>
			<id>https://chipmusic.org/forums/post/195736/#p195736</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: FamiiDI: MIDI for the NES / Famicom (plus a Wii Nunchuk interface!)]]></title>
			<link rel="alternate" href="https://chipmusic.org/forums/post/195735/#p195735"/>
			<content type="html"><![CDATA[<div class="quotebox"><cite>yogi wrote:</cite><blockquote><p>Going through your sketch, occurred to me, if you toggle the Arduino pins between button Press = &#039;Outputing&#039; a 0 and button Release = tri-state Input , would be open collector. Not sure how much the AT can drain, but it would work as long as the pad&#039;s pull-ups&nbsp; are high values (low current flow). Not sure if you could Read the buttons, but think you could send button presses to the NES easy.<br />Will cobble together a test sketch to try this out.</p></blockquote></div><p>Good thinking! Will be interesting to see if it works - the &#039;resistors&#039; on the joypad are 38kOhms.</p>]]></content>
			<author>
				<name><![CDATA[uXe]]></name>
				<uri>https://chipmusic.org/uXe</uri>
			</author>
			<updated>2013-12-21T22:04:05Z</updated>
			<id>https://chipmusic.org/forums/post/195735/#p195735</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: FamiiDI: MIDI for the NES / Famicom (plus a Wii Nunchuk interface!)]]></title>
			<link rel="alternate" href="https://chipmusic.org/forums/post/195734/#p195734"/>
			<content type="html"><![CDATA[<div class="quotebox"><cite>uXe wrote:</cite><blockquote><p>Just occurred to me though - not sure if the buttons on the joypad itself could still be functional or if you would need to cut the pins from their previous connections before you wire them to the Arduino... wondering what would happen if you are trying to pull a pin low with the Arduino if it is still being held high by the joypad&#039;s built-in pull-ups, or try to pull a pin low by pressing a button on the joypad when it is being held high by the Arduino?</p></blockquote></div><p>&nbsp; Going through your sketch, occurred to me, if you toggle the Arduino pins between button Press = &#039;Outputing&#039; a 0 and button Release = tri-state Input , would be open collector. Not sure how much the AT can drain, but it would work as long as the pad&#039;s pull-ups&nbsp; are high values (low current flow). Not sure if you could Read the buttons, but think you could send button presses to the NES easy.<br />Will cobble together a test sketch to try this out.<br />Yogi</p>]]></content>
			<author>
				<name><![CDATA[yogi]]></name>
				<uri>https://chipmusic.org/yogi</uri>
			</author>
			<updated>2013-12-21T21:45:25Z</updated>
			<id>https://chipmusic.org/forums/post/195734/#p195734</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: FamiiDI: MIDI for the NES / Famicom (plus a Wii Nunchuk interface!)]]></title>
			<link rel="alternate" href="https://chipmusic.org/forums/post/195678/#p195678"/>
			<content type="html"><![CDATA[<div class="quotebox"><cite>uXe wrote:</cite><blockquote><p>The way I&#039;ve re-written the code is to have a toggle switch on pin 12 to choose between MIDI or Joypad mode, so the two modes would never be running at the same time, and there wouldn&#039;t be any slow-down to worry about... it seems like having the two modes running at once would cause issues / conflicts with both of them wanting to have control of the same 8 bits?</p><p>Also, the code is written with the Duemilanove in mind, so the pin / port values may need to be adjusted for a Teensy - all of the &#039;genuine&#039; Teensy boards seem to have more than just 18 pins available?</p></blockquote></div><p>Cool! Midi or joypad works. Been reading the SPI lib and I think could still read buttons as well as process midi to the NES. Will do some testing.<br /> The boards I order are copies of the Sparkfun ProMicro Leonardo compatible,<br /><a href="https://www.sparkfun.com/products/11098" target="_blank">https://www.sparkfun.com/products/11098</a><br /> $5 w/Free shipping (from HK, ugh!) About the same form factor of the Teensy and same AVR, ATMega32u4. Guess I could burn the Teensy bootloader to it (don&#039;t even know if it comes with a BL?), but the pin def is a little different. <br />Yogi</p>]]></content>
			<author>
				<name><![CDATA[yogi]]></name>
				<uri>https://chipmusic.org/yogi</uri>
			</author>
			<updated>2013-12-21T04:03:26Z</updated>
			<id>https://chipmusic.org/forums/post/195678/#p195678</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: FamiiDI: MIDI for the NES / Famicom (plus a Wii Nunchuk interface!)]]></title>
			<link rel="alternate" href="https://chipmusic.org/forums/post/195674/#p195674"/>
			<content type="html"><![CDATA[<p>The way I&#039;ve re-written the code is to have a toggle switch on pin 12 to choose between MIDI or Joypad mode, so the two modes would never be running at the same time, and there wouldn&#039;t be any slow-down to worry about... it seems like having the two modes running at once would cause issues / conflicts with both of them wanting to have control of the same 8 bits?</p><p>Also, the code is written with the Duemilanove in mind, so the pin / port values may need to be adjusted for a Teensy - all of the &#039;genuine&#039; Teensy boards seem to have more than just 18 pins available?</p>]]></content>
			<author>
				<name><![CDATA[uXe]]></name>
				<uri>https://chipmusic.org/uXe</uri>
			</author>
			<updated>2013-12-21T02:33:09Z</updated>
			<id>https://chipmusic.org/forums/post/195674/#p195674</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: FamiiDI: MIDI for the NES / Famicom (plus a Wii Nunchuk interface!)]]></title>
			<link rel="alternate" href="https://chipmusic.org/forums/post/195673/#p195673"/>
			<content type="html"><![CDATA[<div class="quotebox"><cite>uXe wrote:</cite><blockquote><div class="quotebox"><cite>yogi wrote:</cite><blockquote><p>A brute force approach could be adding a second 4021, with the arduino in the middle-reading the KB 4021 and outputting a parallel byte to the second NES 4021.</p></blockquote></div><p>Another alternative would be to go ahead and cut the shift register input pins from their original connections to the joypad, wire them up to the Arduino for MIDI control, and then make connections directly to the pads on the controller PCB for detecting button presses - read them into the Arduino directly and bypass the shift register! Simple on / off, like how an Atari joystick works - just wire eight Arduino pins to the pads highlighted here in blue, the pins should be held high by the joypad&#039;s pull-ups and then when the buttons are pressed those pins should be pulled low:</p><p><a class="postimg" href="http://www.slagcoin.com/joystick/pcb_diagrams/nes_diagram1.jpg" title="http://www.slagcoin.com/joystick/pcb_diagrams/nes_diagram1.jpg" id="forum_image_74857234"><img src="http://www.slagcoin.com/joystick/pcb_diagrams/nes_diagram1.jpg" /></a></p></blockquote></div><p>My only concern with letting the Arduino scan the buttons is pin count, but it&#039;s just doable on a Teensy sized form factor. 16 digital +2 uart pins out of 18 total. <br /> The NESpad lib you pointed to is good, but it seems to use digitalRead and digitalWrite. Which is OK, but burns allot of cycles. I think the AVR&#039;s SPI hardware would be a better choice with the 4021, but I may be overlooking something, Will have to play around with some hardware. <br /> The cost of an extra 4021 is small and if the SPI works out it wouldn&#039;t slow down your main code much compared to reading 8 pins. <br />Yogi</p>]]></content>
			<author>
				<name><![CDATA[yogi]]></name>
				<uri>https://chipmusic.org/yogi</uri>
			</author>
			<updated>2013-12-21T01:36:00Z</updated>
			<id>https://chipmusic.org/forums/post/195673/#p195673</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: FamiiDI: MIDI for the NES / Famicom (plus a Wii Nunchuk interface!)]]></title>
			<link rel="alternate" href="https://chipmusic.org/forums/post/195650/#p195650"/>
			<content type="html"><![CDATA[<p>Here&#039;s the updated code - a <a href="http://arduino.cc/en/uploads/Tutorial/button_schem.png" target="_blank">toggle switch</a> connected to pin 12 will select between MIDI or Joypad mode:</p><p><strong>EDIT:</strong> updated again...</p><div class="codebox"><pre><code>#include &lt;MIDI.h&gt;

int unmappedBend;
byte mappedBend;
static byte bitMask[] = {1, 2, 4, 8, 16, 32, 64, 128};

void setup()
{  
  pinMode(0, INPUT); // MIDI IN

  pinMode(11, OUTPUT); // 4021 D7 (A Button)
  pinMode(10, OUTPUT); // 4021 D6 (B Button)
  pinMode(9, OUTPUT); // 4021 D5 (Select)
  pinMode(8, OUTPUT); // 4021 D4 (Start)

  pinMode(A3, OUTPUT); // 4021 D3 (Up)
  pinMode(A2, OUTPUT); // 4021 D2 (Down)
  pinMode(A1, OUTPUT); // 4021 D1 (Left)
  pinMode(A0, OUTPUT); // 4021 D0 (Right)

  pinMode(A5, INPUT); // A Button
  pinMode(A4, INPUT); // B Button
  pinMode(2, INPUT); // Select
  pinMode(3, INPUT); // Start
  pinMode(4, INPUT); // Up
  pinMode(5, INPUT); // Down
  pinMode(6, INPUT); // Left
  pinMode(7, INPUT); // Right

  pinMode(12, INPUT); // Toggle Switch

  // set OUTPUT pins HIGH
  PORTB |= B00001111; // pins 8, 9, 10 and 11
  PORTC |= B00001111; // pins A0, A1, A2 and A3
  
  MIDI.begin(MIDI_CHANNEL_OMNI);
}

void loop()
{
  if (PINB &amp; bitMask[4])
  {
    if (MIDI.read() &amp;&amp; (MIDI.getType() &lt; B11110000))
    {
      if (MIDI.getType() == NoteOn &amp;&amp; MIDI.getData2() &gt; 0)
      {
        if (MIDI.getData1() == 60) // C4
          PORTB &amp;= ~bitMask[3]; // A
        else if (MIDI.getData1() == 62) // D4
          PORTB &amp;= ~bitMask[2]; // B
        else if (MIDI.getData1() == 64) // E4
          PORTB &amp;= ~bitMask[1]; // Select
        else if (MIDI.getData1() == 65) // F4
          PORTB &amp;= ~bitMask[0]; // Start
        else if (MIDI.getData1() == 67) // G4
          PORTC &amp;= ~bitMask[3]; // Up
        else if (MIDI.getData1() == 69) // A4
          PORTC &amp;= ~bitMask[2]; // Down
        else if (MIDI.getData1() == 71) // B4
          PORTC &amp;= ~bitMask[1]; // Left
        else if (MIDI.getData1() == 72) // C5
          PORTC &amp;= ~bitMask[0]; // Right
      }

      else if (MIDI.getType() == NoteOff || (MIDI.getType() == NoteOn &amp;&amp; MIDI.getData2() == 0))
      {
        if (MIDI.getData1() == 60) // C4
          PORTB |= bitMask[3]; // A
        else if (MIDI.getData1() == 62) // D4
          PORTB |= bitMask[2]; // B
        else if (MIDI.getData1() == 64) // E4
          PORTB |= bitMask[1]; // Select
        else if (MIDI.getData1() == 65) // F4
          PORTB |= bitMask[0]; // Start
        else if (MIDI.getData1() == 67) // G4
          PORTC |= bitMask[3]; // Up
        else if (MIDI.getData1() == 69) // A4
          PORTC |= bitMask[2]; // Down
        else if (MIDI.getData1() == 71) // B4
          PORTC |= bitMask[1]; // Left
        else if (MIDI.getData1() == 72) // C5
          PORTC |= bitMask[0]; // Right
      }

      else if (MIDI.getType() == PitchBend)
      {
        unmappedBend = (int)((MIDI.getData1() &amp; B01111111) | ((MIDI.getData2() &amp; B01111111) &lt;&lt; 7));
        mappedBend = map(unmappedBend, 0, 16383, 165, 5); // Arkanoid Paddle range, centered at 170 (+/-80)

        // Paddle Position
        if (mappedBend &amp; bitMask[7]) // A
          PORTB &amp;= ~bitMask[3];
        else
          PORTB |= bitMask[3];
        if (mappedBend &amp; bitMask[6]) // B
          PORTB &amp;= ~bitMask[2];
        else
          PORTB |= bitMask[2];
        if (mappedBend &amp; bitMask[5]) // Select
          PORTB &amp;= ~bitMask[1];
        else
          PORTB |= bitMask[1];
        if (mappedBend &amp; bitMask[4]) // Start
          PORTB &amp;= ~bitMask[0];
        else
          PORTB |= bitMask[0];
        if (mappedBend &amp; bitMask[3]) // Up
          PORTC &amp;= ~bitMask[3];
        else
          PORTC |= bitMask[3];
        if (mappedBend &amp; bitMask[2]) // Down
          PORTC &amp;= ~bitMask[2];
        else
          PORTC |= bitMask[2];
        if (mappedBend &amp; bitMask[1]) // Left
          PORTC &amp;= ~bitMask[1];
        else
          PORTC |= bitMask[1];
        if (mappedBend &amp; bitMask[0]) // Right
          PORTC &amp;= ~bitMask[0];
        else
          PORTC |= bitMask[0];
      }      
    }
  }

  else
  {
    delay(1);

    // read Joypad buttons directly
    if (PINC &amp; bitMask[5]) // A
      PORTB |= bitMask[3];
    else
      PORTB &amp;= ~bitMask[3];
    if (PINC &amp; bitMask[4]) // B
      PORTB |= bitMask[2];
    else
      PORTB &amp;= ~bitMask[2];
    if (PIND &amp; bitMask[2]) // Select
      PORTB |= bitMask[1];
    else
      PORTB &amp;= ~bitMask[1];
    if (PIND &amp; bitMask[3]) // Start
      PORTB |= bitMask[0];
    else
      PORTB &amp;= ~bitMask[0];
    if (PIND &amp; bitMask[4]) // Up
      PORTC |= bitMask[3];
    else
      PORTC &amp;= ~bitMask[3];
    if (PIND &amp; bitMask[5]) // Down
      PORTC |= bitMask[2];
    else
      PORTC &amp;= ~bitMask[2];
    if (PIND &amp; bitMask[6]) // Left
      PORTC |= bitMask[1];
    else
      PORTC &amp;= ~bitMask[1];
    if (PIND &amp; bitMask[7]) // Right
      PORTC |= bitMask[0];
    else
      PORTC &amp;= ~bitMask[0];
  }
}</code></pre></div>]]></content>
			<author>
				<name><![CDATA[uXe]]></name>
				<uri>https://chipmusic.org/uXe</uri>
			</author>
			<updated>2013-12-20T22:55:31Z</updated>
			<id>https://chipmusic.org/forums/post/195650/#p195650</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: FamiiDI: MIDI for the NES / Famicom (plus a Wii Nunchuk interface!)]]></title>
			<link rel="alternate" href="https://chipmusic.org/forums/post/195639/#p195639"/>
			<content type="html"><![CDATA[<div class="quotebox"><cite>yogi wrote:</cite><blockquote><p>A brute force approach could be adding a second 4021, with the arduino in the middle-reading the KB 4021 and outputting a parallel byte to the second NES 4021.</p></blockquote></div><p>Another alternative would be to go ahead and cut the shift register input pins from their original connections to the joypad, wire them up to the Arduino for MIDI control, and then make connections directly to the pads on the controller PCB for detecting button presses - read them into the Arduino directly and bypass the shift register! Simple on / off, like how an Atari joystick works - just wire eight Arduino pins to the pads highlighted here in blue, the pins should be held high by the joypad&#039;s pull-ups and then when the buttons are pressed those pins should be pulled low:</p><p><a class="postimg" href="http://www.slagcoin.com/joystick/pcb_diagrams/nes_diagram1.jpg" title="http://www.slagcoin.com/joystick/pcb_diagrams/nes_diagram1.jpg" id="forum_image_23442576"><img src="http://www.slagcoin.com/joystick/pcb_diagrams/nes_diagram1.jpg" /></a></p>]]></content>
			<author>
				<name><![CDATA[uXe]]></name>
				<uri>https://chipmusic.org/uXe</uri>
			</author>
			<updated>2013-12-20T21:26:09Z</updated>
			<id>https://chipmusic.org/forums/post/195639/#p195639</id>
		</entry>
</feed>
