0

Tutorial: Wii Nunchuck Pan Tilt Servo

Finally i did it! i was with the idea of this project for so long… and finally got time to do it. I know is a very common project on the web, many sites with the videos and tutorials, but is so cool that was worth the time! (and once you have it working, you can do thousands of projects and ideas with it).

 

So, the setup is simple:

 

2014-05-16 10.33.51

We are using:

  • 1x Arduino Uno
  • 1x Nunchuck I2C connector
  • 1x Arduino Proto shield
  • 1x 5v power supply (for the servos)
  • 1x Servo Pan Tilt brackets
  • 2x Servos
  • 1x Wii Nunchuck controller

And here is the code:

// auteur officiel Alexis Couronne skitoo.net
// Modified by @mrlndr for @arduinoarts


#include <Servo.h>
Servo servo1; 
Servo servo2;
#include <Wire.h>

// Doit être ajusté en fonction de chaque nunchuck
#define ZEROX 530  
#define ZEROY 530
#define ZEROZ 530

// adresse I2C du nunchuck
#define WII_NUNCHUK_I2C_ADDRESS 0x52


// définition d'une variable counter
int counter;

// définition d'un tableau de données
uint8_t data[6];

void setup() 
{ 
  // on attache le servomoteur à la pin 11 (PWM)
  servo1.attach(11);
  servo2.attach(12);

  // initialisation du nunchuck
  Wire.begin();

  Wire.beginTransmission(WII_NUNCHUK_I2C_ADDRESS);
  Wire.write(0xF0);
  Wire.write(0x55);
  Wire.endTransmission();

  Wire.beginTransmission(WII_NUNCHUK_I2C_ADDRESS);
  Wire.write(0xFB);
  Wire.write(0x00);
  Wire.endTransmission();
} 

void loop() 
{ 
    // on demande 6 octets au nunchuck
    Wire.requestFrom(WII_NUNCHUK_I2C_ADDRESS, 6);

    counter = 0;
    // tant qu'il y a des données
    while(Wire.available())
    {
      // on récupère les données
      data[counter++] = Wire.read();
    }

    // on réinitialise le nunchuck pour la prochaine demande
    Wire.beginTransmission(WII_NUNCHUK_I2C_ADDRESS);
    Wire.write(0x00);
    Wire.endTransmission();

    if(counter >= 5)
    {
      // on extrait les données
      double accelX = ((data[2] << 2) + ((data[5] >> 2) & 0x03) - ZEROX);
      double accelY = ((data[3] << 2) + ((data[5] >> 4) & 0x03) - ZEROY);
      double accelZ = ((data[4] << 2) + ((data[5] >> 6) & 0x03) - ZEROZ);

      // données d'accélération de l'axe Y
      // on limite la valeur entre -180 et 180
      int value = constrain(accelY, -180, 180);
      // on mappe cette valeur pour le servomoteur soit entre 0 et 180
      value = map(value, -180, 180, 0, 180);
      // on écrit sur le servomoteur la valeur
      servo1.write(180-value);
      
      // données d'accélération de l'axe X
      value = constrain(accelX, -180, 180);
      value = map(value, -180, 180, 0, 180); 
      servo2.write(180-value);

      // un petit delai pour pas saturer le servomoteur
      delay(80);
    }
}

by @mrlndr

Related Posts

  • It´s Summer time!!!! (it means we have some crazy hot days in Madrid... 40º C!!!!) This summer has been quite complicated in Spain, and in my flat i don´t have air conditioning, so i needed to have a solution fast, since the fan was not doing enough... i came up…
  • It finally arrived! the Range Sensor we bought from Ebay! it´s time to see it working with our Arduino UNO board. It´s the Ultrasonic Ranging Module HC-SR04, who works as a sonar, sending and ultrasonic sound and measuring how long does it takes to come back. This delay it´s transformed…
  • Finally we are doing something else than our SM-1 Ikea Robot Lamp. It´s been refreshing doing some other projects and tutorials, because these last days have been everything about the robot. See it in action on this video (and watch out for the fire part!) http://www.youtube.com/watch?v=sbP1CUqC5EA So, let´s get to…