Help? I know this has to be something simple, but I'm struggling to figure out what I changed without realizing that I changed it.
My mapped value seems to not be working? It is only returning '0', and not the expected mapped values. Would it be possible to get a second set of eyes, as I'm sure it's something quite simple that my less knowledgeable eyes are missing.
int sensorPin = A0; // select the input pin for the pressure transducer
int sensorValue; // variable to store the value coming from the sensor
int val; // variable to store mapped value
int percent; // variable to store calculated percentage
float analogVoltage; //variabe to store calculated analog Voltage
// This function creates the timer object. It's part of Blynk library
BlynkTimer timer;
void myTimer()
{
// This function describes what will happen with each timer tick
// e.g. writing sensor value to datastream VX
Blynk.virtualWrite(V1, val);
Blynk.virtualWrite(V2, analogVoltage);
Blynk.virtualWrite(V4, percent);
}
void setup() {
// Debug console. Make sure you have the same baud rate selected in your serial monitor
Serial.begin(115200);
while (!Serial)
pinMode(LED_PIN, OUTPUT);
delay(100);
BlynkEdgent.begin();
// send data to Blynk every 60 minutes (3600 seconds)
timer.setInterval(3600L, myTimer);
}
void loop() {
// turn the LED_PIN on so that we know loop is running
digitalWrite(LED_PIN, HIGH);
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// convert sensorValue to analog number
analogVoltage = sensorValue * 3.3 / 1023;
/* Map an analog value to 8 bits (0 to 255) */
// map(value, fromLow, fromHigh, toLow, toHigh)
val = map(analogVoltage, 0.48, 1.09, 0, 3000);
// calculate % full
percent = ((val / 3000) * 100);
// log Blynk event for various cistern water levels
// trigger alert when cistern water volume is below 750 gallons
if (val < 750)
{
Blynk.logEvent("cistern_water_volume_low", String("Cistern less than 1/4 full: ") + val);
}
// trigger alert when cistern water volume is around 1500 gallons
if (val > 1485 && val < 1515)
{
Blynk.logEvent("cistern_water_volume_1500", String("Cistern roughly 1/2 full: ") + val);
}
// trigger alert when cistern water volume is around 2250 gallons
if (val > 2235 && val < 2265)
{
Blynk.logEvent("cistern_water_volume_2250", String("Cistern rough;y 3/4 full: ") + val);
}
// trigger alert when cistern water volume is over 2950 gallons
if (val > 2950)
{
Blynk.logEvent("cistern_water_volume_high", String("Cistern over full: ") + val);
}
// print data in the serial monitor for debugging
Serial.print("Blynk Virtal Pin V2 value is: ");
Serial.println(sensorValue);
Serial.print("Voltage Reading: ");
Serial.print(analogVoltage);
Serial.println(" V");
Serial.print("Gallons in cistern: ");
Serial.print(val, 1);
Serial.println(" gal");
Serial.print("Percent full: ");
Serial.print(percent, 1);
Serial.println(" %");
// turn the LED_PIN off:
digitalWrite(LED_PIN, LOW);
// run Blynk libraries
BlynkEdgent.run();
timer.run();
//delay run for 10 seconds
delay(10000);
}
The map() function uses integer math so will not generate fractions, when the math might indicate that it should do so. Fractional remainders are truncated, and are not rounded or averaged
In reading the other responses where it is suggested that I change val from an int to a float, I can see how that would be necessary since I'm introducing a decimal with my analogVoltage math (3.3). Since I could still get a decimal place, even by switching to millivolts, I'm guessing that I would still need to use float rather than int, correct?
Thanks for the feedback. I appreciate the opportunity to learn.
You don't need to change the declaration of val from an int to a float unless you really want to. The output of map() is always a long, no matter what the inputs are, since they will be cast to longs as well.
My suggestion of map(analogVoltage*1000, 480, 1090, 0, 3000) multiplies the first three terms by 1000 before they get converted into longs as inputs into map.
If analogVoltage was 3.33333, then analogVoltage*1000=3333.33 which then gets truncated like long(3333.33)=3333 before map() sees it and produces map(3333.33, 480, 1090, 0, 3000)=14031.
If you did change the declaration, you'll get integer reulst in your float, like 14031.00
See if this helps:
The map() function in Arduino is used to re-map an integer number from one range to another. It is especially useful when you need to convert sensor readings, scale values, or translate input data from one range of values to another.
Syntax:
map(value, fromLow, fromHigh, toLow, toHigh)
Parameters:
value: The number to map.
fromLow: The lower bound of the value’s current range.
fromHigh: The upper bound of the value’s current range.
toLow: The lower bound of the value’s target range.
toHigh: The upper bound of the value’s target range.
How It Works:
The map() function uses a linear transformation formula to scale the input value from its original range to the desired range. Here’s the underlying formula:
Suppose you have a sensor that provides values between 0 and 1023, and you want to scale these values to a range of 0 to 100:
int sensorValue = analogRead(A0); // Read sensor value (0-1023)
int mappedValue = map(sensorValue, 0, 1023, 0, 100); // Map it to 0-100 range
Serial.println(mappedValue); // Print the mapped value
Important Notes:
The map() function does not constrain the output within the toLow and toHigh bounds, so if the input value is outside the fromLow to fromHigh range, the output will also be outside the toLow to toHigh range.
If you need to ensure the mapped value stays within the bounds, you should use the constrain() function to limit the results.
This function is great for scaling values and is commonly used in various Arduino projects to adjust sensor readings, control outputs, or match data ranges between components.
This was also a valuable response. I did some digging to try and understand this better, and I came across this post on Blynk. I'm including it here in the event someone else comes along and could benefit from the information: https://community.blynk.cc/t/using-blynktimer-or-simpletimer/53326