Étape 3: Arduino Sketch
Basé sur le code de http://www.ideavirus.it/ adapté avec l’aide d’Oliver à Londres Hackspace. Comprend sont : wire.h, string.h, stdio.h et Servo.h - pour une raison quelconque Instructables drops eux quand j’ai coller mon code.#include
#include
#include
#include / / contrôles servo déclencheur seulement
Servo myservo ; créer objet servo servo déclencheur de contrôle
int pos = 90 ; variable pour stocker la position du servo déclencheur
uint8_t outbuf [6] ;
cnt int = 0 ;
int ledPin = 13 ;
int servoPin = 7 ;
int servoPin2 = 6 ;
int pulseWidth = 0 ;
int pulseWidth2 = 0 ;
long lastPulse = 0 ;
long lastPulse2 = 0 ;
int z_button = 0 ;
int c_button = 0 ;
int refreshTime = 70 ; peaufiné pour servos FutabaS3003
int minPulse = 600 ; peaufiné pour servos FutabaS3003
int minPulse2 = 600 ; peaufiné pour servos FutabaS3003
int maxPulse = 2400 ; position du servo maximale
int maxPuls2e = 2400 ; position du servo maximale
dtime int = 10 ;
#define pwbuffsize 10
long pwbuff [pwbuffsize] ;
long pwbuffpos = 0 ;
long pwbuff2 [pwbuffsize] ;
long pwbuffpos2 = 0 ;
void setup()
{
myservo.Attach(8) ; s’adapte sur le servo déclencheur sur la broche 8 l’objet de servo
Serial.Begin (9600) ;
Wire.Begin () ;
nunchuck_init () ;
pinMode (servoPin, sortie) ;
pinMode (servoPin2, sortie) ;
pulseWidth = minPulse ;
pulseWidth2 = minPulse2 ;
Serial.Print ("Finished setup\n") ;
}
Sub nunchuck_init()
{
Wire.beginTransmission (0 x 52) ;
Wire.Send (0 x 40) ;
Wire.Send (0 x 00) ;
Wire.endTransmission () ;
}
Sub send_zero()
{
Wire.beginTransmission (0 x 52) ;
Wire.Send (0 x 00) ;
Wire.endTransmission () ;
}
int t = 0 ;
void loop()
{
t ++ ;
Enfin = millis() ;
Si (t == 1) {}
t = 0 ;
Wire.requestFrom (0 x 52, 6) ;
tandis que (Wire.available ()) {}
outbuf [cnt] = nunchuk_decode_byte (Wire.receive ()) ;
digitalWrite (ledPin, HIGH) ;
CNT ++ ;
}
Si (cnt > = 5) {}
int z_button = 0 ;
int c_button = 0 ;
Si ((outbuf [5] >> 0) & 1)
z_button = 1 ;
Si ((outbuf [5] >> 1) & 1)
c_button = 1 ;
Switch (c_button) {}
cas 1 :
muovi (outbuf [3], outbuf [2]) ;
rupture ;
case 0 :
muovi (outbuf [1] / 2 + 0x3E, outbuf [0] / 2 + 0x3E) ;
rupture ;
}
Switch (z_button) {}
case 0 :
pour (pos = 90; pos > = 120; pos += 1) / / déclencheur est déclenché lorsque vous appuyez sur le bouton Z
{
myservo.Write(POS) ;
Delay(15) ;
}
pour (pos = 120; pos > = 90; pos-= 2) / / renvoie la gâchette vers le centre
{
myservo.Write(POS) ;
Delay(15) ;
}
rupture ;
cas 1 :
rupture ;
}
}
CNT = 0 ;
send_zero() ;
}
updateServo() ;
Delay(dtime) ;
}
Sub updateServo()
{
Si (millis() - lastPulse > = refreshTime) {}
digitalWrite (servoPin, HIGH) ;
delayMicroseconds(pulseWidth) ;
digitalWrite (servoPin, basse) ;
digitalWrite (servoPin2, HIGH) ;
delayMicroseconds(pulseWidth2) ;
digitalWrite (servoPin2, basse) ;
lastPulse = millis() ;
}
}
int i = 0 ;
Sub 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 ;
Si ((outbuf [5] >> 0) & 1)
z_button = 1 ;
Si ((outbuf [5] >> 1) & 1)
c_button = 1 ;
Si ((outbuf [5] >> 2) & 1)
accel_x_axis += 2 ;
Si ((outbuf [5] >> 3) & 1)
accel_x_axis += 1 ;
Si ((outbuf [5] >> 4) & 1)
accel_y_axis += 2 ;
Si ((outbuf [5] >> 5) & 1)
accel_y_axis += 1 ;
Si ((outbuf [5] >> 6) & 1)
accel_z_axis += 2 ;
Si ((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 ++ ;
}
nunchuk_decode_byte de char (char x)
{
x = (x ^ 0 x 17) + 0 x 17 ;
Return x ;
}
muovi Sub {(uint8_t, uint8_t XY)
flotteur tilt = (700 - x * 2 * 2) ;
flotteur tilt2 = (0x7E - y + 0x7E) * 2 * 2 ;
inclinaison = (inclinaison) ;
pulseWidth = (inclinaison * 5) + minPulse ;
Tilt2 = (tilt2-288) ;
pulseWidth2 = (tilt2 * 5) + minPulse2 ;
pwbuff [pwbuffpos] = pulseWidth ;
pwbuff2 [pwbuffpos2] = pulseWidth2 ;
Si (++ pwbuffpos == pwbuffsize) pwbuffpos = 0 ;
Si (++ pwbuffpos2 == pwbuffsize) pwbuffpos2 = 0 ;
pulseWidth = 0 ;
pulseWidth2 = 0 ;
pour (int p = 0; p pulseWidth += pwbuff [p] ;
pulseWidth2 += pwbuff2 [p] ;
}
pulseWidth / = pwbuffsize ;
pulseWidth2 / = pwbuffsize ;
}