PDA

View Full Version : Teensy 2++ & two Wii Nunchucks (Working)



stroffduino
12-15-2012, 04:09 PM
Hardware:
2 Nunchucks (mine are Nyko Kama)
2 Nunchucky's (Adafruit)
2 NPN Transistors
2 Resistors for transistor base (I semi guessed on the values at 10K)
1 Teensy 2++

I wired them up according to the diagram on this page:
http://www.wiimoteproject.com/general-discussion/2-nunchuck-with-arduino-help/
Note:
SCL = Clk = Pin 0 = D0,
SDA = Data = Pin 1 = D1 (connected to collector and emitter of transistors)
Grd connected to Teensy ground
3.3V connected to Teensy 5V

Software:
Using portions of code and library found: http://arduino.cc/forum/index.php?topic=84267.0

I'm not sure if it's necessary, but I did modify the first few lines of twi.h edit as recommended in the top link...



#define ATMEGA8

#ifndef CPU_FREQ^M
#define CPU_FREQ 16000000L
#endif

#ifndef TWI_FREQ^M
#define TWI_FREQ 100000L
#endif


Teensyduino Code:
(Lights up on board LED when C buttons are pushed & Serial prints a bunch of info)





/* circuit schematic can be found at this forum:
http://www.wiimoteproject.com/tech-chat/2-nunchuck-with-arduino-help/
Nunchuks operate off of 3v3 supplied by arduino
First Few lines of twi.h edit...
#define ATMEGA8

#ifndef CPU_FREQ^M
#define CPU_FREQ 16000000L
#endif

#ifndef TWI_FREQ^M
#define TWI_FREQ 100000L
#endif

bd
(aka:johnnyonthespot)
*/

#include <Wire.h>
#include "ArduinoNunchuk.h"

ArduinoNunchuk nunchuk = ArduinoNunchuk();

int xjoystick1;
int yjoystick1;
int xtilt1;
int ytilt1;
int ztilt1;
boolean buttonC1ON;
boolean buttonZ1ON;
boolean buttonCandZ1ON;

int xjoystick2;
int yjoystick2;
int xtilt2;
int ytilt2;
int ztilt2;
boolean buttonC2ON;
boolean buttonZ2ON;
boolean buttonCandZ2ON;

int led = 6;
int nunPin1 = 15;
int nunPin2 = 17;

void setup()
{
pinMode(led, OUTPUT);
Serial.begin(57600);
digitalWrite(nunPin1, HIGH); // BOTH CHUCK ON FOR INIT
digitalWrite(nunPin2, HIGH);
nunchuk.init();
}

void loop ()
{

//.......................change to nunchuck1........................


digitalWrite(nunPin1, HIGH);
digitalWrite(nunPin2, LOW);


//.......................process nunchuck1..........................


nunchuk.update();
if(nunchuk.cButton == 1 && nunchuk.zButton != 1){
buttonC1ON = true;
digitalWrite(led, HIGH);
Serial.println("button pressed 1st Nunchuck");
}
else{
buttonC1ON = false;
digitalWrite(led, LOW);
}
if(nunchuk.zButton == 1 && nunchuk.cButton != 1){
buttonZ1ON = true;
}
else{
buttonZ1ON = false;
}
if(nunchuk.cButton == 1 && nunchuk.zButton == 1){
boolean buttonCandZ1ON = true;
Serial.println("2 buttons 1st Nunchuck");
}
else{
boolean buttonCandZ1ON = false;
}

xjoystick1 = nunchuk.analogX;
xjoystick1 = constrain(xjoystick1, 26, 226);
xjoystick1 = map(xjoystick1, 26, 226, 0, 255);

yjoystick1 = nunchuk.analogY;
yjoystick1 = constrain(yjoystick1, 26, 226);
yjoystick1 = map(yjoystick1, 26, 226, 0, 255);

xtilt1 = nunchuk.accelX;
xtilt1 = constrain(xtilt1, 320, 720);
xtilt1 = map(xtilt1, 320, 720, 0, 255);

ytilt1 = nunchuk.accelY;
ytilt1 = constrain(ytilt1, 320, 720);
ytilt1 = map(ytilt1, 320, 720, 0, 255);

ztilt1 = nunchuk.accelZ;
ztilt1 = constrain(ztilt1, 320, 720);
ztilt1 = map(ztilt1, 320, 720, 0, 255);

Serial.print("jX1: ");
Serial.print(xjoystick1);
Serial.print(" jY1: ");
Serial.print(yjoystick1);
Serial.print(" tX1: ");
Serial.print(xtilt1);
Serial.print(" tY1: ");
Serial.print(ytilt1);
Serial.print(" tZ1: ");
Serial.println(ztilt1);


//.......................change to nunchuck2........................


digitalWrite(nunPin1, LOW);
digitalWrite(nunPin2, HIGH);


//.......................process nunchuck2........................


nunchuk.update();
if(nunchuk.cButton == 1 && nunchuk.zButton != 1){
buttonC2ON = true;
digitalWrite(led, HIGH);
Serial.println("button pressed 2nd Nunchuck");
}
else{
buttonC2ON = false;
digitalWrite(led, LOW);
}
if(nunchuk.zButton == 1 && nunchuk.cButton != 1){
buttonZ2ON = true;
}
else{
buttonZ2ON = false;
}
if(nunchuk.cButton == 1 && nunchuk.zButton == 1){
boolean buttonCandZ2ON = true;
Serial.println("2 buttons 2nd Nunchuck");
}
else{
boolean buttonCandZ2ON = false;
}

xjoystick2 = nunchuk.analogX;
xjoystick2 = constrain(xjoystick2, 26, 226);
xjoystick2 = map(xjoystick2, 26, 226, 0, 255);

yjoystick2 = nunchuk.analogY;
yjoystick2 = constrain(yjoystick2, 26, 226);
yjoystick2 = map(yjoystick2, 26, 226, 0, 255);

xtilt2 = nunchuk.accelX;
xtilt2 = constrain(xtilt2, 320, 720);
xtilt2 = map(xtilt2, 320, 720, 0, 255);

ytilt2 = nunchuk.accelY;
ytilt2 = constrain(ytilt2, 320, 720);
ytilt2 = map(ytilt2, 320, 720, 0, 255);

ztilt2 = nunchuk.accelZ;
ztilt2 = constrain(ztilt2, 320, 720);
ztilt2 = map(ztilt2, 320, 720, 0, 255);

Serial.print("jX2: ");
Serial.print(xjoystick2);
Serial.print(" jY2: ");
Serial.print(yjoystick2);
Serial.print(" tX2: ");
Serial.print(xtilt2);
Serial.print(" tY2: ");
Serial.print(ytilt2);
Serial.print(" tZ2: ");
Serial.print(ztilt2);

}