Question about Wire and Serial

Hello

I have been sitting here for a couple of days now and coded, but i never got it to work so i "killed my darling" and begun to code from scratch to see where my fault was,
I am using an Arduino Leonardo to get vaues from 2 gyroscopes and get te X and Y values into my code to emulate a keyboard, i got it working yesterday. but when i should begin to code my sexond part of my program to do the sam but translate it into a joystick command the serialmonitor never started.

So i begin to write the code from the begining and runned my code bit by bit, and i saw when i added the wire code in void setup the serial monitor stopt working. does it comes with conflict with serial readings?

I know that i have lots of Serial.begin(); and Serial.end() calls but that was just a idea if it was a conflict in the beging so i want to be shur it only was used when i needed it

would be good if someone had experience of this and can point me in the right direction

#include<Wire.h>
#include<Keyboard.h>
const int MPU2=0x69,MPU1=0x68;
int16_t AcX1,AcY1,AcZ1,Tmp1,GyX1,GyY1,GyZ1;
int16_t AcX2,AcY2,AcZ2,Tmp2,GyX2,GyY2,GyZ2;

int GyroLx = GyX1;
int GyroLy = GyY1;
int GyroRx = GyX2;
int GyroRy = GyY2;


void setup() {

    pinMode(2, INPUT);
    pinMode(7, INPUT);
    digitalWrite(2, HIGH);
    digitalWrite(7, HIGH);
    Wire.begin(); 
    Wire.beginTransmission(MPU1);
    Wire.write(0x6B);// PWR_MGMT_1 register 
    Wire.write(0); // set to zero (wakes up the MPU-6050)
    Wire.endTransmission(true);Wire.begin(); 
    Wire.beginTransmission(MPU2);
    Wire.write(0x6B);// PWR_MGMT_1 register 
    Wire.write(0); // set to zero (wakes up the MPU-6050)
    Wire.endTransmission(true);


}

void loop() {
  //Serial.println("Testing serial data");
  if (digitalRead(2))
    {
    while(digitalRead(2)) //Do this while the pin 2 is HIGH
      {
      getXY();
      Serial.begin(2400);
      Serial.println("Keyboard mode active");
      Keyboard.begin();      
      Serial.print("value of GyX1 is: ");
      Serial.println(GyroLx);
      Serial.print("value of GyX2 is: ");
      Serial.println(GyroRx);
      Serial.end();
      delay(250); 
      } 
    }
  else if (digitalRead(7))
    {
    while(digitalRead(7)) //Do this while the pin 2 is HIGH
      {
      Serial.begin(2400);
      Serial.println("Joystick mode active");
      Serial.end();
      delay(250);
      }
    }
  else
    {
    Serial.begin(2400);
    Serial.println("No mode is active");
    Serial.end();
    delay(250);
    }
}

void getXY(){
    GetMpuValue1(MPU1);
    GetMpuValue2(MPU2);
    GyroLx = GyX1;
    GyroRx = GyX2;
    GyroLy = GyY2;
    GyroRy = GyY2;
  
  }
void GetMpuValue1(const int MPU){ 
   
      Wire.beginTransmission(MPU); 
      Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) 
      Wire.endTransmission(false);
      Wire.requestFrom(MPU, 14, true); // request a total of 14 registers 
      AcX1=Wire.read()<<8| Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L) 
      AcY1=Wire.read()<<8|  Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
      AcZ1=Wire.read()<<8| Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L) 
      Tmp1=Wire.read()<<8| Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L) 
      GyX1=Wire.read()<<8| Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L) 
      GyY1=Wire.read()<<8| Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L) 
      GyZ1=Wire.read()<<8| Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L) 
  }
void GetMpuValue2(const int MPU){ 
   
      Wire.beginTransmission(MPU); 
      Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) 
      Wire.endTransmission(false);
      Wire.requestFrom(MPU, 14, true); // request a total of 14 registers 
      AcX2=Wire.read()<<8| Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L) 
      AcY2=Wire.read()<<8|  Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
      AcZ2=Wire.read()<<8| Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L) 
      Tmp2=Wire.read()<<8| Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L) 
      GyX2=Wire.read()<<8| Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L) 
      GyY2=Wire.read()<<8| Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L) 
      GyZ2=Wire.read()<<8| Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
  }

Why do you keep opening and closing the serial stream ?

I already have answerd on that question

but i have solved my problem. It was one of my cables that were connected on pin 2 that interfered with my SDA pin that also goes to pin 2 so i just changet pin 2 to pin 3

This is not your issue, but note that statements like this are not guaranteed to work the way you want them to. The first Wire.read() could be executed after the second Wire.read() (the C++ compiler is allowed to do stuff like that), and the order that you read data will be backwards.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.