Oled code breaks Blynk

This is for a project due soon so I am starting to panic. The code commented out in “void fall()” breaks Blynk and causes it to not connect to the app. The OLED code breaks it and so does the if statement. I’m not sure why it is doing this. The code works perfectly when it is commented out. Any help would be greatly appreciated as I’ve been struggling.

Arduino Nano with ESP8266 connection.
Android Oreo Blynk App
Blynk Version 0.5.3



#define BLYNK_PRINT Serial

#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"

#define I2C_ADDRESS 0x3C
#define RST_PIN 4
MPU6050 accelgyro;
SSD1306AsciiWire oled;

int16_t ax, ay, az;
//int16_t gx, gy, gz;

float nax;
float nay;
float naz;
float mag;
//float fall_alt;
//float fall_value;
bool notSent = true;

int spike_count = 0;

boolean falling, send_message;
volatile boolean cancel;

#define OUTPUT_READABLE_ACCELGYRO

// WIRELESS / Blynk
char auth[] = "XXXX";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "XXXX";
char pass[] = "XXXX";

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial1

//#define BLYNK_PRINT Serial
// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

BlynkTimer timer;
  //WidgetLED led1(V1);

//WidgetTerminal terminal(V0);

void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.

  Blynk.virtualWrite(V5, millis() / 1000);
  
}

void getAcceleration() {
    accelgyro.getAcceleration(&ax, &ay, &az); 
    nax = (ax/163.84);
    nay = (ay/163.84);
    naz = (az/163.84);
    mag = pow(pow(nax,2)+pow(nay,2)+pow(naz,2),0.5);
}

void fall()
{
  
  getAcceleration();
    if (mag>125) {                    //345!!!!!!!
      falling = true;
    }

   if (mag>125 && falling) { //290!!!!
      spike_count++;
    }

 if (falling && notSent) 
    {
         Blynk.virtualWrite(V1, 255);
          Blynk.virtualWrite(V0, "Fall Detected!");
          notSent = false;
          oled.clear();
  //oled.set2X();
  //oled.println("Fall Detected!");
         }

/*if (spike_count > 4 && falling) {
  spike_count = 0;
  falling = false;
  notSent = true;
  falsepos();
}
  
 */ 
         
    }



void falsepos() {
  Blynk.virtualWrite(V0, "False Alarm!");
  Blynk.virtualWrite(V1, 0);
}
void setup()
{
   #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
        Wire.begin();
        //Wire.setClock(400000);
        // Wire.setClock(400000L);
    #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
        Fastwire::setup(400, true);
    #endif
       oled.begin(&Adafruit128x32, I2C_ADDRESS, RST_PIN);
oled.setFont(Adafruit5x7);
  oled.clear();
  oled.set2X();
  oled.println("On");
  // Debug console
  Serial.begin(9600);

 accelgyro.initialize();
    accelgyro.setXGyroOffset(220);
    accelgyro.setYGyroOffset(76);
    accelgyro.setZGyroOffset(-85);
    accelgyro.setXAccelOffset(-977);
    accelgyro.setYAccelOffset(707);
    accelgyro.setZAccelOffset(1109);
  
  
  
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8442);
  

  // Setup a function to be called every second
  timer.setInterval(1000L, myTimerEvent);
  timer.setInterval(1L, fall);

}


void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}


It is not the OLED code causing the issue… more likely the fact that you are running the relevant timer at 1ms :scream: … not even enough time to complete part of it’s tasks before attempting to run it again…

Then when activating the Blynk.virtualWrite() commands… well, who knows how rapidly those try to run each loop and cause disconnections.

Bad timing + bad code = bad results

Thank you, is there a way I can run the crucial code separately at 1ms and the rest slower? I need this code:

getAcceleration();
    if (mag>125) {                    
      falling = true;
    }

to be run almost instantly.

Thank you.

If this is a quick sensor read and simply passing the data to a variable for somewhat less frequent processing, then you can probably have it in the void loop()… It is the function that uses the falling variable and subsequent reaction that can’t take up too much overall time from Blynk’s own background processing.

Blynk is for IoT… lots of App <–> Server <–> Device cross communication… and thus not really suited for instantaneous sensor/reaction processing.

Quick and fast processing can be done in the background, but if you want App display then you need to somehow run that on samples instead of real time.

1 Like

Legend! Thank you.