Jump to content





NUNCHUK + ARDUINO = ATARI JOYSTICK

Posted by k-Pack, 05 December 2014 · 1,739 views

nunchuk arduino joystick
 
Attached Image
 
Attached Image
 
The Arduino is an open source microprocessor system that is being used to by many hobbyist and hackers for what seems to be endless possibilities.  One such possibility is to read the data from a  Wii Nunchuk and send it to a computer through the USB port. This is explained very well on the Arduino web site.
 
  
What I have done is to manipulate the input from the Wii Nunchuk and send it out to the Joystick port  to mimic the classic Atari Joystick. 
 
I would rather think of this write-up as a how I did it guide.  There are so many ways to accomplish a task with the Arduino board and I would be mistaken to think that this is the only or the best.  There are also other microprocessors that can accomplish this.
 
Step 1.  Get nunchuk talking to the Arduino.
 
The Arduino web site has all the information you need to do this.  The Arduino uses the C++ programming language, which allows the use of defined classes and libraries.  You can download the nunchuk class to your IBM or MAC for use in your sketches (the Arduino word for programs).  The example program for the nunchuk class has the Arduino read the nunchuk data stream, transform the date for the x-y positions, the z and c buttons, and the acceleration sensors, and then sends the data to the serial monitor in the Arduino programming software.
 
A nunchuk extension cable was used to connect the nunchuk to the Arduino.  Plug the nunchuk into the extenstion cable, cut off the connector on the unconnected end, solder pins on the 4 wires and insert them into the prototype board. There are alternatives.
 
Make these connections between the nunchuk and Arduino.
GND to GND
3.3V to 3.3V
Data to Analog Pin 4
Clock to Analog Pin 5
  
That’s the easy way. I made it a little harder. 
 
In the book Arduino-A Quick-Start Guide by Maik Schmidt are the instructions and a listing of his nunchuk class library.  I learned a lot about programming the Atari by typing in the program listings and hoped that it would help with C.  The typing helped a little and the trouble shooting helped a lot. I have the first addition of the book and several of functions have had a name change and libray names were changed.  The book is in its second addition, I was unable to access it to see if his program had been updated.
 
The modified program listings are the files I used are:
 

Wii_Nunchuk.h -
// Nunchuck Class 
// Page142 Arduino A Quick-Start Guide
//         By Maik Schmidt
//
#ifndef _NUNCHUK_H_
#define _NUNCHUK_H_




#define NUNCHUK_BUFFER_SIZE 6


class Nunchuk{
  public:
  void initialize();
  boolean update();
  
  int joystick_x() const { return _buffer[0]; }
  int joystick_y() const { return _buffer[1]; }
  
  int x_acceleration() const {
    return ((int)(_buffer[2]) << 2) | ((_buffer[5] >> 2) & 0x03);
  }
  
  int y_acceleration() const {
    return ((int)(_buffer[3]) << 2) | ((_buffer[5] >> 4) & 0x03);
  }
  
  int z_acceleration () const {
    return ((int)(_buffer[4]) << 2) | ((_buffer[5] >> 6) & 0x03);
  }
  
  boolean z_button() const { return ! (_buffer[5] & 0x01); }
  
  boolean c_button() const { return ! (_buffer[5] & 0x02); }
  
private:
  void request_data();
  char decode_byte(const char);
  
  unsigned char _buffer[NUNCHUK_BUFFER_SIZE];
  
};


#endif
 
Wii_Nunchuk.cpp
 
 
#include <arduino.h>
#include <Wire.h>
#include "Wii_Nunchuk.h"


#define NUNCHUK_DEVICE_ID  0x52


void Nunchuk::initialize(){
  Wire.begin();
  Wire.beginTransmission(NUNCHUK_DEVICE_ID);
  Wire.write(0x40);
  Wire.write(0x00);
  Wire.endTransmission();
  update();
}


boolean Nunchuk::update() {
  delay(1);
  Wire.requestFrom(NUNCHUK_DEVICE_ID,NUNCHUK_BUFFER_SIZE);
  int byte_counter = 0;
  while (Wire.available() && byte_counter < NUNCHUK_BUFFER_SIZE)
    _buffer[byte_counter++] = decode_byte(Wire.read());
  request_data();
  return byte_counter == NUNCHUK_BUFFER_SIZE;
}
void Nunchuk::request_data(){
  Wire.beginTransmission(NUNCHUK_DEVICE_ID);
  Wire.write(0x00);
  Wire.endTransmission();
}


char Nunchuk::decode_byte(const char b){
  return (b ^ 0x17) + 0x17;
}
 
Example program.
nunchuk_cal.ino
 
// using Wii_Nunchuk.h to read nunchuk data with monitor.
//page 145 Arduino: A quick-Start Guide by Maik Schmidt
#include <arduino.h>
#include <Wire.h>
#include <Wii_Nunchuk.h>


const unsigned int BAUD_RATE = 19200;
Nunchuk nunchuk;


void setup() {
  Serial.begin(BAUD_RATE);
  nunchuk.initialize();
}


void loop() {
  if (nunchuk.update()) {
    Serial.print(nunchuk.joystick_x());
    Serial.print(" ");
    Serial.print(nunchuk.joystick_y());
    Serial.print(" ");
    Serial.print(nunchuk.x_acceleration());
    Serial.print(" ");
    Serial.print(nunchuk.y_acceleration());
    Serial.print(" ");
    Serial.print(nunchuk.z_acceleration());
    Serial.print(" ");
    Serial.print(nunchuk.c_button());
    Serial.print(" ");
    Serial.println(nunchuk.z_button());
  }
}
 
 
Step 2. Transform the nunchuk data and set digital outputs
 
The first thing you learn to do with the Arduino is turn on and off a LEDs.  You will need 5 LEDs and 5 – 330ohm resistors.  Hook them up to digital pins 3 – 7 and ground.  One LED of each of the switches in the joystick.
 
Attached Image
 
 
Now input the following sketch and compile it to the Arduino.  If you use a different library there are changes that may be needed.
 
 
 
Atari sketch.
atari_nunchuk.ino
 
//Use a Wii Nunchuk for an Atari Joystick
//Pack007 - 2015


#include <arduino.h>
#include <Wire.h>
#include <Wii_Nunchuk.h>   // from quick start book


int Up_Pin = 3;
int Down_Pin = 4;
int Left_Pin = 5;
int Right_Pin = 6;
int Trig_Pin = 7;


Nunchuk nunchuk;


void setup(){
  nunchuk.initialize();
  pinMode(Up_Pin,OUTPUT);
  pinMode(Down_Pin,OUTPUT);
  pinMode(Left_Pin,OUTPUT);
  pinMode(Right_Pin,OUTPUT);
  pinMode(Trig_Pin,OUTPUT);
}


void loop(){
  if (nunchuk.update()){
    if(nunchuk.joystick_x() > 180)
    {
      digitalWrite(Right_Pin,HIGH);
    }
    else
    {
      digitalWrite(Right_Pin,LOW); 
    }


    if(nunchuk.joystick_x() < 80 )
    {
      digitalWrite(Left_Pin,HIGH);
    }
    else 
    {
      digitalWrite(Left_Pin,LOW);
      }


    if(nunchuk.joystick_y() >180)
    {
      digitalWrite(Up_Pin,HIGH);
      }
      else
      {
      digitalWrite(Up_Pin,LOW);
      }
    
     if (nunchuk.joystick_y() < 80)
     {
      digitalWrite(Down_Pin,HIGH);
     }
     else
      {
        digitalWrite(Down_Pin,LOW);
      }


  if (nunchuk.z_button())
    {
    digitalWrite(Trig_Pin,HIGH);
    }
    else
    {
      digitalWrite(Trig_Pin,LOW);
    }
  }
}
 
With the nunchuk attached and powered up, move the stick around and press the Z trigger.  LEDs should light up and go out depending on the position of the joy stick. That’s it for the programming.
Attached Image
 
Step 3.  Make the Atari connection.
 
Whenever I can, I like to connect my experiments to the Atari using a photocoupler (optoisolator). These are LED/phototransistor pairs that maintain a separation between them.  The LED on one side switches on and off the phototransistor on the other.  The gap is an added measure of safety for the equipment.  Burn up one side or the other, but not both.  Just be sure your voltage is under 5000V or the current could jump the gap.
 
Replace the 5 LEDs with the LED side of 5 photocouplers.   PS2501 worked well for me.  The DIP packages come with 1, 2, and 4 photocouplers.  I used one PS2501-4 and one PS2501-1. I’m not sure how universal these numbers are but these were in my parts box.
 
Attached Image
 
I hope you have a broken joystick with a good cable (or build your own cable if you like. See DE RE Joystick by Adelbert Fernandez in the May 1986 issue of ANTIC).  Connect the wires to the transistor side of the photocouplers as in the schematic drawing.
 
Colors - standard Joystick cable.
Up        White
Down    Blue
Left      Green
Right    Brown
Trig     Orange
GND    Black
 
 
Attached Image
 
Plug the cord into the Atari, power up the Arduino and load up the game.   Let’s play….
 
After thoughts:
 
Maybe this shouldn’t be your first Arduino project.  It wasn’t mine. It depends on where you start on the Arduino learning curve. I started with an Arduino UNO and a kit containing motor, buzzers, LEDs, switches, and several sensors.  It also came with instructions on how to hook them up and program examples.  I’ve also read “Getting Started with the Arduino” by Massimo Banzi and “Programming Arduino – Getting Started with Sketches” by Simon Monk. Both helped.
 
Or maybe I started this journey when I put together a reproduction of a Morse code sender/receiver. That was back around 1959.
 




Cool, now you can play around with the analog XY to direction translation.  Maybe even do pseudo analog (e.g. sending down, none, down, none @ 60Hz) & rapid fire.

  • Report

That may have some interesting possibilities.  I'm wondering if my score would improve by using a pulsed signal for the steering while playing Pole Position.

 

I'm still trying to build a knowledge base on the Arduino and have lots of things to try.  Never sure what I'll try next.  This ones on my bucket list. 

  • Report

Latest Visitors