Arduino code 1

From Control Systems Technology Group
Jump to navigation Jump to search
#include <Wire.h>
#include <MPU6050.h>
#include <Servo.h>

Servo sg90_x;
Servo sg90_y;

int servo_x = 2;
int servo_y = 10;
MPU6050 sensor ;
int16_t ax, ay, az ;
int16_t gx, gy, gz ;

double Setpoint;

void setup ( )
{
  Setpoint = 90;
  sg90_x.attach (servo_x);
  sg90_y.attach (servo_y);
  sg90_x.write(Setpoint);
  sg90_y.write(Setpoint);
  Wire.begin( );
  Serial.begin  (9600);

  Serial.println  ( "Initializing the sensor" );
  sensor.initialize ( );
  Serial.println (sensor.testConnection ( ) ? "Successfully Connected" : "Connection failed");
  delay (100);
}

void loop ( )

{
  sensor.getMotion6 (&ax, &ay, &az, &gx, &gy, &gz);
  x_out = map(ax, -17000, 17000, 180, 0); //mapped in reverse order to get a counteractive movement. 
  y)out = map(ay, -17000, 17000, 180, 0);


sg90_x.write(x_out);
sg90_y.write(y_out);