Offline
Jelly Stone park, MD USA
uXe wrote:

Which makes the code:

#include <MIDI.h>

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 &= ~B00001111; // pins 8, 9, 10 and 11
  PORTC &= ~B00001111; // pins A0, A1, A2 and A3
  
  MIDI.begin(MIDI_CHANNEL_OMNI);
}

void loop()
{
  if (MIDI.read() && (MIDI.getType() < B11110000))
  {
    if (MIDI.getType() == NoteOn && MIDI.getData2() > 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 && MIDI.getData2() == 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() == PitchBend)
    {
      unmappedBend = (int)((MIDI.getData1() & B01111111) | ((MIDI.getData2() & B01111111) << 7));
      mappedBend = map(unmappedBend, 0, 16383, 165, 5); // Arkanoid Paddle range, centered at 170 (+/-80)

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

You'r quick! Too cool! I'll get a pad wired up ASAP, in between Xmas stuff, can't waiit.
Yogi

Last edited by yogi (Dec 22, 2013 5:21 pm)

Offline
Melbourne, Australia

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?! hmm

Offline
Jelly Stone park, MD USA

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'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.
Yogi

Last edited by yogi (Dec 23, 2013 1:30 am)