Arduino / Wii kontrollerade Lego Star Wars raket torn (3 / 3 steg)
Steg 3: Arduino skiss
Baserat på kod från http://www.ideavirus.it/ anpassas med hjälp av Oliver på London Hackspace. Innehåller är: wire.h, string.h, stdio.h och Servo.h - av någon anledning Instructables droppar dem när jag klistrar in min kod.#include
#include
#include
#include / / kontroller trigger servo bara
Servo myservo; skapa objekt för att styra trigger servo servo
int pos = 90. variabel att lagra trigger servo position
uint8_t outbuf [6].
int cnt = 0;
int ledPin = 13.
int servoPin = 7.
int servoPin2 = 6;
int pulseWidth = 0;
int pulseWidth2 = 0;
lång lastPulse = 0;
lång lastPulse2 = 0;
int z_button = 0;
int c_button = 0;
int refreshTime = 70. tweaked för FutabaS3003 servon
int minPulse = 600; tweaked för FutabaS3003 servon
int minPulse2 = 600; tweaked för FutabaS3003 servon
int maxPulse = 2400; maximal servo position
int maxPuls2e = 2400; maximal servo position
int dtime = 10;
#define pwbuffsize 10
lång pwbuff [pwbuffsize];
lång pwbuffpos = 0;
lång pwbuff2 [pwbuffsize];
lång pwbuffpos2 = 0;
void setup()
{
myservo.attach(8); tillmäter objektet servo servo utlösare på stift 8
Serial.BEGIN (9600);
Wire.BEGIN ();
nunchuck_init ();
pinMode (servoPin, produktionen);
pinMode (servoPin2, produktionen);
pulseWidth = minPulse;
pulseWidth2 = minPulse2;
Serial.Print ("färdiga 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 ++;
äntligen = millis();
om (t == 1) {
t = 0;
Wire.requestFrom (0x52, 6);
medan (Wire.available ()) {
outbuf [cnt] = nunchuk_decode_byte (Wire.receive ());
digitalWrite (ledPin, hög);
CNT ++;
}
om (cnt > = 5) {
int z_button = 0;
int c_button = 0;
om ((outbuf [5] >> 0) & 1)
z_button = 1;
om ((outbuf [5] >> 1) & 1)
c_button = 1;
växel (c_button) {
fall 1:
muovi (outbuf [3], outbuf [2]);
bryta;
fall 0:
muovi (outbuf [1] / 2 + 0x3E, outbuf [0] / 2 + 0x3E);
bryta;
}
växel (z_button) {
fall 0:
för (pos = 90; pos > = 120; pos + = 1) / / bränder utlösare när du trycker på Z-knappen
{
myservo.write(POS);
Delay(15);
}
för (pos = 120; pos > = 90; pos-= 2) / / returnerar trigger till mitten
{
myservo.write(POS);
Delay(15);
}
bryta;
fall 1:
bryta;
}
}
CNT = 0;
send_zero();
}
updateServo();
Delay(dtime);
}
void updateServo()
{
om (millis() - lastPulse > = refreshTime) {
digitalWrite (servoPin, hög);
delayMicroseconds(pulseWidth);
digitalWrite (servoPin, låg);
digitalWrite (servoPin2, hög);
delayMicroseconds(pulseWidth2);
digitalWrite (servoPin2, låg);
lastPulse = millis();
}
}
int jag = 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;
om ((outbuf [5] >> 0) & 1)
z_button = 1;
om ((outbuf [5] >> 1) & 1)
c_button = 1;
om ((outbuf [5] >> 2) & 1)
accel_x_axis += 2.
om ((outbuf [5] >> 3) & 1)
accel_x_axis + = 1;
om ((outbuf [5] >> 4) & 1)
accel_y_axis += 2.
om ((outbuf [5] >> 5) & 1)
accel_y_axis + = 1;
om ((outbuf [5] >> 6) & 1)
accel_z_axis += 2.
om ((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;
återvändande x;
}
void muovi (uint8_t x, uint8_t y) {
flyta tilt = (700 - x * 2 * 2);
flyta tilt2 = (0x7E - y + 0x7E) * 2 * 2.
lutning = (lutning);
pulseWidth = (luta * 5) + minPulse;
Tilt2 = (tilt2-288);
pulseWidth2 = (tilt2 * 5) + minPulse2;
pwbuff [pwbuffpos] = pulseWidth;
pwbuff2 [pwbuffpos2] = pulseWidth2;
om (++ pwbuffpos == pwbuffsize) pwbuffpos = 0;
om (++ pwbuffpos2 == pwbuffsize) pwbuffpos2 = 0;
pulseWidth = 0;
pulseWidth2 = 0;
för (int p = 0, p pulseWidth += pwbuff [p];
pulseWidth2 += pwbuff2 [p];
}
pulseWidth = pwbuffsize;
pulseWidth2 = pwbuffsize;
}