Nunchuck 3D

#include <math.h>
#include <Wire.h>
#include <WiiChuck.h>
#include <TVout.h>
#include <stdio.h>
#include "notedefs.h"
#include "lib3d.h"

#define    SETUP_WIDTH  120
#define    WIDTH   119                  // visible width...
#define    HEIGHT  96

#define    SOUND_OUT_PIN    2

#define    MAX_ROLL  15                // nunchuck max roll
#define    MAX_PITCH  90                // nunchuck max pitch

TVout     cTV;
WiiChuck  cChuck = WiiChuck();

Scene3D  cScene;

int    iLeftY = HEIGHT / 2;
int    iRightY = HEIGHT / 2;

int angleStart, currentAngle;
int tillerStart = 0;
double angle;

#define  CUBE    20

void setup()
{
 cChuck.begin();
 cChuck.update();
 cChuck.calibrateJoy();

 cTV.begin(_PAL,SETUP_WIDTH,HEIGHT);    //for devices with only 1k sram(m168) use TV.begin(_PAL,128,56)
 cTV.clear_screen();

 cScene.CreateScene(WIDTH,HEIGHT,&cTV);
 cScene.AddCube(0,0,0,CUBE);
}

void loop()
{
 char    buffer[20];
 int     iRoll;
 float   fRollAngle;
 int     iPitch;
 float   fPitchAngle;

 cTV.delay_frame(1);
 cChuck.update();
 cTV.clear_screen();

 iRoll = cChuck.readRoll();
 fRollAngle = (PI * iRoll)/MAX_ROLL;

 iPitch = cChuck.readPitch();
 fPitchAngle = (PI * iPitch)/MAX_PITCH;

 sprintf(buffer,"%d %d",iRoll,iPitch);
 cTV.print_str(0,0,buffer);

//  cTV.draw_line(0,iLeftY - iRoll, WIDTH, iLeftY + iRoll, 1);

 cScene.SetHAngle(fRollAngle);
 cScene.SetVAngle(fPitchAngle);
 cScene.UpdateDisplay();

 if (iRoll > MAX_ROLL)
 {
 cTV.tone( NOTE_D4 );
 }
 else if (iRoll < -MAX_ROLL)
 {
 cTV.tone( NOTE_B4 );
 }
 else
 {
 cTV.noTone();
 }

/*
 Serial.print(", ");  
 Serial.print(chuck.readPitch());
 Serial.print(", ");  

 Serial.print((int)chuck.readAccelX());
 Serial.print(", ");  
 Serial.print((int)chuck.readAccelY());
 Serial.print(", ");  

 Serial.print((int)chuck.readAccelZ());

//  Serial.print(", ");    
//  Serial.print((int)chuck.readJoyX());
//  Serial.print(", ");  
//  Serial.print((int)chuck.readJoyY()); 

 Serial.println();

 */

}

3 thoughts on “Nunchuck 3D

Leave a Reply

Your email address will not be published. Required fields are marked *