Skip to content

Instantly share code, notes, and snippets.

@mohamedbark
Created October 20, 2018 11:12
Show Gist options
  • Save mohamedbark/22cb598188bbab60093cf6ba1c2f91f4 to your computer and use it in GitHub Desktop.
Save mohamedbark/22cb598188bbab60093cf6ba1c2f91f4 to your computer and use it in GitHub Desktop.
Master Arduino code for bluetooth and gyroscope
#include <Wire.h>
#include <MPU6050.h>
#include <Servo.h>
//---------------------
int Pot = A1;
int value;
//---------------------------
Servo myservo; // create servo object to control a servo
MPU6050 mpu;
float timeStep = 0.01;
float yaw = 0;
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
{
Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
delay(500);
}
// Calibrate gyroscope. The calibration must be at rest.
// If you don't want calibrate, comment this line.
mpu.calibrateGyro();
// Set threshold sensivty. Default 3.
// If you don't want use threshold, comment this line or set 0.
mpu.setThreshold(5);
// pinMode(A0 , INPUT);
}
int hamoksha=0;
void loop()
{
Vector norm = mpu.readNormalizeGyro();
yaw = yaw + norm.ZAxis *timeStep;
hamoksha=map(yaw,-80,80,0,180);
hamoksha=constrain(hamoksha,0,180);
myservo.write(hamoksha);
//----------------------------------
value = analogRead(Pot);
value=map(value,0,1023,0,255);
//---------------------------
Serial.write('a');
delay(100);
Serial.write(value);
Serial.write('a');
delay(100);
Serial.write(hamoksha);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment