Posts about... All (17) Android (2) Hooking Up Parts (10) Game Controllers (3) Robots (2)

Control Servos with a Nintendo NES Controller

If you’ve read my two previous posts How to Hook Up Servos and Hack a Nintendo (NES) Controller, combining the two is simple. This post details how to control 2 servos with a NES controller.

Wiring

There’s only one +5V pin on the Arduino (UNO). You’ll need to use the power rail(s) on a breadboard and some extra jumper wires in order to provide power from the Arduino to the 2 servos and the NES controller. Other than that, you’ll hook up the parts normally.

nes_servos

Code

#include <NESpad.h>
#include <Servo.h>

NESpad nintendo = NESpad(2,3,4);
Servo myServo;
Servo otherServo;

byte state = 0;
int pos = 1; // myServo
int val = 1; // otherServo

void setup() {
myServo.attach(9);
otherServo.attach(10);
Serial.begin(9600);
}

void loop() {

delay(5);

state = nintendo.buttons();

// Left
if (state & NES_LEFT){
if(pos > 1){
pos = pos - 10;
myServo.write(pos);
delay(15);
Serial.println(pos);
}
}

//Right
if (state & NES_RIGHT){
if(pos < 179){
pos = pos + 10;
myServo.write(pos);
delay(15);
Serial.println(pos);
}
}

//B
if (state & NES_B){
if(val > 1){
val = val - 10;
otherServo.write(val);
delay(15);
Serial.println(val);
}

}

// A
if (state & NES_A){
if(val < 179){
val = val + 10;
otherServo.write(val);
delay(15);
Serial.println(val);
}
}

}

Video