Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 16055

Switch between MPU 6050 and BT06 to control servos

$
0
0

I have two codes that work separately, but how do you get them to switch? I want the servo to use MPU6050 signals when BT06 is disconnected. I have attached the two codes i use.

#include <Wire.h>
#include <MPU6050.h>
#include <Servo.h>   

Servo myservo; // servo name
Servo sg90;          
Servo sg90two;          


int servo_pin = 3;
int servo_pin1 = 5;

MPU6050 sensor ;

int16_t ax, ay, az ;
int16_t gx, gy, gz ;

void setup ( ){ 

sg90.attach ( servo_pin );
sg90two.attach ( servo_pin1 );

Wire.begin ( );
Serial.begin  (9600); 
Serial.println  ( "Initializing the sensor" ); 
sensor.initialize ( ); 
Serial.println (sensor.testConnection ( ) ? "Successfully Connected" : "Connection failed"); 
delay (1000); 

Serial.println ( "Taking Values from the sensor" );
delay (200);


}


void loop ( ) 

{ 

sensor.getMotion6 (&ax, &ay, &az, &gx, &gy, &gz);

ax = map (ax, -15000, 7500, 0, 180) ;
ay = map (ay, -17000, 17000, 0, 180) ;

Serial.println (ax);

sg90.write (ax); 
sg90two.write (180-ax); 

delay (1000);



}

#include <SoftwareSerial.h> // TX RX software library for bluetooth

#include <Servo.h> // servo library 
Servo myservo; // servo name
Servo myservo1; // servo name

int bluetoothTx = 10; // bluetooth tx to 10 pin
int bluetoothRx = 11; // bluetooth rx to 11 pin

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
  myservo.attach(3); // attach servo signal wire to pin 3
  myservo1.attach(5); // attach servo signal wire to pin 5
  //Setup usb serial connection to computer
  Serial.begin(9600);

  //Setup Bluetooth serial connection to android
  bluetooth.begin(9600);
}

void loop()
{
  //Read from bluetooth and write to usb serial
  if(bluetooth.available()> 0 ) // receive number from bluetooth
  {
    int servopos = bluetooth.read(); // save the received number to servopos
    Serial.println(servopos); // serial print servopos current number received from bluetooth
    myservo.write(servopos); // roate the servo the angle received from the android app
    myservo1.write(180-servopos); // roate the servo the angle received from the android app
  }


}

3 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 16055

Latest Images

Trending Articles



Latest Images