Go Down

Topic: Need help about float function (Read 77 times) previous topic - next topic

Nick209

Hello) I need help! In last line of code I see error:
48: error: 'bar' was not declared in this scope

I know English language very badly!!!!! I understand the error, but I can't fix it.

Code: [Select]

#define CAYENNE_PRINT Serial   // Comment this out to disable prints and save space
#include <CayenneEthernet.h>   // Change this to use a different communication device. See Communications examples.

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "AuthenticationToken";

// Virtual Pin of the widget.
#define VIRTUAL_PIN V1

void setup()
{
  Serial.begin(9600);
  Cayenne.begin(token);
}

void loop()
{
  Cayenne.run();

    // 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
    float bar = mapFloat(voltage, 0.5, 4.5, 0.0, 8.0);    // variation on the Arduino map() 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;
}

// This function is called when the Cayenne widget requests data for the Virtual Pin.
CAYENNE_OUT(VIRTUAL_PIN)
{
  // Read data from the sensor and send it to the virtual channel here.
  // You can write data using virtualWrite or other Cayenne write functions.
  // For example, to send a temperature in Celsius you can use the following:
  // Cayenne.virtualWrite(VIRTUAL_PIN, 25.5, TEMPERATURE, CELSIUS);
   Cayenne.virtualWrite(VIRTUAL_PIN, bar, 2);
}

MarkT

bar is local to the function loop().  Thus its not visible elsewhere.  Perhaps you mean to use a global variable?
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

sterretje

#2
Aug 13, 2017, 02:22 pm Last Edit: Aug 13, 2017, 02:23 pm by sterretje
Variable 'bar' is declared in loop() and hence not known anywhere else. If you make it a global variable, it will solve the problem.

Code: [Select]
...
...
float bar;

void setup()
{
  ...
  ...
}

void loop()
{
  ...
  ...

  // INTERPRET VOLTAGES
  bar = mapFloat(voltage, 0.5, 4.5, 0.0, 8.0);    // variation on the Arduino map() function

}

float mapFloat(float x, float in_min, float in_max, float out_min, float out_max)
{
  ...
  ...
}

CAYENNE_OUT(VIRTUAL_PIN)
{
  ...
  ...
}
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

david_prentice

bar is a local variable with scope in the loop() function.

I do not see how your CAYENNE_OUT() will know anything about the local variable called bar.

Make sure that variables have the correct scope.   The compiler will get the type correct in the process.
And use the correct overloaded method relevant to a f-p variable.

David.

Nick209


Go Up