Skip to content

Instantly share code, notes, and snippets.

@dccourt
Created January 26, 2012 23:38
Show Gist options
  • Save dccourt/1685862 to your computer and use it in GitHub Desktop.
Save dccourt/1685862 to your computer and use it in GitHub Desktop.
Simple Nunchuck/Servo mashup
// Controlling a servo position using a Wii Nunchuck.
// by Fanjita <http://www.fanjita.org/>
// Includes code from 'Knob' sample by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
#include <Wire.h>
#include <Servo.h>
#include "wiichuck.h"
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
// Initialise WiiChuck in pins A2-A5.
nunchuck_setpowerpins();
nunchuck_init();
pinMode(13, OUTPUT);
}
void loop()
{
nunchuck_get_data();
val = nunchuck_joyx(); // 0 - 256
val = map(val, 0, 256, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
// Let's also copy the Z-button state to pin13's built-in LED.
digitalWrite(13, nunchuck_zbutton());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment