Go Down

Topic: Wii nunchuck controlled robot (Read 787 times) previous topic - next topic

Sporerodas

Feb 19, 2017, 11:38 am Last Edit: Feb 19, 2017, 11:43 am by Sporerodas
Hi!
I am going to build a wii nunchuck controlled robot. The nunchuck is going to be wireless connected to the arduino.
I have read XenonJohns instructable and I will also use a wireless chuck and connecting the adapter to a extension cable that is connecting a arduino uno. But I need some help with the coding.
In XenonJohns code are you controlling LEDs but I want to control motors. I'm using a arduino uno and a AF motor shield to control the motors.
The robot should have three legs(motors). If I'm pressing the c button on the chuck, a servo will move 60 degrees. If I'm moving the joystick,the robot will move forward,backward,left or right.


My code with motor controll:


Code: [Select]
#define POWER_VIA_PORT_C2_C3 1    // can use port pins port C2 and C3 as power supply of the Nunchuck
//comment this out if you are powering the wireless receiver directly from 5V supply
#include <AFMotor.h>
#include <Wire.h>
#include <utility/twi.h>
#include <EEPROM.h>
#include <Servo.h>
#include "Nunchuk.h"

Nunchuk chuk;


int i; //declare i as an integer
int j; //declare j as an integer
Servo myservo;
#define myservoc 1   //defines pin 1 as output for c button (0V or 5V output)

AF_DCMotor motor1(1, MOTOR12_64KHZ); //motor1 is connected to M1 port
AF_DCMotor motor2(2, MOTOR12_64KHZ); //motor2 is connected to M2 port
AF_DCMotor motor3(3, MOTOR34_64KHZ); //motor3 is connected to M3 port

void setup()
{
myservo.attach(1)//servo attach to pin 1 on arduino board(pin 1 going to servo headers on shield)
myservo.write 90//do not know what that is but I think that you must write it :D
pinMode(myservoc, OUTPUT); //tell arduino it is an output

//Serial.begin(115200);
Wire.begin();

// use 400 kHz for the nunchuck, because it doesn't work reliable with 100 kHz
TWBR = ((CPU_FREQ / 400000L) - 16) / 2;

// power supply of the Nunchuck via port C2 and C3 (for the wiichuck adapter directly plugged on Arduino)
#ifdef POWER_VIA_PORT_C2_C3
PORTC &=~ _BV(PORTC2);
PORTC |=  _BV(PORTC3);
DDRC |= _BV(PORTC2) | _BV(PORTC3);  // make outputs
delay(100);  // wait for things to stabilize
#endif

delay(100);
}

void loop()
{
if(!chuk.ok())
{
 //Serial.println("Initing nunchuk, it's broken!");
 chuk.init();
}

if(chuk.read())
{
 
 j = chuk.joyY() + 127; //joy scale is -127 to +127 so now converted to 0 - 255
 
 if (j<1) j=1;
 if (j>253) j=253;
 //Serial.print("Joy Y = ");
 //Serial.println(j);
 

if (chuk.buttonC() == 1 && chuk.buttonC() == 0) {
myservo.write(60)
delayMicroseconds(1500)
myservo.write(0)
delayMicroseconds(1000)
}
                               
if (chuk.joyY() >127.5 && chuk.joyY() <190,75 {
motor1.setSpeed(127,5);
motor2.setSpeed(127,5);
motor3.setSpeed(127,5);

motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
}

if (chuk.joyY() >190,75 && chuk.joyY() <=255
motor1.setSpeed(255);
motor2.setSpeed(255);
motor3.setSpeed(255);

motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
//add backward,left,right
}

                                  }

I think that there are some wrong parts in my code because I am pretty new at controlling robots wireless.
If I upload this code to my arduino uno I hope that the robot can move at two different speeds. I think that the arduino will understand that 127,5 is the center (no motors moving) and if  the joystick is between 127,5 to 190,75 the motors are moving forward(not so fast). And if the joystick is between 190,75 to 255 the motors are going forward (fast).
When I'm trying to verify the code this error comes:
Code: [Select]
Arduino:1.6.9 (Windows 7), Board:"Arduino/Genuino Uno"

WARNING: Spurious .github folder in 'Adafruit Motor Shield library' library
C:\Users\Isac\Desktop\programmering\arduino\sketcher text\Wireless_wii_nunchuck_controlled_arduino_robot\Wireless_wii_nunchuck_controlled_arduino_robot.ino:9:21: fatal error: Nunchuk.h: No such file or directory

#include "Nunchuk.h"

                   ^

compilation terminated.

exit status 1
Error compiling for board Arduino/Genuino Uno.This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.


So my question is how I can make this error disappear and fix the problems in my code.

PaulS

Code: [Select]
int i; //declare i as an integer
int j; //declare j as an integer

Really? Who'd have guessed that that was what that code was doing?

Quote
fatal error: Nunchuk.h: No such file or directory
Perhaps for someone that needs to comment an int statement this message is too obscure. I can't see how, though.

It means that you FAILED to download or install the library properly.
The art of getting good answers lies in asking good questions.

Sporerodas

Hi!
I can't find Nunchuck.h library. I have search on Nunchuck.h on "handle library" but the only thing I found was wiichuck library. Can someone please send me the Nunchuck.h library or a code when you use wiichuck library to control motors.

Go Up