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]);
}
}