Hello everyone!!! I’m working on an Arduino based robot arm that copies human movements. The sensors are potentiometers and a Wii Nunchuck, and the actuators are servomotors. I need help with the code, because I don’t know enough programming yet. The code should move 6 servos: 4 of them connected to potentiometers and the other 2 connected to the Wii Nunchuck (1 responding to the inclination of the Nunchuck and the other to a button of the Nunchuck, but this last one can only be on 2 positions, 0º or 180º).
If at least you could help me with some of the parts I would really appreciate it
I know it’s a lot of work and I don’t really expect to receive any help, but I have to try
PD: Sorry for my bad english
So you need to read the Pots and the WiiNC values and send them to your servos?
The following coded assumes you have correctly wired your project.
to read the pots and send value to servo:
[code]#include <Servo.h>
Servo Servo1;
int potpin1 = 0;
int val;
void setup()
{
Servo1.attach(9);
}
void loop()
{
val = analogRead(potpin1);
val = map(val, 0, 1023, 0, 179);
Servo1.write(val);
delay(15);
}[/code]
A multiple servo set up may look like this: (this would set you up to use a joystick)
[code]#include <Servo.h>
const int servo1 = 3; // first servo
const int servo2 = 10; // second servo
const int servo3 = 5; // third servo
const int servo4 = 11; // fourth servo
const int servo5 = 9; // fifth servo
const int joyH = 2; // L/R Parallax Thumbstick
const int joyV = 3; // U/D Parallax Thumbstick
const int joyX = 4; // L/R Parallax Thumbstick
const int joyP = 5; // U/D Parallax Thumbstick
const int potpin = 0; // O/C potentiometer
int servoVal; // variable to read the value from the analog pin
Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
Servo myservo3; // create servo object to control a servo
Servo myservo4; // create servo object to control a servo
Servo myservo5; // create servo object to control a servo
void setup() {
// Servo
myservo1.attach(servo1); // attaches the servo
myservo2.attach(servo2); // attaches the servo
myservo3.attach(servo3); // attaches the servo
myservo4.attach(servo4); // attaches the servo
myservo5.attach(servo5); // attaches the servo
// Inizialize Serial
Serial.begin(9600);
}
void loop(){
servoVal = analogRead(potpin);
servoVal = map(servoVal, 0, 1023, 0, 179);
myservo5.write(servoVal);
delay(15);
// Display Joystick values using the serial monitor
outputJoystick();
// Read the horizontal joystick value (value between 0 and 1023)
servoVal = analogRead(joyH);
servoVal = map(servoVal, 0, 1023, 0, 180); // scale it to use it with the servo (result between 0 and 180)
myservo2.write(servoVal); // sets the servo position according to the scaled value
// Read the horizontal joystick value (value between 0 and 1023)
servoVal = analogRead(joyV);
servoVal = map(servoVal, 0, 1023, 70, 180); // scale it to use it with the servo (result between 70 and 180)
myservo1.write(servoVal); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
// Read the horizontal joystick value (value between 0 and 1023)
servoVal = analogRead(joyP);
servoVal = map(servoVal, 0, 1023, 70, 180); // scale it to use it with the servo (result between 70 and 180)
myservo4.write(servoVal); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
// Read the horizontal joystick value (value between 0 and 1023)
servoVal = analogRead(joyX);
servoVal = map(servoVal, 0, 1023, 70, 180); // scale it to use it with the servo (result between 70 and 180)
myservo3.write(servoVal); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}[/code]
I have found some info on reading the WiiNC here and you may be able to pull the code from that
create.arduino.cc/projecthub/in … ino-597254
up
Thanks for the help, innerbreed! How would you do to move servos with the inclination of the Nunchuck? (I mean, with the accelerometer that the Nunchuck includes).
And how would you move a servo with a button of the Nunchuck?
/*
- NunchuckPrint
- 2007 Tod E. Kurt, todbot.com/blog/
- Change log:
- Mark Tashiro - Changed Wire.read to Wire.write
- Changed Wire.receive to Wire.read
- Added code for servos
*/
#include <Wire.h>
#include <Servo.h>
Servo servoLeft; // Define left servo
Servo servoRight; // Define right servo
static uint8_t nunchuck_buf[6]; // array to store nunchuck data,
void setup()
{
Serial.begin(19200);
servoLeft.attach(10); // Set left servo to digital pin 10
servoRight.attach(9); // Set right servo to digital pin 9
nunchuck_setpowerpins(); // use analog pins 2&3 as fake gnd & pwr
nunchuck_init(); // send the initilization handshake
Serial.print (“Finished setup\n”);
}
void loop()
{
nunchuck_get_data();
// map nunchuk data to a servo data point
int x_axis = map(nunchuck_buf[0], 23, 222, 180, 0);
int y_axis = map(nunchuck_buf[1], 32, 231, 0, 180);
//move servo to desired position based on Wii nunchuk reading
servoLeft.write(x_axis);
servoRight.write(y_axis);
// un-comment next line to print data to serial monitor
// nunchuck_print_data();
}
//
// Nunchuck functions
//
// Uses port C (analog in) pins as power & ground for Nunchuck
static void nunchuck_setpowerpins()
{
#define pwrpin PORTC3
#define gndpin PORTC2
DDRC |= _BV(pwrpin) | _BV(gndpin);
PORTC &=~ _BV(gndpin);
PORTC |= _BV(pwrpin);
delay(100); // wait for things to stabilize
}
// initialize the I2C system, join the I2C bus,
// and tell the nunchuck we’re talking to it
void nunchuck_init()
{
Wire.begin(); // join i2c bus as master
Wire.beginTransmission(0x52); // transmit to device 0x52
Wire.write(0x40); // sends memory address
Wire.write(0x00); // sends sent a zero.
Wire.endTransmission(); // stop transmitting
}
// Send a request for data to the nunchuck
// was “send_zero()”
void nunchuck_send_request()
{
Wire.beginTransmission(0x52); // transmit to device 0x52
Wire.write(0x00); // sends one byte
Wire.endTransmission(); // stop transmitting
}
// Receive data back from the nunchuck,
int nunchuck_get_data()
{
int cnt=0;
Wire.requestFrom (0x52, 6); // request data from nunchuck
while (Wire.available ()) {
// receive byte as an integer
nunchuck_buf[cnt] = nunchuk_decode_byte(Wire.read());
cnt++;
}
nunchuck_send_request(); // send request for next data payload
// If we recieved the 6 bytes, then go print them
if (cnt >= 5) {
return 1; // success
}
return 0; //failure
}
// Print the input data we have recieved
// accel data is 10 bits long
// so we read 8 bits, then we have to add
// on the last 2 bits. That is why I
// multiply them by 2 * 2
void nunchuck_print_data()
{
static int i=0;
int joy_x_axis = nunchuck_buf[0];
int joy_y_axis = nunchuck_buf[1];
int accel_x_axis = nunchuck_buf[2]; // * 2 * 2;
int accel_y_axis = nunchuck_buf[3]; // * 2 * 2;
int accel_z_axis = nunchuck_buf[4]; // * 2 * 2;
int z_button = 0;
int c_button = 0;
// byte nunchuck_buf[5] contains bits for z and c buttons
// it also contains the least significant bits for the accelerometer data
// so we have to check each bit of byte outbuf[5]
if ((nunchuck_buf[5] >> 0) & 1)
z_button = 1;
if ((nunchuck_buf[5] >> 1) & 1)
c_button = 1;
if ((nunchuck_buf[5] >> 2) & 1)
accel_x_axis += 2;
if ((nunchuck_buf[5] >> 3) & 1)
accel_x_axis += 1;
if ((nunchuck_buf[5] >> 4) & 1)
accel_y_axis += 2;
if ((nunchuck_buf[5] >> 5) & 1)
accel_y_axis += 1;
if ((nunchuck_buf[5] >> 6) & 1)
accel_z_axis += 2;
if ((nunchuck_buf[5] >> 7) & 1)
accel_z_axis += 1;
Serial.print(i,DEC);
Serial.print("\t");
Serial.print(“joy:”);
Serial.print(joy_x_axis,DEC);
Serial.print(",");
Serial.print(joy_y_axis, DEC);
Serial.print(" \t");
Serial.print(“acc:”);
Serial.print(accel_x_axis, DEC);
Serial.print(",");
Serial.print(accel_y_axis, DEC);
Serial.print(",");
Serial.print(accel_z_axis, DEC);
Serial.print("\t");
Serial.print(“but:”);
Serial.print(z_button, DEC);
Serial.print(",");
Serial.print(c_button, DEC);
Serial.print("\r\n"); // newline
i++;
}
// Encode data to format that most wiimote drivers except
// only needed if you use one of the regular wiimote drivers
char nunchuk_decode_byte (char x)
{
x = (x ^ 0x17) + 0x17;
return x;
}
Thanks for the code! Now I can move servos with the joystick and the accel. However, I still have a question: how can I move a servo by pressing a button on the Nunchuck?