Wire library and TVout doesn't work together

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

TVout will use almost all the CPU of you small arduino through interrupts- this likely impacts everything else you try to do.
I would not plan to use an arduino if I needed a TV signal out. Use an RPi for example

1 Like

Post a schematic of how you have it wired, not a frizzy picture along with links to technical information on the hardware devices.


here you go

you missed that point :wink:

Go on. Please explain WHY you don't like Fritzing diagrams.

They look pretty clear to me. And they are much easier for a layman to follow.

Ok, I am suspicious of in line T junctions. When you would probably use individual wires to the breadboard.

And some breadboards have a gap in the power bus lines. The diagram shows continuous bus lines. We have to trust that the OP is using the correct image.

David.

We tend to not like Fritzing diagrams because they are usually hard to read esp. on a smartphone - for example I can't easily read the pins names for your two components at the bottom

You can't see the value of the components easily (resistor, cap) .

You can't really tell what components are being used and if they are the exact type you have or just what the library had to offer. for example your 2 components at the bottom right are unlabelled (your 2 MPU6050 I suspect of course) - but which ones did you use? (➜ hence the requests for links to technical information on the hardware devices) or there are many variants of breadboards, the one you show might not be the breadboard you have (some are cut in the middle and the power lines do not go through all the way)

very often (not your case) the lines are just overlapping in a big mess and it's hard to tell what's connected where (your red lines going straight above the 5V pin, you could argue it's unclear if they just happen to go over the board or if both are plugged into the 5V pin)

very often this is the theoretical thingy the poster took on the internet but failed to reproduce. So asking them to grab a piece of paper and draw with a ruler and pencil exactly as things are connected in real life helps too

but anyway - as I wrote, I think the challenge here is that TVout is requiring lots of CPU cycles through interrupts and that has an impact on the rest of the system

...

Two finger zoom doesn't work on your phone?

That's a worry. :thinking:

Whose two? David's?

The diagram was from @encryptsine.

Methinks @david_prentice may be teasing you. :grin:

Thanks for the reply. I have asked several times. gilshultz has never replied.

Yes, I understand the resistor values. I understand the 5V connection is unclear. Hence it is wise to use individual wires to the breadboard avoiding inline T connections.

I also understand that the diagram might just be copied from a website. However it looks pretty genuine to me. Newcomers are allergic to capacitors. And often use big electrolytics where 100nF ceramic are required.
I can read the MPU6050 labels on a Tablet and on a PC. I don't have a Smart phone.

I like pencil drawings but they can be less clear than coloured wires on a Fritzing. I also prefer conventional schematics to understand a circuit.
However a physical wiring scheme is easier for a user to connect and to check. Especially if they stick to the same wire colours shown in the Fritzing.

Hey-ho. We all have different opinions in life.

I think it is wise to post links to the actual electronic modules that you have bought. Then we don't need to guess about which make, 5V tolerance, current, chips, ... are in a module.

Unfortunately I have no experience of TVOut. So I can't contribute.
But the wiring scheme looks ok to me.

David.

For the component I have :
A Arduino UNO r3
An RCA cable that I have manually solder
And 2 mpu 6050 : https://www.amazon.fr/ARCELI-MPU6050-AccéléromÚtre-Gyroscope-Convertisseur/dp/B07BVXN2GP/ref=sr_1_2?keywords=mpu6050&qid=1649790506&sprefix=mpu%2Caps%2C107&sr=8-2

Agree to that and not all fritzing are born equals

Then I loose the text and global view of the circuit. My eyesight is not what it used to be


david_prentice I haven't bothered as many others as here have answered for me. To make you happy here are a few: I cannot read it, I have a 36" monitor running at 4K and the lettering on the blue things in not readable. No idea what the yellow blob is. There are two small blobs in the lower right, again I do not know what they are. To find out I must read your explanation, and many times I read similar as or it should be the same. That is also why I ask for links to technical information, azon is generally just a sales link and a consumer of my time. How you join the wires in the middle is a mystery as you plug in the ends. Looking at a schematic I would have the picture in a few seconds, not spend time looking up stuff that may not be correct. You show a switch is it NO or NC? You show resistors what is the value, that is indeterminate on a BW monitor. Your frizzy has no power connections so it gets its energy from the cloud. Not all of the plug boards are the same, the long rails are sometimes split in the middle depending on which one you have, I have both types. Where is the title for the picture, I do not see it.

J-M-L is the only person that has ever answered my query.

And he managed to do it with punctuation !!

I can understand doubts over connections, resistor values, ..., pin labels.

However I can see a 10k and a 470R. I can read the module pin labels.
He explains that the picture of a RCA plug is for TV out.

I just make the naive assumption that there is a copper wire inside each coloured jumper cable.

The OP has linked to the hardware in #10 which has cleared up my worries about 3.3V

Ok, it would have been nice if the OP had provided wiring scheme and component links in #1. But I think he has done an excellent job.
And even nicer if it was a subject that I am confident in.
But I am sure that there are TVOut experts out there. And they can give good advice.

Oh, I regard a schematic as a formal left-to-right drawing of electrical components. Arranged for electrical clarity with no regard for physical placement of components.
A wiring scheme is a point to point diagram of how to connect your wires to the component modules. Or a formal list of end-to-end points e.g. named terminals.

I am not an Electrician. But motors, switchgear, switches, ... are generally drawn as a point to point wiring scheme. I am not sure if the average Electrician is familiar with academic electronic schematics.
But I would prefer my house to be wired by an Electrician than an Electronics expert.

David.

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