Her er servoerne styret med thumbsticken:
youtu.be/DG1QlFWik9Y
Og her med accelrometeret:
youtu.be/WLUAryxeDOw
Man skal stadig manuelt skifte imellem det i koden. Det bliver opdateret så man kan bruge en af knapperne til det.
Den anden knap skal bruges til at slippe elektroner løs... Men det er ikke færdigt endnu...
Her er koden for dem der er nysgerrige, den er stadig lidt rodet og fyldt med egne noter og debug-output.
Kode: Vælg alt
/*
* WiiChuck for two servos
* Using thumbstick or accel.
* Bofh, 17-10-2016
*
*/
#include <Wire.h>
#include <nunchuck_funcs.h>
#include <Servo.h>
int loop_cnt=0;
byte accx,accy,zbut,cbut,joyx,joyy; //store byte from chuck
int ledPin = 13;
Servo myservox; // create servox object to control a servo on the horisontal axis
Servo myservoy; // create servoy object to control a servo on the vertical axis
int valx; //a var to move servox
int valy; //a var to move servoy
void setup()
{
Serial.begin(19200);
nunchuck_setpowerpins();
nunchuck_init(); // send the initilization handshake
Serial.print("WiiChuckDemo ready\n"); //Chuck ready?
delay(500);
//Get servos ready
myservox.attach(9); // attaches the servo on pin 9 to the servo object
myservoy.attach(10); // attaches the servo on pin 10 to the servo object
myservox.write(45); //set servox to 90
delay(10);
myservoy.write(90); //set servoy to 90
delay(10);
Serial.print("Servos ready\n");
delay(500);
}
void loop()
{
if( loop_cnt > 50 ) { // every X msecs get new data
loop_cnt = 0;
nunchuck_get_data();
accx = nunchuck_accelx(); // ranges from approx 70 - 182
accy = nunchuck_accely(); // ranges from approx 65 - 173
zbut = nunchuck_zbutton(); // 0 or 1
cbut = nunchuck_cbutton(); // 0 or 1
joyx = nunchuck_joyx(); // ranges from approx 31 to 234
joyy = nunchuck_joyy(); // ranges from approx 19 to 213
Serial.print("accx: "); Serial.print((byte)accx,DEC);
Serial.print("\taccy: "); Serial.print((byte)accy,DEC);
Serial.print("\tzbut: "); Serial.print((byte)zbut,DEC);
Serial.print("\tcbut: "); Serial.println((byte)cbut,DEC);
Serial.print("\tjoyx: "); Serial.println((byte)joyx,DEC);
Serial.print("\tjoyy: "); Serial.println((byte)joyy,DEC);
//valx = map(joyx, 31, 234, 4, 176); //make joyx values 2 servox values
valx = map(accx, 70, 182, 4, 176); //make acellx values 2 servox values
myservox.write(valx); //write to servox
//valy = map(joyy, 19, 212, 0, 123); //make joyy values 2 servoy values
valy = map(accy, 65, 173, 0, 123); //make accy values 2 servoy values
myservoy.write(valy); //write to servoy
}
loop_cnt++;
delay(1);
}
Nærbillede af gimballen: Der kan man også tydeligt se hvilke servoer jeg har brugt.