Go Down

Topic: showing time + kWh not working properly (Read 183 times) previous topic - next topic

bogdan666

I have a little problem with a piece of code that is not showing me what i want. everything works fine except for the elapsed time, witch for some reason when the minutes is 59 and has to go to 1:00, it actually goes to 1:09, and my w/hour works, as long as i don't change the Voltage and amps, if after let's say, i used 100w in one hour, i set my A to 0, the total Wh goes to 0, but it should stay to 100, so something is missing. Here is what my lcd looks like, and the piece of code.

Code: [Select]
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
#include <Adafruit_ADS1015.h>
Adafruit_ADS1115 ads(0x4B);
Adafruit_ADS1115 adsa(0x4A);

float V1 = 0.0;
float V2 = 0.0;
float V3 = 0.0;
float V4 = 0.0;
float V5 = 0.0;
float V6 = 0.0;


void setup(void)
{
  lcd.init();
  lcd.backlight();
  Serial.begin(9600);
  ads.begin();
  adsa.begin();
}
unsigned long startMillis;
unsigned long currentMillis;
unsigned long elapsedMillis;

void loop(void)
{
  int16_t adc1;
  adc1 = ads.readADC_SingleEnded(1);
  V1 = (adc1 * 0.0001875) * 6;
  int16_t adc2;
  adc2 = ads.readADC_SingleEnded(2);
  V2 = (adc2 * 0.0001875) * 6;
  int16_t adc3;
  adc3 = ads.readADC_SingleEnded(3);
  V3 = (adc3 * 0.0001875) * 6;
  int16_t adc4;
  adc4 = adsa.readADC_SingleEnded(1);
  V4 = (adc4 * 0.0001875) * 10;
  int16_t adc5;
  adc5 = adsa.readADC_SingleEnded(2);
  V5 = (adc5 * 0.0001875) * 10;
  int16_t adc6;
  adc6 = adsa.readADC_SingleEnded(3);
  V6 = (adc6 * 0.0001875) * 10;

  currentMillis = millis();
  elapsedMillis = (currentMillis - startMillis);
  unsigned long SS = (elapsedMillis / 1000) % 60;
  unsigned long MM = (elapsedMillis / (60000)) % 60;
  unsigned long HH = (elapsedMillis / (3600000));

  lcd.setCursor(0, 0);
  lcd.print(V1, 2);
  lcd.setCursor(5, 0);
  lcd.print("V");
  lcd.setCursor(7, 0);
  lcd.print(V2, 2);
  lcd.setCursor(12, 0);
  lcd.print("V");
  lcd.setCursor(14, 0);
  lcd.print(V3, 2);
  lcd.setCursor(19, 0);
  lcd.print("V");

  lcd.setCursor(0, 1);
  lcd.print(V4, 2);
  lcd.setCursor(5, 1);
  lcd.print("A");
  lcd.setCursor(7, 1);
  lcd.print(V5, 2);
  lcd.setCursor(12, 1);
  lcd.print("A");
  lcd.setCursor(14, 1);
  lcd.print(V6, 2);
  lcd.setCursor(19, 1);
  lcd.print("A");

  lcd.setCursor(0, 2);
  lcd.print(V1 * V4, 1);
  lcd.setCursor(5, 2);
  lcd.print("W");
  lcd.setCursor(7, 2);
  lcd.print(V2 * V5, 1);
  lcd.setCursor(12, 2);
  lcd.print("W");
  lcd.setCursor(14, 2);
  lcd.print(V3 * V6, 1);
  lcd.setCursor(19, 2);
  lcd.print("W");

  lcd.setCursor(0, 3);
  lcd.print((((V1 * V4) + (V2 * V5)) * SS) / 60 , 0);
  lcd.setCursor(7, 3);
  lcd.print(((V3 * V6) * SS) / 60, 0);
  lcd.setCursor(14, 3);
  lcd.print(HH);
  lcd.setCursor(17, 3);
  lcd.print(":");
  lcd.setCursor(18, 3);
  lcd.print(MM);

}

TheMemberFormerlyKnownAsAWOL

Quote
when the minutes is 59 and has to go to 1:00, it actually goes to 1:09,
You need to print your own leading zeroes.
Please don't PM technical questions - post them on the forum, then everyone benefits/suffers equally

bogdan666

Actually, while doing some more digging, i found out the problem for the time, i don't use lcd.clear so the "9" is left there from 59. But using lcd.clear is causing unwanted flicker... so, back to digging. if i can make it to show minutes like 01, 02, 03... problem solved. and what about the Wh not adding up ?

TheMemberFormerlyKnownAsAWOL

So, don't use clear, just print all the digits of the number, even if they are zero.

You code would benefit from a dose of arrays.
Please don't PM technical questions - post them on the forum, then everyone benefits/suffers equally

bogdan666

i fixed the problem using  lcd.print(" "); just before showing MM, so it clears only the last digit and it works. My code is collected from here and there and i am happy that it works....you lost me at arrays.

bogdan666

I am thinking that it would be easier to just write the current W + the W 10ms ago(if that is possible), and then repeat, and since the W 10ms ago will always have the total power because you keep adding to it, even if i add 0 it would just stay at the total consumption level. Is there a code to help me with this ?

bogdan666

energy = energy += V * A * 1/360000;

this was the answer for everyone who will read this topic. Thanks.

Go Up