Hi, im currently on a project and I need to connect 2 MPU6050 to one Arduino and after, create a pong with it on a tv. I use this code to connect 2 MPU6050
#include "Wire.h" // Arduino Wire library
#include "I2Cdev.h" //Installer ces 2 librairies
#include "MPU6050.h"
#include "math.h"
// AD0 low = 0x68 (default for InvenSense evaluation board)
// AD0 high = 0x69
MPU6050 accelgyro1(0x68);
MPU6050 accelgyro2(0x69);
int16_t ax1, ay1, az1; //mesures brutes capteur 1
int16_t gx1, gy1, gz1;
int16_t ax2, ay2, az2; //mesures brutes capteur 2
int16_t gx2, gy2, gz2;
float angle1 = 0;
float angle2 = 0;
const int LED = 3;
const int LED1 = 4;
const int LED2 = 5;
const int LED3 = 6;
void setup() {
Wire.begin(); //I2C bus
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB (LEONARDO)
}
// initialize device
Serial.println("Initialisation I2C...");
accelgyro1.initialize();
accelgyro2.initialize();
// verify connection
Serial.println("Test de la conection du dispositif ...");
Serial.println(accelgyro1.testConnection() ? "MPU6050 n°1 connection reussie" : "MPU6050 n°1 connection echec");
Serial.println(accelgyro2.testConnection() ? "MPU6050 n°2 connection reussie" : "MPU6050 n°2 connection echec");
pinMode(LED, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
delay(1000);
}
void loop() {
accelgyro1.getMotion6(&ax1, &ay1, &az1, &gx1, &gy1, &gz1);
angle1 = 0.98 * (angle1 + float(gy1) * 0.01 / 131) + 0.02 * atan2((double)ax1, (double)az1) * 180 / PI;
accelgyro2.getMotion6(&ax2, &ay2, &az2, &gx2, &gy2, &gz2);
angle2 = 0.98 * (angle2 + float(gy2) * 0.01 / 131) + 0.02 * atan2((double)ax2, (double)az2) * 180 / PI;
Serial.print(angle1);
Serial.print("\t");
Serial.println(angle2);
delay(10);
if (angle1 <= 0)
{
digitalWrite(LED, HIGH);
}
else{
digitalWrite(LED, LOW);
}
if (angle1 >= 0)
{
digitalWrite(LED1, HIGH);
}
else{
digitalWrite(LED1, LOW);
}
if (angle2 <= 0)
{
digitalWrite(LED2, HIGH);
}
else{
digitalWrite(LED2, LOW);
}
if (angle2 >= 0)
{
digitalWrite(LED3, HIGH);
}
else{
digitalWrite(LED3, LOW);
}
}
And this one for the pong (currently with potentiometer), but when I try to combine the 2, the TV output is dead. Especially when I add the line "Wire.begin()"
#include <Arduino.h>
#include <TVout.h>
#include <fontALL.h>
#include <ezButton.h>
TVout TV;
const int screenWidth = 128;
const int screenHeight = 96;
const int paddleHeight = 16;
int ligne;
int buttonPin;
int p1y, p2y; // Stores "last" position
int p1s, p2s; // Scores
int ballX, ballY, ballDx, ballDy; // Stores the balls position
void drawMenu();
void drawPaddles();
void drawBall();
void drawField();
void resetGame();
void resetBall();
void drawCadre();
ezButton button(2);
void setup() {
TV.begin(_NTSC);
TV.clear_screen();
TV.select_font(font8x8);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(6, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
randomSeed(analogRead(2));
button.setDebounceTime(50);
pinMode(2, INPUT); digitalWrite(2, HIGH);
drawMenu();
while (!button.isPressed())
button.loop();
decompte();
resetGame();
}
void loop() {
drawField();
drawBall();
drawPaddles();
drawCadre();
if (ballX < 0) { p2s++; resetBall(); }
if (ballX > screenWidth) { p1s++; resetBall(); }
if (ballY <= 0 || ballY >= screenHeight) { ballDy *= -1; }
// Player 1 collision detection
if (ballX == 4 && ballY > p1y && ballY < p1y + paddleHeight) {
ballDx *= -1;
}
if (ballX == screenWidth - 4 && ballY > p2y && ballY < p2y + paddleHeight) {
ballDx *= -1;
}
// Check for game over
if (p1s >= 10 || p2s >= 10) {
TV.clear_screen();
TV.set_cursor(40, 35);
TV.print("Vainqueur :");
TV.set_cursor(42, 45);
TV.print(p1s > p2s ? "Team 1" : "Team 2");
delay(5000);
drawMenu();
while (digitalRead(2)) { }
decompte();
resetGame();
}
/*if (digitalRead(6) == LOW) {
TV.set_cursor(10, 20 + 8);
TV.print("Button 1 down");
}
if (digitalRead(5) == LOW) {
TV.set_cursor(10, 20 + 8 + 8);
TV.print("Button 2 down");
}*/
delay(25);
}
void resetGame() {
TV.clear_screen();
p1s = 0;
p2s = 0;
resetBall();
}
void resetBall() {
ballX = screenWidth / 2;
ballY = screenHeight / 2;
ballDx = random(2) == 1 ? 1 : -1;
ballDy = random(2) == 1 ? 1 : -1;
}
void drawField() {
// Center line
TV.draw_rect(screenWidth / 2, 0, 1, screenHeight, 1, 1);
// Scores
TV.set_cursor(screenWidth / 2 - 2 * 8, 3);
TV.print(p1s);
TV.set_cursor(screenWidth / 2 + 12, 3);
TV.print(p2s);
}
void drawBall() {
TV.set_pixel(ballX, ballY, 0);
ballX += ballDx;
ballY += ballDy;
TV.set_pixel(ballX, ballY, 1);
}
void drawPaddles() {
// First - draw black over the current paddles on screen
TV.draw_rect(2, p1y, 1, paddleHeight, 0, 0);
TV.draw_rect(screenWidth - 4, p2y, 1, paddleHeight, 0, 0);
p1y = map(analogRead(A0), 0, 1023, screenHeight - paddleHeight, 0);
TV.draw_rect(2, p1y, 1, paddleHeight, 1, 0);
p2y = map(analogRead(A1), 0, 1023, screenHeight - paddleHeight, 0);
TV.draw_rect(screenWidth - 4, p2y, 1, paddleHeight, 1, 0);
}
void drawMenu() {
TV.clear_screen();
TV.select_font(font8x8);
TV.print(10, 5, "Pong game with MPU6050");
TV.select_font(font6x8);
TV.print(45, 35, "Press");
TV.print(37, 45, "to play");
delay(1000);
}
void decompte() {
TV.clear_screen();
TV.print(63, 45, "5");
delay(1000);
TV.clear_screen();
TV.print(63, 45, "4");
delay(1000);
TV.clear_screen();
TV.print(63, 45, "3");
delay(1000);
TV.clear_screen();
TV.print(63, 45, "2");
delay(1000);
TV.clear_screen();
TV.print(63, 45, "1");
delay(1000);
TV.clear_screen();
TV.print(53, 45, "Start");
delay(1000);
}
void drawCadre() {
TV.draw_line(0,96,0,0,WHITE);
TV.draw_line(128,0,0,0,WHITE);
TV.draw_line(127,0,127,96,WHITE);
TV.draw_line(0,95,128,95,WHITE);
}
Im going to link a Fritzing plan
