Stap 3: Arduino Sketch
Op basis van de code van http://www.ideavirus.it/ met hulp van Oliver op Londen Hackspace aangepast. Omvat zijn: wire.h, string.h, stdio.h en Servo.h - om wat voor reden Instructables drops hen wanneer ik plak mijn code.#include
#include
#include
#include / / regelt alleen de trigger-servo
Servo myservo; servo-object om te bepalen van trigger servo maken
int pos = 90; variabele op te slaan van de positie van de servo trigger
uint8_t outbuf [6];
int cnt = 0;
int ledPin = 13;
int servoPin = 7;
int servoPin2 = 6;
int pulseWidth = 0;
int pulseWidth2 = 0;
lange lastPulse = 0;
lange lastPulse2 = 0;
int z_button = 0;
int c_button = 0;
int refreshTime = 70; getweaked voor FutabaS3003 servo 's
int minPulse = 600; getweaked voor FutabaS3003 servo 's
int minPulse2 = 600; getweaked voor FutabaS3003 servo 's
int maxPulse = 2400; maximale servo positie
int maxPuls2e = 2400; maximale servo positie
int rekenwijze = 10;
#define pwbuffsize 10
lange pwbuff [pwbuffsize];
lange pwbuffpos = 0;
lange pwbuff2 [pwbuffsize];
lange pwbuffpos2 = 0;
VOID Setup
{
myservo.attach(8); de trigger servo op pin 8 hecht aan de servo-object
Serial.begin (9600);
Wire.begin ();
nunchuck_init ();
pinMode (servoPin, OUTPUT);
pinMode (servoPin2, OUTPUT);
pulseWidth = minPulse;
pulseWidth2 = minPulse2;
Serial.Print ("afgewerkte setup\n");
}
VOID nunchuck_init()
{
Wire.beginTransmission (0x52);
Wire.send (0x40);
Wire.send (0x00);
Wire.endTransmission ();
}
VOID send_zero()
{
Wire.beginTransmission (0x52);
Wire.send (0x00);
Wire.endTransmission ();
}
int t = 0;
void loop
{
t ++;
eindelijk = millis();
Als (t == 1) {}
t = 0;
Wire.requestFrom (0x52, 6);
terwijl (Wire.available ()) {}
outbuf [cnt] = nunchuk_decode_byte (Wire.receive ());
digitalWrite (ledPin, hoge);
CNT ++;
}
Als (cnt > = 5) {}
int z_button = 0;
int c_button = 0;
Als ((outbuf [5] >> 0) & 1)
z_button = 1;
Als ((outbuf [5] >> 1) & 1)
c_button = 1;
schakelaar (c_button) {}
Case 1:
muovi (outbuf [3], outbuf [2]);
breken;
Case 0:
muovi (outbuf [1] / 2 + 0x3E, outbuf [0] / 2 + 0x3E);
breken;
}
schakelaar (z_button) {}
Case 0:
voor (pos = 90; pos > = 120; pos += 1) / / de trigger wordt gestart als Z-knop ingedrukt
{
myservo.write(POS);
delay(15);
}
voor (pos = 120; pos > = 90; pos-= 2) / / retourneert trigger wilt centreren
{
myservo.write(POS);
delay(15);
}
breken;
Case 1:
breken;
}
}
CNT = 0;
send_zero();
}
updateServo();
delay(DTIME);
}
VOID updateServo()
{
Als (millis() - lastPulse > = refreshTime) {}
digitalWrite (servoPin, hoge);
delayMicroseconds(pulseWidth);
digitalWrite (servoPin, laag);
digitalWrite (servoPin2, hoge);
delayMicroseconds(pulseWidth2);
digitalWrite (servoPin2, laag);
lastPulse = millis();
}
}
int i = 0;
VOID printNunchuckData()
{
int joy_x_axis = outbuf [0];
int joy_y_axis = outbuf [1];
int accel_x_axis = outbuf [2];
int accel_y_axis = outbuf [3];
int accel_z_axis = outbuf [4];
int z_button = 0;
int c_button = 0;
Als ((outbuf [5] >> 0) & 1)
z_button = 1;
Als ((outbuf [5] >> 1) & 1)
c_button = 1;
Als ((outbuf [5] >> 2) & 1)
accel_x_axis += 2;
Als ((outbuf [5] >> 3) & 1)
accel_x_axis += 1;
Als ((outbuf [5] >> 4) & 1)
accel_y_axis += 2;
Als ((outbuf [5] >> 5) & 1)
accel_y_axis += 1;
Als ((outbuf [5] >> 6) & 1)
accel_z_axis += 2;
Als ((outbuf [5] >> 7) & 1)
accel_z_axis += 1;
Serial.Print (i, DEC);
Serial.Print ("\t");
Serial.Print ("X:");
Serial.Print (joy_x_axis, DEC);
Serial.Print ("\t");
Serial.Print ("Y:");
Serial.Print (joy_y_axis, DEC);
Serial.Print ("\t");
Serial.Print ("AccX:");
Serial.Print (accel_x_axis, DEC);
Serial.Print ("\t");
Serial.Print ("AccY:");
Serial.Print (accel_y_axis, DEC);
Serial.Print ("\t");
Serial.Print ("AccZ:");
Serial.Print (accel_z_axis, DEC);
Serial.Print ("\t");
Serial.Print (z_button, DEC);
Serial.Print ("");
Serial.Print (c_button, DEC);
Serial.Print ("\r\n");
i ++;
}
char nunchuk_decode_byte (char x)
{
x = (x ^ 0x17) + 0x17;
return x;
}
VOID muovi (uint8_t x, uint8_t y) {}
zweven tilt = (700 - x * 2 * 2);
tilt2 zweven = (0x7E - y + 0x7E) * 2 * 2;
kantelen (tilt) =;
pulseWidth = (kantelen * 5) + minPulse;
Tilt2 = (tilt2-288);
pulseWidth2 = (tilt2 * 5) + minPulse2;
pwbuff [pwbuffpos] = pulseWidth;
pwbuff2 [pwbuffpos2] = pulseWidth2;
Als (++ pwbuffpos == pwbuffsize) pwbuffpos = 0;
Als (++ pwbuffpos2 == pwbuffsize) pwbuffpos2 = 0;
pulseWidth = 0;
pulseWidth2 = 0;
voor (int p = 0; p pulseWidth += pwbuff [p];
pulseWidth2 += pwbuff2 [p];
}
pulseWidth / = pwbuffsize;
pulseWidth2 / = pwbuffsize;
}