Display non-raw data and LCD at same time

I am using a Mega2560 and using USB serial to communicate w/ the Blynk app. I have the most current Blynk Library. and verified it is all installed properly.

I can use the example serial USB sketches and get sensor data to Blynk. No problem with this portion.

I have a pH sensor(analog), turbidity sensor(analog), current sensor(analog), and an ultrasonic sensor(digiaal) that I am reading data. All of these sensors work and are being serially printed. I am also displaying these values on a Sparkfun LCD. At the moment I am using my void loop() to send these values to my LCD. I have read that it is best to keep the void loop clean. Is there a way that I can use my screen and Blynk at the same time?

If I use an example sketch, it will display raw data but not my calculated data. How do I get it to display exactly as my serial monitor does?

I am also having problems getting my ultrasonic sensor to work with Blynk. It uses two digital pins to measure the trigger and echo. In the Blynk app, when I select digital it only uses one pin. Any suggestions on that?

#define BLYNK_PRINT Serial1
#include <BlynkSimpleStream.h>

char auth[] = "xxx]";

  // defines pins numbers
  const int trigPin = 9;
  const int echoPin = 10;

  const int currentSensor = A1;

void setup() {
  GLCD.Init();
  GLCD.SelectFont(System5x7);

  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  
  Serial1.begin(9600);
  Serial.begin(9600); // Starts the serial communication
  Blynk.begin(Serial,auth);
}

float ultrasonic() {
  // defines variables
  long duration;
  float distance;

  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2;

  return distance;
}

float current() {
  int readValue;             //value read from the sensor
  int maxValue = 0;          // store max value here
   uint32_t start_time = millis();
   while((millis()-start_time) < 1000) //sample for 1 Sec
   {
       readValue = analogRead(currentSensor);
       // see if you have a new maxValue
       if (readValue > maxValue) 
       {
           /*record the maximum sensor value*/
           maxValue = readValue;
       }
   }
  
  float nVPP = (maxValue * 5.0) / 1024.0;
  /*
   * Use Ohms law to calculate current across resistor
   and express in mA 
   */
   
   float nCurrThruResistorPP = (nVPP/200.0) * 1000.0;
   /* 
   Use Formula for SINE wave to convert
   to RMS 
   */
   
   float nCurrThruResistorRMS = nCurrThruResistorPP * 0.707;
   /* 
   Current Transformer Ratio is 1000:1...
   
   Therefore current through 200 ohm resistor
   is multiplied by 1000 to get input current
   */
   
   float nCurrentThruWire = nCurrThruResistorRMS * 1000;

   Serial.print("Volts Peak : ");
   Serial.println(nVPP,3);
 
   Serial.print("Current Through Resistor (Peak) : ");
   Serial.print(nCurrThruResistorPP,3);
   Serial.println(" mA Peak to Peak");
   
   Serial.print("Current Through Resistor (RMS) : ");
   Serial.print(nCurrThruResistorRMS,3);
   Serial.println(" mA RMS");
   
   Serial.print("Current Through Wire : ");
   Serial.print(nCurrentThruWire,3);
   Serial.println(" mA RMS");
   
   Serial.println();

   return nCurrentThruWire;
}

float turbidity() {
  int sensorValue = analogRead(A0);
  return sensorValue * (100.0 / 1024.0);
}

float ph() {
  const int pin = A2;
  int buf[10];

  for (int i = 0; i < 10; i++) {
    buf[i] = analogRead(pin);
    delay(10);
  }

  for (int i = 0; i < 9; i++)
  {
    for (int j = i + 1; j < 10; j++)
    {
      if (buf[i] > buf[j])
      {
        int temp = buf[i];
        buf[i] = buf[j];
        buf[j] = temp;
      }
    }
  }
  int avgValue = 0;
  for (int i = 2; i < 8; i++)
    avgValue += buf[i];
  float pHVol = (float)avgValue * 5.0 / 1024 / 6;
  return  -5.70 * pHVol + 21.34;
}


typedef struct {
  const char *prompt;
  float (*function)();
} Sensor;

const Sensor sensors[] = {
  {"Distance: ", ultrasonic},
  {"Current: ", current},
  {"% Turbidity: ", turbidity},
  {"pH: ", ph},
  {NULL, NULL}
};

void loop() {
  Blynk.run();
  
  int sensorCount = 0;
  while(true) {
    Sensor sensor = sensors[sensorCount];
    if (!sensor.function) break;
    sensorCount++;
  }

  float sensorValues[sensorCount];
  for (int i = 0; i < sensorCount; i++) {
    sensorValues[i] = sensors[i].function();
  }
  
  GLCD.ClearScreen();

  Serial.println(sensorCount);
  for (int i = 0; i < sensorCount; i++) {
    GLCD.CursorTo(0, i);
    GLCD.print(sensors[i].prompt);
    GLCD.print(sensorValues[i]);
  }
}

Use timed functions to send your LCD data…

yes I have heard this but is there anyways to display data to my LCD and Blynk at the same time as I have it?

I just told you how… you can do all sorts of external displays and sensors at the same time as being connected to the Blynk App if you code things properly in timed loops.

Ultrasonic sensors are picky on timing, but it is workable…

And as for your viewing of calculated vs raw data, well that is simply up to how you process the data before sending it to the LCD (or App… or both)… not really a Blynk issue.

Thank you for your advice. I have cleared up my void loop and have restructured my code. I am able to upload without any errors. I am trying to read my sensor data over virtual pins, however I am not getting any data on Blynk. If I use the analog pins I can see view my raw data. I have omitted my LCD code to try and narrow it down. Any suggestions where I should go from here? Thanks.

#define BLYNK_PRINT Serial1
#include <BlynkSimpleStream.h>
#include <SPI.h>

char auth[] = "xxx";

// Pin definitions.
const int trigPin = 9;
const int echoPin = 10;

BlynkTimer timer;

void setup() {
  Serial1.begin(9600);
  Serial.begin(9600);
  Blynk.begin(Serial,auth);
  
  // Configure the sensor I/O pins.
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  Blynk.run();
  timer.run();  
} 

void Ultrasonic(){  
    int long duration = 0;
    int long distance = 0;
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
 
  duration = pulseIn(echoPin, HIGH);  // Measure the time for which the echo pin is high.
  distance = duration * 0.034 / 2; // Convert to inches.
  
  Blynk.virtualWrite(V9, distance);
}

//// Reads the current sensor and returns the measured current in mA.
//// Code based on http://henrysbench.capnfatz.com/henrys-bench/arduino-current-measurements/ta12-100-arduino-ac-current-sensor-tutorial/.
float current() {
  int maxValue = 0;
  uint32_t start_time = millis();
  
  //Take the maximum value over one second.
  while ((millis() - start_time) < 1000) {
    int readValue = analogRead(A1); 
    if (readValue > maxValue) maxValue = readValue;
  }
  float nVPP = (maxValue * 5.0) / 1024.0;          // Convert to volts
  float nCurrThruResistorPP = (nVPP/200.0)         * 1000.0; //Use Ohms law to calculate current across resistor in mA
  float nCurrThruResistorRMS = nCurrThruResistorPP * 0.707; // Convert to RMS
  /*
   Current Transformer Ratio is 1000:1... Therefore current through 200 ohm resistor
   is multiplied by 1000 to get input current
   */
  Blynk.virtualWrite(V1,nCurrThruResistorRMS * 1000);
}

// Measures the turbitity and returns the measured value in percent.
void turbidity() {
  int sensorValue = analogRead(A0);
  float turbiditySensor = sensorValue * (100.0 / 1024.0);
  Blynk.virtualWrite(V0,turbiditySensor);
}

//// Measures and returns the pH.
//// Code from http://scidle.com/how-to-use-a-ph-sensor-with-arduino/.

float ph() {
  //Take ten readings, spaced 10ms apart.
  int buf[10];
  for (int i = 0; i < 10; i++) {
    buf[i] = analogRead(A2);
    delay(10);
  }
  
  // Perform a bubble sort.
  // This is inefficent, but it wasn't worth rewriting
  // as this is not performance-critical.
  
  for (int i = 0; i < 9; i++) {
    for (int j = i + 1; j < 10; j++) {
     //For each pair of adjacent elements...
      
      if (buf[i] > buf[j]) { // If the elements are out of order,
//        // swap them.
        int temp = buf[i];
        buf[i] = buf[j];
        buf[j] = temp;
      }
    }
  }
 
 // Drop the first and last two values,
 // and take the average of what remains.
 int avgValue = 0;
  for (int i = 2; i < 8; i++) avgValue += buf[i];
   
 // Convert to pH and return.
 float pHVol = (float)avgValue * 5.0 / 1024 / 6;
 Blynk.virtualWrite(V2, -5.70 * pHVol + 21.34);
}

Any more suggestions?

Is your Arduino even connecting to the App?

Yes, if I change to the analog instead of virtual in Blynk, I can see the raw data on my phone from my analog pins.

Ah, that’s what you meant.

When using the direct pin readings in a Widget, you set a reading interval… but when using the sketch to send data to the App via Blynk.virtualWrite() you need to set the Widget to PUSH… Have you done that?

Yes, all my virtual pins are on push