Arduino Loop() function not looping

Hello!
This is the first time I've asked a forum for help. I'm pretty new at programming these incredible devices, but they are fun. I am currently trying to run two .91" OLED displays and an accelerometer off of an Arduino Nano using a TCA9548A multiplexer. My code compiles and uploads, but I'm having issues. I get the displays to initialize, but none of my data displays. The main thing I'm writing for though is this: It seems the void loop() function isn't looping. It runs through the entire code and then just stops/hangs. I have used the serial monitor a lot for trying to troubleshoot, so every "Serial.print" line you see in my code below is only for troubleshooting. Those lines do not do anything for the end result.

Any assistance will be greatly appreciated. Thank you for your time.
Here is my code:

/*
  Electronic Level
  Uses MPU-6050 IMU
  Displays on 2 ea. .91" OLED displays and row of LED bulbs

*/
#include <Arduino.h>
#include <SPI.h>

// Include Wire Library for I2C
#include <Wire.h>
extern "C" {
#include "utility/twi.h"  // from Wire Library, so we can do bus scanning
}

// Include two libraries for displays
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define TCAADDR 0x70

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

#define OLED_RESET -1 // Reset Pin # (or -1 if sharing Arduino reset pin)

#define PIN_SDA A4
#define PIN_SCL A5
#define PIN_INT RST

/*                      Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)        */
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// Reset pin not used but needed for library
#define OLED_RESET 10

//Variables for Gyroscope
int gyro_x, gyro_y, gyro_z;
long gyro_x_cal, gyro_y_cal, gyro_z_cal;
boolean set_gyro_angles;

long acc_x, acc_y, acc_z, acc_total_vector;
float angle_roll_acc, angle_pitch_acc;

float angle_pitch, angle_roll;
int angle_pitch_buffer, angle_roll_buffer;
float angle_pitch_output, angle_roll_output;

// Setup timers and temp variables
long loop_timer;
int temp;

// Display counter
int displaycount = 0;

int myCounter = 0;

void tcaselect(uint8_t i) {
  if (i > 7) return;

  Wire.beginTransmission(TCAADDR);
  Wire.write(1 << i);
  Wire.endTransmission();
}

void setup() {

  while (!Serial);
  delay(1000);

  //Start I2C
  Wire.begin();

  Serial.begin(9600);

  display.begin();

  /* Set multiplexer to channel 2 and initialize OLED-0 with I2C addr 0x3C */
  tcaselect(2);
  if (!display.begin(0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }

  display.display();
  delay(2000);
  display.clearDisplay();
  // Print on first row of LCD
  display.setCursor(0, 0);
  display.setTextSize(1);
  display.write("HELLO!");
  display.display();

  /* Initialize OLED-1 to channel 4 with I2C addr 0x3C */
  tcaselect(4);
  if (!display.begin(0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }

  display.display();
  delay(2000);
  display.clearDisplay();
  // Print on first row of LCD
  display.setCursor(0, 0);
  display.setTextSize(1);
  display.write("HELLO!");
  display.display();

  //Setup the registers of the MPU-6050
  tcaselect(7);
  setup_mpu_6050_registers();

  //Read the raw acc and gyro data from the MPU-6050 1000 times
  for (int cal_int = 0; cal_int < 1000 ; cal_int ++) {
    read_mpu_6050_data();
    //Add the gyro x offset to the gyro_x_cal variable
    gyro_x_cal += gyro_x;
    //Add the gyro y offset to the gyro_y_cal variable
    gyro_y_cal += gyro_y;
    //Add the gyro z offset to the gyro_z_cal variable
    gyro_z_cal += gyro_z;
    //Delay 3us to have 250Hz for-loop
    delay(3);
  }

  // Divide all results by 1000 to get average offset
  gyro_x_cal /= 1000;
  gyro_y_cal /= 1000;
  gyro_z_cal /= 1000;


  // Init Timer
  //loop_timer = micros();

  // Troubleshoot 1
  Serial.println("Through Set-up section");
  Serial.print("x = ");
  Serial.print(gyro_x_cal);
  Serial.print("  :  y = ");
  Serial.print(gyro_y_cal);
  Serial.print("  :  z = ");
  Serial.println(gyro_z_cal);
  Serial.println(" ");
}

void loop() {

  // Get data from MPU-6050
  read_mpu_6050_data();

  //Subtract the offset values from the raw gyro values
  gyro_x -= gyro_x_cal;
  gyro_y -= gyro_y_cal;
  gyro_z -= gyro_z_cal;

  //Calculations for accelerometer
  my_calculations();

  //Counting the loops - only display every n-th reading
  my_counters();

}

void my_calculations() {
  // Troubleshoot 2
  Serial.println("Start of Loop section");
  myCounter = myCounter + 1;
  Serial.print("x = ");
  Serial.print(gyro_x);
  Serial.print("  :  y = ");
  Serial.print(gyro_y);
  Serial.print("  :  z = ");
  Serial.print(gyro_z);
  Serial.println("....");
  Serial.print("Counter = ");
  Serial.println(myCounter);

  //Gyro angle calculations . Note 0.0000611 = 1 / (250Hz x 65.5)

  //Calculate the traveled pitch angle and add this to the angle_pitch variable
  angle_pitch += gyro_x * 0.0000611;
  //Calculate the traveled roll angle and add this to the angle_roll variable
  //0.000001066 = 0.0000611 * (3.142(PI) / 180degr) The Arduino sin function is in radians
  angle_roll += gyro_y * 0.0000611;

  //If the IMU has yawed transfer the roll angle to the pitch angle
  angle_pitch += angle_roll * sin(gyro_z * 0.000001066);
  //If the IMU has yawed transfer the pitch angle to the roll angle
  angle_roll -= angle_pitch * sin(gyro_z * 0.000001066);

  //Accelerometer angle calculations

  //Calculate the total accelerometer vector
  acc_total_vector = sqrt((acc_x * acc_x) + (acc_y * acc_y) + (acc_z * acc_z));

  //57.296 = 1 / (3.142 / 180) The Arduino asin function is in radians
  //Calculate the pitch angle
  angle_pitch_acc = asin((float)acc_y / acc_total_vector) * 57.296;
  //Calculate the roll angle
  angle_roll_acc = asin((float)acc_x / acc_total_vector) * -57.296;

  //Accelerometer calibration value for pitch
  angle_pitch_acc -= 0.0;
  //Accelerometer calibration value for roll
  angle_roll_acc -= 0.0;

  if (set_gyro_angles) {

    //If the IMU has been running
    //Correct the drift of the gyro pitch angle with the accelerometer pitch angle
    angle_pitch = angle_pitch * 0.9996 + angle_pitch_acc * 0.0004;
    //Correct the drift of the gyro roll angle with the accelerometer roll angle
    angle_roll = angle_roll * 0.9996 + angle_roll_acc * 0.0004;
  }
  else {
    //IMU has just started
    //Set the gyro pitch angle equal to the accelerometer pitch angle
    angle_pitch = angle_pitch_acc;
    //Set the gyro roll angle equal to the accelerometer roll angle
    angle_roll = angle_roll_acc;
    //Set the IMU started flag
    set_gyro_angles = true;
  }

  //To dampen the pitch and roll angles a complementary filter is used
  //Take 90% of the output pitch value and add 10% of the raw pitch value
  angle_pitch_output = angle_pitch_output * 0.9 + angle_pitch * 0.1;
  //Take 90% of the output roll value and add 10% of the raw roll value
  angle_roll_output = angle_roll_output * 0.9 + angle_roll * 0.1;
  //Wait until the loop_timer reaches 4000us (250Hz) before starting the next loop

  // Print to Serial Monitor
  //Serial.print(" | Angle  = "); Serial.println(angle_pitch_output);
  return;
}

void setup_mpu_6050_registers() {

  //Activate the MPU-6050

  //Start communicating with the MPU-6050
  Wire.beginTransmission(0x68);
  //Send the requested starting register
  Wire.write(0x6B);
  //Set the requested starting register
  Wire.write(0x00);
  //End the transmission
  Wire.endTransmission();

  //Configure the accelerometer (+/-8g)

  //Start communicating with the MPU-6050
  Wire.beginTransmission(0x68);
  //Send the requested starting register
  Wire.write(0x1C);
  //Set the requested starting register
  Wire.write(0x10);
  //End the transmission
  Wire.endTransmission();

  //Configure the gyro (500dps full scale)

  //Start communicating with the MPU-6050
  Wire.beginTransmission(0x68);
  //Send the requested starting register
  Wire.write(0x1B);
  //Set the requested starting register
  Wire.write(0x08);
  //End the transmission
  Wire.endTransmission();

}

void my_counters() {

  if (displaycount == 100) {
    Serial.println("Troubleshoot 5 - in the IF statement");
    screenPrint();
    displaycount = 0;
  }  else {
    // Increment the display counter
    displaycount = displaycount + 1;
  }

  Serial.print("displaycount is:  ");
  Serial.println(displaycount);
  return;
}

void screenPrint() {

  tcaselect(2);
  // Print on first row of LCD
  display.display();
  delay(1000);
  display.clearDisplay();
  display.setCursor(0, 0);
  display.write("Pitch: ");
  display.write(angle_pitch_output);
  display.display();
  Serial.println("Troubleshoot 3");

  tcaselect(4);
  display.display();
  delay(1000);
  display.clearDisplay();
  display.setCursor(0, 0);
  display.write("Roll: ");
  display.write(angle_roll_output);
  display.display();
  Serial.println("Troubleshoot 4");
  return;
}

void read_mpu_6050_data() {

  //Read the raw gyro and accelerometer data

  //Start communicating with the MPU-6050
  Wire.beginTransmission(0x68);
  //Send the requested starting register
  Wire.write(0x3B);
  //End the transmission
  Wire.endTransmission();
  //Request 14 bytes from the MPU-6050
  Wire.requestFrom(0x68, 14);
  //Wait until all the bytes are received
  while (Wire.available() < 14);

  //Following statements left shift 8 bits, then bitwise OR.
  //Turns two 8-bit values into one 16-bit value
  acc_x = Wire.read() << 8 | Wire.read();
  acc_y = Wire.read() << 8 | Wire.read();
  acc_z = Wire.read() << 8 | Wire.read();
  temp = Wire.read() << 8 | Wire.read();
  gyro_x = Wire.read() << 8 | Wire.read();
  gyro_y = Wire.read() << 8 | Wire.read();
  gyro_z = Wire.read() << 8 | Wire.read();
}

This has potential to hang your program forever if there are less than 14 bytes queued for reading

I think that was the intention, since 14 bytes were requested. But if there's a bad connection or it gets out of sync, yes.

Wire.requestFrom(0x68, 14);
  //Wait until all the bytes are received
  while (Wire.available() < 14);

It's definitely an area to focus on...

Since you do have serial debug, could you post the serial output here?

Wire.requestFrom() will return either 14, if the request is successful, or 0 if there is an error. If successful, the function will not return until the transfer is complete, and will always return 14 bytes regardless of how many bytes the slave device sends.

See http://www.gammon.com.au/i2c

I have not looked at the code in detail, but running two OLED displays with full display buffers on a Nano is likely to use too much dynamic memory (ram) - a single display often runs into memory problems.

You have OLED_RESET defined twice.

'Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);'
You only have one 'display' object for both displays. How is the object going to keep track of the state of the display when the hardware is shifting under its feet?

Were you planning to have more than two displays? If not, why not just set the two OLEDs to different addresses?

Your main problem is that you never set the text color:

  display.setTextColor(SSD1306_WHITE);

You also have three display.begin() statements.


void setup() {

  while (!Serial);  //<<<<< not needed on a Nano, and should be AFTER Serial.begin()
  delay(1000);

  //Start I2C
  Wire.begin();

  Serial.begin(9600);

  display.begin(); //<<<< needless display.begin <<<<<<<

  /* Set multiplexer to channel 2 and initialize OLED-0 with I2C addr 0x3C */
  tcaselect(2);
  if (!display.begin(0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }

First of all, thank you all for your replies so quickly. Secondly, something to add, the program worked well on a single LED display. It wasn't until I tried to incorporate the twin OLED displays and the multiplexer that I got issues. I tried referencing several blogs and tutorials on both the displays and the multiplexer and it seems there aren't any two people that do it the same. Any redundant and/or duplicate code referencing either the displays or the multiplexer was me trying to get it to work. Like I said, I'm pretty new to this stuff. I try to reference the libraries when I can, but most of the time the comments within the code are written for people who already understand what the author is trying to say. For a newbie, it's just gibberish.

'johnwasser',
The part of the code you are speaking of was written as:
'Adafruit_SSD1306 display1(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, 1);
Adafruit_SSD1306 display2(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, 1);'

but since I was addressing each of the screens individually, I thought I could "share" the object between the two. I believe that's just me not understanding the code.

'aarg' & killzone_kid',
I honestly didn't know what that snippet of the code did. It was used because I saw it in several other peoples examples. One of you asked for the serial print out... I copied the beginning and the end of it for you. The middle is just a continued repeat of the step until the count reaches 100.

The Serial Print out:

Through Set-up section
x = -197  :  y = -37  :  z = 23
 
Start of Loop section
x = -4  :  y = 3  :  z = -2....
Counter = 1
displaycount is:  1
Start of Loop section
x = -5  :  y = -1  :  z = -7....
Counter = 2
displaycount is:  2
Start of Loop section
x = -10  :  y = -15  :  z = 2....
.........
(ending)
Start of Loop section
x = 0  :  y = -16  :  z = -7....
Counter = 99
displaycount is:  99
Start of Loop section
x = -1  :  y = -5  :  z = 2....
Counter = 100
displaycount is:  100
Start of Loop section
x = -12  :  y = -12  :  z = 7....
Counter = 101
Troubleshoot 5 - in the IF statement
Troubleshoot 3
Troubleshoot 4
displaycount is:  0

Hello again.
Well, I've cleaned up a lot of the problems you all mentioned. I've got a display object for each of the displays, I got rid of the duplicate "OLED_RESET"s, I reduced the count down to 50, and I've separated most of the code into their individual functions and cleaned up the loop() section.

Something strange is happening in the logic of this program that I don't understand. When it goes into the "my_calculations()" function, it loops through the entire thing 50 times but only prints the troubleshooting serial print section (located at the end of the function) once, and only after it has counted to 50. It never prints out the troubleshooting serial print when the count is under 50 and it only calls on the screenPrint() function at the end.

Below is the revised code and a copy of the serial monitor. If anyone has any other ideas, please let me know. I'm at a loss. Thank you in advance for your time.
Ron.


/*
  Electronic Level
  Uses MPU-6050 IMU
  Displays on 2 ea. .91" OLED displays and row of LED bulbs

*/
#include <Arduino.h>
#include <SPI.h>

// Include Wire Library for I2C
#include <Wire.h>
extern "C" {
#include "utility/twi.h"  // from Wire Library, so we can do bus scanning
}

// Include two libraries for displays
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define TCAADDR 0x70

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

#define OLED_RESET 4 // Reset Pin # (or -1 if sharing Arduino reset pin)

#define PIN_SDA A4
#define PIN_SCL A5
#define PIN_INT RST

/*                      Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)        */
Adafruit_SSD1306 display1(OLED_RESET);
Adafruit_SSD1306 display2(OLED_RESET);

//Variables for Gyroscope
int gyro_x, gyro_y, gyro_z;
long gyro_x_cal, gyro_y_cal, gyro_z_cal;
boolean set_gyro_angles;

long acc_x, acc_y, acc_z, acc_total_vector;
float angle_roll_acc, angle_pitch_acc;

float angle_pitch, angle_roll;
int angle_pitch_buffer, angle_roll_buffer;
float angle_pitch_output, angle_roll_output;

// Setup timers and temp variables
long loop_timer;
int temp;

// Display counter
int displaycount = 0;

int myCounter = 0;

void tcaselect(uint8_t i) {
  if (i > 7) return;

  Wire.beginTransmission(TCAADDR);
  Wire.write(1 << i);
  Wire.endTransmission();
}

void setup() {

  while (!Serial);
  delay(1000);

  //Start I2C
  Wire.begin();

  Serial.begin(9600);

  /* Set multiplexer to channel 2 and initialize OLED-0 with I2C addr 0x3C */
  tcaselect(2);
  display1.begin(0x3C);

  if (!display1.begin(0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }

  display1.display();
  delay(2000);
  display1.clearDisplay();
  // Print on first row of LCD
  display1.setCursor(0, 0);
  display1.setTextSize(1);
  display1.write("HELLO!");
  display1.display();

  /* Initialize OLED-1 to channel 4 with I2C addr 0x3C */
  tcaselect(4);
  display2.begin(0x3C);

  if (!display2.begin(0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }

  display2.display();
  delay(2000);
  display2.clearDisplay();
  // Print on first row of LCD
  display2.setCursor(0, 0);
  display2.setTextSize(1);
  display2.write("HELLO!");
  display2.display();

  //Setup the registers of the MPU-6050
  tcaselect(7);
  setup_mpu_6050_registers();

  //Read the raw acc and gyro data from the MPU-6050 1000 times
  for (int cal_int = 0; cal_int < 1000 ; cal_int ++) {
    read_mpu_6050_data();
    //Add the gyro x offset to the gyro_x_cal variable
    gyro_x_cal += gyro_x;
    //Add the gyro y offset to the gyro_y_cal variable
    gyro_y_cal += gyro_y;
    //Add the gyro z offset to the gyro_z_cal variable
    gyro_z_cal += gyro_z;
    //Delay 3us to have 250Hz for-loop
    delay(3);
  }


  // Divide all results by 1000 to get average offset
  gyro_x_cal /= 1000;
  gyro_y_cal /= 1000;
  gyro_z_cal /= 1000;


  // Init Timer
  //loop_timer = micros();

  // Troubleshoot 1
  Serial.println("Through Set-up section");
  Serial.print("x = ");
  Serial.print(gyro_x_cal);
  Serial.print("  :  y = ");
  Serial.print(gyro_y_cal);
  Serial.print("  :  z = ");
  Serial.println(gyro_z_cal);
  Serial.println(" ");
}

// ******************************************************************
void loop() {

  // Get data from MPU-6050
  read_mpu_6050_data();

  //Subtract the offset values from the raw gyro values
  gyro_x -= gyro_x_cal;
  gyro_y -= gyro_y_cal;
  gyro_z -= gyro_z_cal;

  //Calculations for accelerometer
  my_calculations();



}
// ******************************************************************

void my_calculations() {
  // Troubleshoot 2
  Serial.println("Start of Loop section");
  myCounter = myCounter + 1;
  Serial.print("x = ");
  Serial.print(gyro_x);
  Serial.print("  :  y = ");
  Serial.print(gyro_y);
  Serial.print("  :  z = ");
  Serial.print(gyro_z);
  Serial.println("....");
  Serial.print("Counter = ");
  Serial.println(myCounter);

  //Gyro angle calculations . Note 0.0000611 = 1 / (250Hz x 65.5)

  //Calculate the traveled pitch angle and add this to the angle_pitch variable
  angle_pitch += gyro_x * 0.0000611;
  //Calculate the traveled roll angle and add this to the angle_roll variable
  //0.000001066 = 0.0000611 * (3.142(PI) / 180degr) The Arduino sin function is in radians
  angle_roll += gyro_y * 0.0000611;

  //If the IMU has yawed transfer the roll angle to the pitch angle
  angle_pitch += angle_roll * sin(gyro_z * 0.000001066);
  //If the IMU has yawed transfer the pitch angle to the roll angle
  angle_roll -= angle_pitch * sin(gyro_z * 0.000001066);

  //Accelerometer angle calculations

  //Calculate the total accelerometer vector
  acc_total_vector = sqrt((acc_x * acc_x) + (acc_y * acc_y) + (acc_z * acc_z));

  //57.296 = 1 / (3.142 / 180) The Arduino asin function is in radians
  //Calculate the pitch angle
  angle_pitch_acc = asin((float)acc_y / acc_total_vector) * 57.296;
  //Calculate the roll angle
  angle_roll_acc = asin((float)acc_x / acc_total_vector) * -57.296;

  //Accelerometer calibration value for pitch
  angle_pitch_acc -= 0.0;
  //Accelerometer calibration value for roll
  angle_roll_acc -= 0.0;

  if (set_gyro_angles) {

    //If the IMU has been running
    //Correct the drift of the gyro pitch angle with the accelerometer pitch angle
    angle_pitch = angle_pitch * 0.9996 + angle_pitch_acc * 0.0004;
    //Correct the drift of the gyro roll angle with the accelerometer roll angle
    angle_roll = angle_roll * 0.9996 + angle_roll_acc * 0.0004;
  }
  else {
    //IMU has just started
    //Set the gyro pitch angle equal to the accelerometer pitch angle
    angle_pitch = angle_pitch_acc;
    //Set the gyro roll angle equal to the accelerometer roll angle
    angle_roll = angle_roll_acc;
    //Set the IMU started flag
    set_gyro_angles = true;
  }

  //  TROUBLESHOOTING TOOL ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  if (displaycount == 50) {
    Serial.println("Troubleshoot 5 - in the IF statement");
    screenPrint();
    displaycount = 0;
  }

  if (displaycount < 50) {
    // Increment the display counter
    displaycount = displaycount + 1;
    return;

    Serial.println(" ");
    Serial.print("displaycount is:  ");
    Serial.println(displaycount);
  } // END TROUBLESHOOTING TOOL  ^^^^^^^^^^^^^^^^^^^^^^


  //To dampen the pitch and roll angles a complementary filter is used
  //Take 90% of the output pitch value and add 10% of the raw pitch value
  angle_pitch_output = angle_pitch_output * 0.9 + angle_pitch * 0.1;
  //Take 90% of the output roll value and add 10% of the raw roll value
  angle_roll_output = angle_roll_output * 0.9 + angle_roll * 0.1;
  //Wait until the loop_timer reaches 4000us (250Hz) before starting the next loop

  // Print to Serial Monitor
  //Serial.print(" | Angle  = "); Serial.println(angle_pitch_output);

}

void setup_mpu_6050_registers() {

  //Activate the MPU-6050

  //Start communicating with the MPU-6050
  Wire.beginTransmission(0x68);
  //Send the requested starting register
  Wire.write(0x6B);
  //Set the requested starting register
  Wire.write(0x00);
  //End the transmission
  Wire.endTransmission();

  //Configure the accelerometer (+/-8g)

  //Start communicating with the MPU-6050
  Wire.beginTransmission(0x68);
  //Send the requested starting register
  Wire.write(0x1C);
  //Set the requested starting register
  Wire.write(0x10);
  //End the transmission
  Wire.endTransmission();

  //Configure the gyro (500dps full scale)

  //Start communicating with the MPU-6050
  Wire.beginTransmission(0x68);
  //Send the requested starting register
  Wire.write(0x1B);
  //Set the requested starting register
  Wire.write(0x08);
  //End the transmission
  Wire.endTransmission();

}

void screenPrint() {

  tcaselect(2);
  // Print on first row of LCD
  display1.display();
  delay(1000);
  display1.clearDisplay();
  display1.setCursor(0, 0);
  display1.write("Pitch: ");
  display1.write(angle_pitch_output);
  display1.display();
  Serial.println("Troubleshoot 3");

  tcaselect(4);
  display2.display();
  delay(1000);
  display2.clearDisplay();
  display2.setCursor(0, 0);
  display2.write("Roll: ");
  display2.write(angle_roll_output);
  display2.display();
  Serial.println("Troubleshoot 4");
}

void read_mpu_6050_data() {

  //Read the raw gyro and accelerometer data

  //Start communicating with the MPU-6050
  Wire.beginTransmission(0x68);
  //Send the requested starting register
  Wire.write(0x3B);
  //End the transmission
  Wire.endTransmission();
  //Request 14 bytes from the MPU-6050
  Wire.requestFrom(0x68, 14);
  //Wait until all the bytes are received
  while (Wire.available() < 14);

  //Following statements left shift 8 bits, then bitwise OR.
  //Turns two 8-bit values into one 16-bit value
  acc_x = Wire.read() << 8 | Wire.read();
  acc_y = Wire.read() << 8 | Wire.read();
  acc_z = Wire.read() << 8 | Wire.read();
  temp = Wire.read() << 8 | Wire.read();
  gyro_x = Wire.read() << 8 | Wire.read();
  gyro_y = Wire.read() << 8 | Wire.read();
  gyro_z = Wire.read() << 8 | Wire.read();
}

Monitor print out:

Through Set-up section
x = -208  :  y = -35  :  z = 25
 
Start of Loop section
x = -6  :  y = 11  :  z = -3....
Counter = 1
Start of Loop section
x = 0  :  y = 1  :  z = -6....
Counter = 2
Start of Loop section
x = -1  :  y = 1  :  z = 0....
Counter = 3
Start of Loop section
x = -8  :  y = 1  :  z = 1....
,,,,,,,,,,,,,,,,,,,,,,,  END OF SERIAL PRINT
Start of Loop section
x = 6  :  y = 0  :  z = -3....
Counter = 49
Start of Loop section
x = 9  :  y = -2  :  z = -4....
Counter = 50
Start of Loop section
x = 8  :  y = -11  :  z = -6....
Counter = 51
Troubleshoot 5 - in the IF statement
Troubleshoot 3
Troubleshoot 4

... because of the

return;

that follows

aarg,

The return that follows where?

where you do the troubleshooting print you mentioned...

  if (displaycount < 50) {
    // Increment the display counter
    displaycount = displaycount + 1;
    return;

Sorry, I should have said "precedes"...

Well, I thought maybe you were onto something. I took the "return;" out of it and I still don't get it to print until the end and of course the "displaycount" is only 1 since it only went through that portion of the code once. I don't get it. It loops through that whole function except the "TROUBLESHOOTING" part. The end of the serial print is below:

Start of Loop section
x = 1  :  y = 3  :  z = 5....
Counter = 49
 
displaycount is:  49
Start of Loop section
x = -1  :  y = -4  :  z = 4....
Counter = 50
 
displaycount is:  50
Start of Loop section
x = -9  :  y = -2  :  z = -2....
Counter = 51
Troubleshoot 5 - in the IF statement
Troubleshoot 3
Troubleshoot 4
 
displaycount is:  1

and yet it stops looping as soon as it gets to the count that I am looking for..... 50.

I don't quite understand what you are asking. You clearly only want to print the troubleshooting 5 line and run the screenPrint function when displaycount equals 50.

  //  TROUBLESHOOTING TOOL ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  if (displaycount == 50) {
    Serial.println("Troubleshoot 5 - in the IF statement");
    screenPrint();
    displaycount = 0;
  }

Yes, but I want it to keep doing that. It doesn't keep looping. I was under the understanding that the void loop() would continue over and over. What is making it stop at the first count of 50?

You never set the multiplexer back to the MPU-6050 after switching it over to the displays.

1 Like

Ahhhhhh! Thank you!!! I feel so dumb. I really appreciate your help.

Thank you all for the assistance. You made my first time going to a forum for help a very satisfying one. I was able to get the loop to work like I needed it to. I still don't have the displays working, but I still have options to try before coming for help.
Thank you again.
Ron.

1 Like

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