How to add this code to Blynk

ive been trying to get a vacuum sensor to work with blynk.
I have got it connected to A0 and with no code mods to the ethernet example im able to get the gauge widget to show the value. but not accurately.
the sensor is 0.5v to 4.5v.
id like to show the accurate value in a display widget on virtual pin 1
ive tried putting the posted code in the void loop of the blynk code then putting the Blynk.virtualWrite (V1,inHG); before the void loop but it will not compile
id like it to update every 1 second.
I tried adding this code to the push data blynk example but it will not work
im a bit confused by the timer event and the Blynkvirtual write

Thanks for the help in advance

 // MEASUREMENT
  // make 16 readings and average them (reduces some noise) you can tune the number 16 of course
  int count = 16;
  int raw = 0;
  for (int i=0; i< count; i++) raw += analogRead(A0);  // return 0..1023 representing 0..5V
  raw = raw / count;

  // CONVERT TO VOLTAGE
  float voltage = 5.0 * raw / 1023; // voltage = 0..5V;  we do the math in millivolts!!

  // INTERPRET VOLTAGES
  if (voltage < 0.5)
  {
    Serial.println("open circuit");
  }
  else if (voltage < 4.5)  // between 0.5 and 4.5 now...
  {
    float inHG = mapFloat(voltage, 0.5, 4.5, -29.52, 61.08);    // variation on the Arduino map() function
    Serial.print (raw);
    Serial.print(",  ");
    Serial.print(voltage);
    Serial.print(",    ");
    Serial.println(inHG, 2);  // 2 decimals
  }
  else 
  {
    Serial.println("Signal too high!!");
  }
}

float mapFloat(float x, float in_min, float in_max, float out_min, float out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}



You should read this:
http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/keep-your-void-loop-clean

Pete.

I followed your advice and modified my code to keep the void loop open and clean
only problem is I keep getting an error while compiling
Blynk does not name a type
for this line of code
Blynk.virtualWrite(V1,inHG);

#define BLYNK_PRINT Serial


#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";

BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sensordataSend()
{
   // MEASUREMENT
  // make 16 readings and average them (reduces some noise) you can tune the number 16 of course
  int count = 16;
  int raw = 0;
  for (int i=0; i< count; i++) raw += analogRead(A0);  // return 0..1023 representing 0..5V
  raw = raw / count;

  // CONVERT TO VOLTAGE
  float voltage = 5.0 * raw / 1023; // voltage = 0..5V;  we do the math in millivolts!!

  // INTERPRET VOLTAGES
  if (voltage < 0.5)
  {
    Serial.println("open circuit");
  }
  else if (voltage < 4.5)  // between 0.5 and 4.5 now...
  {
    float inHG = mapFloat(voltage, 0.5, 4.5, -29.52, 61.08);    // variation on the Arduino map() function
    Serial.print (raw);
    Serial.print(",  ");
    Serial.print(voltage);
    Serial.print(",    ");
    Serial.println(inHG, 2);  // 2 decimals
  }
  else 
  {
    Serial.println("Signal too high!!");
  }
}

float mapFloat(float x, float in_min, float in_max, float out_min, float out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
 Blynk.virtualWrite(V1,inHG);
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth);

  // Setup a function to be called every second
  timer.setInterval(1000L,sensordataSend);
}

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

You have these commands “floating” around outside of a function…

float mapFloat(float x, float in_min, float in_max, float out_min, float out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
Blynk.virtualWrite(V1, inHG);
}

Check all your brackets.

not sure what bracket you mean?
curly bracket or regular bracket?

In this case I believe the curly ones. I am not taking the time to fix your code, just point you to the area I saw a problem with, thus there could be other issues. But mismatching either can have compilation errors.

I appreciate the guidance. I think I know what you mean by the brackets I see it now. ill fix it and give it a go.

“Blynk.virtualWrite(V1,inHG);” needs to be in the same curly brackets as “float inHG …”,

  {
    float inHG = mapFloat(voltage, 0.5, 4.5, -29.52, 61.08);    // variation on the Arduino map() function
    Serial.print (raw);
    Serial.print(",  ");
    Serial.print(voltage);
    Serial.print(",    ");
    Serial.println(inHG, 2);  // 2 decimals
    Blynk.virtualWrite(V1,inHG);
  }

thanks wickedbeernut,



#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";

BlynkTimer timer;

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sensordataSend()
{
   // MEASUREMENT
  // make 16 readings and average them (reduces some noise) you can tune the number 16 of course
  int count = 16;
  int raw = 0;
  for (int i=0; i< count; i++) raw += analogRead(A0);  // return 0..1023 representing 0..5V
  raw = raw / count;

  // CONVERT TO VOLTAGE
  float voltage = 5.0 * raw / 1023; // voltage = 0..5V;  we do the math in millivolts!!

  // INTERPRET VOLTAGES
  if (voltage < 0.5)
  {
    Serial.println("open circuit");
  }
  else if (voltage < 4.5)  // between 0.5 and 4.5 now...
  {
    float inHG = mapFloat(voltage, 0.5, 4.5, -29.52, 61.08);    // variation on the Arduino map() function
    Serial.print (raw);
    Serial.print(",  ");
    Serial.print(voltage);
    Serial.print(",    ");
    Serial.println(inHG, 2);  // 2 decimals
    Blynk.virtualWrite(V1,inHG);
  }
  else 
  {
    Serial.println("Signal too high!!");
  }
}

float mapFloat(float x, float in_min, float in_max, float out_min, float out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}



void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth);

  // Setup a function to be called every second
  timer.setInterval(1000L,sensordataSend);
}

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

Success!!!
works perfectly
One quick question though
to view serial monitor with blynk running does the Arduino have to be hooked through usb to a pc?
or is there a way to view it on blynk app?

You could consider using the Blynk Terminal Widget.

Yes and no… The IDE Serial Monitor requires the USB link. But depending on the MCU used, there are a few ways of redirecting Serial output and even Blynk Print/Debug info, back to a Widget display (usually Terminal) or an attached OLED, LCD, even VGA display. They are not straight forward though.

if I use the terminal.print function. do I just replace the serial.print function in the code with terminal.print or do I have to assign a virtual pin to it also the terminal widget has a pin selector option in the widget options

terminal.print() also requires terminal.flush() to confirm the info is sent, so not really a drop in replacement (and of course will only work when actually connected to a server). You can also just use the normal Blynk.virtualWrite() command.

When I use Terminal to show Serial commands when troubleshooting, I usually just duplicate commands like such…

Serial.print("Something happened here\n"); // To Serial Monitor
Blynk.virtualWrite(vPin, "Something happened here\n");  // To Terminal Widget on vPin

Here’s an example,

You’ll specify the same virtual pin as part of the Terminal Widget Settings on the app side and as part of the WidgetTerminal declaration on the device side,

WidgetTerminal terminal(V1);

Thanks,
appreciate the help tonight.
Ill try it out and see how it goes.