CODIGOS BIMESTRE 4

 BLUETOOTH

Código #1

#include <SoftwareSerial.h>   // libreria comunicación serie

SoftwareSerial BT(10,11);   // pin 10 como RX, pin 11 cómo TX


void setup(){

  Serial.begin(9600);        // comunicación de monitor serial a 9600 bps

  Serial.println("Listo");  // escribe Listo en el monitor

  BT.begin(38400);         // comunicación serie entre Arduino y el módulo a 38400 bps

}


void loop(){

if (BT.available())                // si hay información disponible desde módulo

   Serial.write(BT.read());   // lee Bluetooth y envía a monitor serial de Arduino

if (Serial.available())              // si hay información disponible desde el monitor serial

   BT.write(Serial.read());   // lee monitor serial y envía a Bluetooth

}


Código #2

#include <SoftwareSerial.h> // libreria que permite establecer pines digitales


SoftwareSerial BT(3, 4);  // pin 3 como RX, pin 4 como TX


char valor = 0;      // variable para almacenar caracter recibido

int _1A = 11 ;

int _2A = 10;

int _3A = 9;

int _4A = 8;


void setup(){

  BT.begin(9600);    // comunicacion serie entre Arduino y el modulo

  Serial.begin(115200);

pinMode(_1A, OUTPUT);

pinMode(_2A, OUTPUT);

pinMode(_3A, OUTPUT);

pinMode(_4A, OUTPUT);


}


void loop(){

  

if (BT.available()){     // si hay informacion disponible desde modulo

  valor = BT.read();   // almacena en valor el caracter recibido desde modulo


  if( valor == 'a' )   // adelante

  {

      digitalWrite(_1A,HIGH); 

      digitalWrite(_2A,LOW);

      digitalWrite(_3A,HIGH); 

      digitalWrite(_4A,LOW);

      Serial.println("adelante");

  }

      

  if( valor == 'b' )   // atras

   {

      digitalWrite(_1A,LOW); 

      digitalWrite(_2A,HIGH);

      digitalWrite(_3A,LOW); 

      digitalWrite(_4A,HIGH);

      Serial.println("atras");

   }

      

  if( valor == 'c' )   //derecha

    {

      digitalWrite(_1A,HIGH); 

      digitalWrite(_2A,LOW);

      digitalWrite(_3A,LOW); 

      digitalWrite(_4A,HIGH);

      Serial.println("derecha");

      }

      

  if( valor == 'd' )   //izquierda

     

    {

      digitalWrite(_1A,LOW); 

      digitalWrite(_2A,HIGH);

      digitalWrite(_3A,HIGH); 

      digitalWrite(_4A,LOW);

      Serial.println("izquierda");

    }


      if( valor == 'e' )   //stop

     

    {

      digitalWrite(_1A,LOW); 

      digitalWrite(_2A,LOW);

      digitalWrite(_3A,LOW); 

      digitalWrite(_4A,LOW);

      Serial.println("stop");

    }

}

}


 MPU6050

Código #1 LEER VALORES RAW

//GND - GND
//VCC - VCC
//SDA - Pin A4
//SCL - Pin A5

#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"

const int mpuAddress = 0x68;  //Puede ser 0x68 o 0x69
MPU6050 mpu(mpuAddress);

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

void printTab()
{
   Serial.print(F("\t"));
}

void printRAW()
{
   Serial.print(F("a[x y z] g[x y z]:t"));
   Serial.print(ax); printTab();
   Serial.print(ay); printTab();
   Serial.print(az); printTab();
   Serial.print(gx); printTab();
   Serial.print(gy); printTab();
   Serial.println(gz);
}

void setup()
{
   Serial.begin(9600);
   Wire.begin();
   mpu.initialize();
   Serial.println(mpu.testConnection() ? F("IMU iniciado correctamente") : F("Error al iniciar IMU"));
}

void loop()
{
   // Leer las aceleraciones y velocidades angulares
   mpu.getAcceleration(&ax, &ay, &az);
   mpu.getRotation(&gx, &gy, &gz);

   printRAW();
   
   delay(100);
}

//GND - GND

//VCC - VCC

//SDA - Pin A4

//SCL - Pin A5


#include "I2Cdev.h"

#include "MPU6050.h"

#include "Wire.h"


const int mpuAddress = 0x68;  //Puede ser 0x68 o 0x69

MPU6050 mpu(mpuAddress);


int ax, ay, az;

int gx, gy, gz;


void printTab()

{

   Serial.print(F("\t"));

}


void printRAW()

{

   Serial.print(F("a[x y z] g[x y z]:t"));

   Serial.print(ax); printTab();

   Serial.print(ay); printTab();

   Serial.print(az); printTab();

   Serial.print(gx); printTab();

   Serial.print(gy); printTab();

   Serial.println(gz);

}


void setup()

{

   Serial.begin(9600);

   Wire.begin();

   mpu.initialize();

   Serial.println(mpu.testConnection() ? F("IMU iniciado correctamente") : F("Error al iniciar IMU"));

}


void loop()

{

   // Leer las aceleraciones y velocidades angulares

   mpu.getAcceleration(&ax, &ay, &az);

   mpu.getRotation(&gx, &gy, &gz);


   printRAW();

   

   delay(100);

}


 MPU6050

Código #2 LEER INCLINACIÓN CON ACELERÓMETRO

//GND - GND

//VCC - VCC

//SDA - Pin A4

//SCL - Pin A5


#include "I2Cdev.h"

#include "MPU6050.h"

#include "Wire.h"


const int mpuAddress = 0x68;  // Puede ser 0x68 o 0x69

MPU6050 mpu(mpuAddress);


int ax, ay, az;

int gx, gy, gz;


void setup()

{

   Serial.begin(9600);

   Wire.begin();

   mpu.initialize();

   Serial.println(mpu.testConnection() ? F("IMU iniciado correctamente") : F("Error al iniciar IMU"));

}


void loop() 

{

   // Leer las aceleraciones 

   mpu.getAcceleration(&ax, &ay, &az);


   //Calcular los angulos de inclinacion

   float accel_ang_x = atan(ax / sqrt(pow(ay, 2) + pow(az, 2)))*(180.0 / 3.14);

   float accel_ang_y = atan(ay / sqrt(pow(ax, 2) + pow(az, 2)))*(180.0 / 3.14);


   // Mostrar resultados

   Serial.print(F("Inclinacion en X: "));

   Serial.print(accel_ang_x);

   Serial.print(F("\tInclinacion en Y:"));

   Serial.println(accel_ang_y);

   delay(10);

}

 MPU6050

Código #3 OBTENER LA ORIENTACIÓN MEDIANTE EL DMP


//GND - GND

//VCC - VCC

//SDA - Pin A4

//SCL - Pin A5

//INT - Pin 2


#include "I2Cdev.h"

#include "MPU6050_6Axis_MotionApps20.h"

#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE

    #include "Wire.h"

#endif


// class default I2C address is 0x68

// specific I2C addresses may be passed as a parameter here

// AD0 low = 0x68

// AD0 high = 0x69

MPU6050 mpu;

//MPU6050 mpu(0x69); // <-- use for AD0 high



#define INTERRUPT_PIN 2

#define LED_PIN 13

bool blinkState = false;


// MPU control/status vars

bool dmpReady = false;  // set true if DMP init was successful

uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU

uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)

uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)

uint16_t fifoCount;     // count of all bytes currently in FIFO

uint8_t fifoBuffer[64]; // FIFO storage buffer


Quaternion q;           // [w, x, y, z]

VectorInt16 aa;         // [x, y, z]

VectorInt16 aaReal;     // [x, y, z]

VectorInt16 aaWorld;    // [x, y, z]

VectorFloat gravity;    // [x, y, z]

float ypr[3];           // [yaw, pitch, roll]


volatile bool mpuInterrupt = false;

void dmpDataReady() {

    mpuInterrupt = true;

}


void setup() {

    // join I2C bus (I2Cdev library doesn't do this automatically)

    #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE

        Wire.begin();

        Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties

    #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE

        Fastwire::setup(400, true);

    #endif


    Serial.begin(9600);


    // Iniciar MPU6050

    Serial.println(F("Initializing I2C devices..."));

    mpu.initialize();

    pinMode(INTERRUPT_PIN, INPUT);


    // Comprobar  conexion

    Serial.println(F("Testing device connections..."));

    Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));


    // Iniciar DMP

    Serial.println(F("Initializing DMP..."));

    devStatus = mpu.dmpInitialize();


    // Valores de calibracion

    mpu.setXGyroOffset(220);

    mpu.setYGyroOffset(76);

    mpu.setZGyroOffset(-85);

    mpu.setZAccelOffset(1688);


    // Activar DMP

    if (devStatus == 0) {

        Serial.println(F("Enabling DMP..."));

        mpu.setDMPEnabled(true);


        // Activar interrupcion

        attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), dmpDataReady, RISING);

        mpuIntStatus = mpu.getIntStatus();


        Serial.println(F("DMP ready! Waiting for first interrupt..."));

        dmpReady = true;


        // get expected DMP packet size for later comparison

        packetSize = mpu.dmpGetFIFOPacketSize();

    } else {

        // ERROR!

        // 1 = initial memory load failed

        // 2 = DMP configuration updates failed

        // (if it's going to break, usually the code will be 1)

        Serial.print(F("DMP Initialization failed (code "));

        Serial.print(devStatus);

        Serial.println(F(")"));

    }

}



void loop() {

    // Si fallo al iniciar, parar programa

    if (!dmpReady) return;


    // Ejecutar mientras no hay interrupcion

    while (!mpuInterrupt && fifoCount < packetSize) {

        // AQUI EL RESTO DEL CODIGO DE TU PROGRRAMA

    }


    mpuInterrupt = false;

    mpuIntStatus = mpu.getIntStatus();


    // Obtener datos del FIFO

    fifoCount = mpu.getFIFOCount();


    // Controlar overflow

    if ((mpuIntStatus & 0x10) || fifoCount == 1024) {

        mpu.resetFIFO();

        Serial.println(F("FIFO overflow!"));

    } 

    else if (mpuIntStatus & 0x02) {

        // wait for correct available data length, should be a VERY short wait

        while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();


        // read a packet from FIFO

        mpu.getFIFOBytes(fifoBuffer, packetSize);

        

        // track FIFO count here in case there is > 1 packet available

        // (this lets us immediately read more without waiting for an interrupt)

        fifoCount -= packetSize;


   // MMostrar Yaw, Pitch, Roll

   mpu.dmpGetQuaternion(&q, fifoBuffer);

   mpu.dmpGetGravity(&gravity, &q);

   mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);

   Serial.print("ypr\t");

   Serial.print(ypr[0] * 180/M_PI);

   Serial.print("\t");

   Serial.print(ypr[1] * 180/M_PI);

   Serial.print("\t");

   Serial.println(ypr[2] * 180/M_PI);

   

   // Mostrar aceleracion

   mpu.dmpGetQuaternion(&q, fifoBuffer);

   mpu.dmpGetAccel(&aa, fifoBuffer);

   mpu.dmpGetGravity(&gravity, &q);

   mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);

   Serial.print("areal\t");

   Serial.print(aaReal.x);

   Serial.print("\t");

   Serial.print(aaReal.y);

   Serial.print("\t");

   Serial.println(aaReal.z);

    }

}











































//GND - GND
//VCC - VCC
//SDA - Pin A4
//SCL - Pin A5

#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"

const int mpuAddress = 0x68;  //Puede ser 0x68 o 0x69
MPU6050 mpu(mpuAddress);

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

void printTab()
{
   Serial.print(F("\t"));
}

void printRAW()
{
   Serial.print(F("a[x y z] g[x y z]:t"));
   Serial.print(ax); printTab();
   Serial.print(ay); printTab();
   Serial.print(az); printTab();
   Serial.print(gx); printTab();
   Serial.print(gy); printTab();
   Serial.println(gz);
}

void setup()
{
   Serial.begin(9600);
   Wire.begin();
   mpu.initialize();
   Serial.println(mpu.testConnection() ? F("IMU iniciado correctamente") : F("Error al iniciar IMU"));
}

void loop()
{
   // Leer las aceleraciones y velocidades angulares
   mpu.getAcceleration(&ax, &ay, &az);
   mpu.getRotation(&gx, &gy, &gz);

   printRAW();
   
   delay(100);
}

Comentarios

Entradas populares