I can't get frac to display anything to the right of the decimal point. I am a total newb so may have int & float not working together correctly.
This is my first attempt at at using my own equations rather than using other peoples's sketch and it is not going well, what the frac...lol.
Hardware: Arduino Uno R3, Adafruit standard 20X4 lcd with i2c backpack, Adafruit RTC.
In the sketch attached the equation is (219*17)/4=930.75. Instead I get 930.0... I can't get the value to the right of the decimal to display correctly. I have tried using other values for rw, dist and acre and always get erroneous info to the right of the decimal point. The int, the number to the left of the decimal point is always correct????
Have used frac in a flow meter application with success but can't get frac to work with my equation here.
I have commented out the RTC code with // and there is no difference. I have left in my comments behind // throughout the sketch.
I'm sure it is an obvious error, but not being bothered by much knowledge of C or C++ I can't see it.
Here is my pitiful sketch:
// include the library code:
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(0);
RTC_DS1307 rtc;
int rw = 219;
int dist = 17;
int acre = 4;
float k;
unsigned int frac;
void setup() {
lcd.begin(20, 4);
delay(10);
lcd.setBacklight(HIGH);
Serial.begin(57600); // changing this doesn't seem to matter 9600 or 57600
}
void loop() {
DateTime now = rtc.now();
lcd.setCursor(15, 0);
delay(10);
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
delay(10);
k = AcreageCalculation(rw, dist, acre); // when this is // out then Acre=0.00
lcd.setCursor(0, 2);
delay(10);
lcd.print("Acre");
lcd.print("=");
lcd.print(int(k), DEC); //removing the DEC has no effect, here or two lines below
lcd.print("."); // without this whole number only
frac = (k - int(k)) * 10; // without this whole number only int.0 no matter if 10 or 1000
lcd.print(frac, DEC); // without this whole number only
//lcd.print(k, 2); // thes give int.00 if 2 or .000 if 3 but no remainder
delay (10);
}
float AcreageCalculation(int rw, int dist, int acre){
float result;
result = (rw * dist / acre);
return result; // displays 930.0 instead of 930.75
}
Thanks in advance for any and all help.