Étape 1: Arduino Code
Commande de moteur servo ADXL345 accéléromètre 2
#include < Wire.h >
#include < Servo.h >
Servo myservox ;
Servo myservoy ;
#define périphérique (0 x 53)
#define TO_READ (6)
#define buff de déclencheur 16 octets [TO_READ] ;
int i ;
void setup()
{pinMode(TRIGGER,OUTPUT) ;
digitalWrite(TRIGGER,HIGH) ;
Wire.Begin() ;
myservox.Attach(9) ;
myservoy.Attach(10) ;
myservox.Write(90) ;
Delay(25) ;
myservoy.Write(90) ;
Delay(25) ;
writeTo (périphérique, 0x2D, 0) ;
writeTo (dispositif, 0x2D, 16) ;
writeTo (dispositif, 0x2D, 8);}
void loop()
{int regAddress = 0 x 32 ;
int x, y, z ; digitalWrite(TRIGGER,LOW) ;
Delay(10) ; digitalWrite(TRIGGER,HIGH) ;
readFrom (périphérique, regAddress, TO_READ, chamois) ;
x = 0 ; y = 0 ; z = 0 ;
pour (i = 1; i < = 5; i ++)
{x += (((int)buff[1]) << 8) | buff [0] ;
y += (((int)buff[3]) << 8) | Buff [2] ;
z += (((int)buff[5]) << 8) | Buff [4] ;
Delay(10) ;
}
x / = 5 ;
y / = 5 ;
z / = 5 ;
Si (x < -255) x = -255 ;
ElseIf (x > 255) x = 255 ;
Si (y < -255) y = -255 ;
ElseIf (y > 255) y = 255 ;
x = carte (x, -255, 255, 0, 180) ;
y = plan (y, -255, 255, 0, 180) ;
myservox.Write(x) ;
Delay(25) ;
myservoy.Write(180-y) ;
Delay(25) ;
Delay(200);}
Sub writeTo (int périphérique, l’adresse de l’octet, octet val)
{Wire.beginTransmission(device) ;
Wire.Write(Address) ; Wire.Write(Val) ;
Wire.endTransmission() ; }
Sub readFrom (int périphérique, adresse octets, int NB, buff[]) octets
{Wire.beginTransmission(device) ;
Wire.Write(Address) ;
Wire.endTransmission() ;
Wire.beginTransmission(device) ;
Wire.requestFrom (appareil, num) ;
int i = 0 ;
while(Wire.available())
{
Buff [i] = Wire.read() ; i ++ ;
}
Wire.endTransmission() ;
}