Étape 4: Arduino Code
Le code comporte quelques éléments non nécessaires qui sont négligeables !
#include / / bibliothèque Arduino Wire.h
#include / / bibliothèque Arduino string.h
#include / / Arduino bibliothèque Stdio.h
#include / / bibliothèque Arduino Servo.h
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 ledPin12 = 12 ;
int Z_trig ;
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 = 20 ; peaufiné pour servos High Tech
int minPulse = 300 ; peaufiné pour servos High Tech 3-11
int minPulse2 = 300 ; peaufiné pour servos High Tech 3-11
int maxPulse = 2500 ; position du servo maximale
int maxPuls2e = 2500 ; 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) ;
Serial.Begin (9600) ;
Wire.Begin () ;
nunchuck_init () ;
pinMode (servoPin, sortie) ;
pinMode (servoPin2, sortie) ;
pulseWidth = minPulse ;
pulseWidth2 = minPulse2 ;
Serial.Print ("Finished setup\n") ;
}
void nunchuck_init() / / Wii Nun-chuck handshaking
{
Wire.beginTransmission (0 x 52) ;
Wire.Send (0 x 40) ;
Wire.Send (0 x 00) ;
Wire.endTransmission () ;
}
void send_zero() / / Wii Nun-chuck handshaking
{
Wire.beginTransmission (0 x 52) ;
Wire.Send (0 x 00) ;
Wire.endTransmission () ;
}
int t = 0 ;
void loop()
{
pinMode (ledPin12, sortie) ; Jeux de broches 12 comme une sortie afin que le solénoïde peut être commandée par la touche Z du Nunchuck Wii
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 ; Initialise le Z à zéro
int c_button = 0 ; Initialise C à zéro
Si ((outbuf [5] >> 0) & 1) / / octet examine cinq de the-Nunchuck Wii pour la position du bouton Z
z_button = 1 ; séries Z à 1
Si ((outbuf [5] >> 1) & 1) / / octet examine cinq de the-Nunchuck Wii pour la position du bouton Z
c_button = 1 ; séries C 1
Switch (c_button) {/ / Case For C bouton État
cas 1 :
muovi (outbuf [1] / 2 + 0x3E, outbuf [0] / 2 + 0x3E) ; Active le contrôle Accelromiter pour Nunchuck Wii
rupture ;
case 0 :
muovi (outbuf [3], outbuf [2]) ; defalt contrôle Joystick pour nunchuck Wii
rupture ;
}
Switch (z_button) {/ / cas état de bouton pour Z
cas 1: {}
digitalWrite (ledPin12, basse) ; solénoïde de la valeur par défaut est désactivé
Z_trig = 0;}
rupture ;
case 0: {}
Si (Z_trig == 0) {}
digitalWrite (ledPin12, HIGH) ; Active l’électrovanne
retard (20) ;
digitalWrite (ledPin12, basse) ; désactive l’électrovanne si Z touche maintenue pour protéger contre la rupture
Z_trig = 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 ;
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 ;
}