Electronic load Timelib.h question

Hello, I am trying to make an electronic load, but I got stuck at the clock, so basically I want the clock to only run when current is flowing, so I could see how long it took to discharge a battery for example. I tried to set the clock to 0 when there is no current flowing, but the problem is that when the battery will give 0 amps then the clock will reset, how could I fix that?

#include <Wire.h>
#include <TimeLib.h>
#include <DallasTemperature.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define ONE_WIRE_BUS 12

OneWire oneWire(ONE_WIRE_BUS);  
DallasTemperature sensors(&oneWire);

float totalCharge;
float averageAmps;
float ampSeconds;
float ampHours;
float miliampHours;
float wattHours;
float capacity;
float low_volt;
float current;
float voltage;
float volt;
float curr;
float temp;
float msec;
float time1;

int sample;
int value;
int fanPin=3;
int freq=2000;
int voltPin=A3;
int currPin=A2;
int powerPin=11;
int buzzerPin=2;
int button1_state;
int buttonPin1=5;
int button2_state;
int buttonPin2=6;
int button3_state;
int buttonPin3=7;
int button4_state;
int buttonPin4=8;
int button5_state;
int buttonPin5=9;
int button6_state;
int buttonPin6=10;



Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {

pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
pinMode(buttonPin6, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(powerPin, OUTPUT);
pinMode(fanPin, OUTPUT);
pinMode(voltPin, INPUT); 
pinMode(currPin, INPUT);
  
sensors.begin();
sensors.setResolution(9); 
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

}

void printDigits(int digits){

  display.print(":");
  if(digits < 10)
    display.print('0');
  display.print(digits);
  
}

void loop() { 
  
  button1_state = digitalRead(buttonPin1);
  button2_state = digitalRead(buttonPin2);
  button3_state = digitalRead(buttonPin3);
  button4_state = digitalRead(buttonPin4);
  button5_state = digitalRead(buttonPin5);
  button6_state = digitalRead(buttonPin6);
  
  sensors.requestTemperatures();
  temp = sensors.getTempCByIndex(0);
  
  int oled =digitalRead(10);

  if (temp>50)
  {
    digitalWrite(fanPin,HIGH);
  }
  if (temp<40)
  {
    digitalWrite(fanPin,LOW); 
  }
  
  if (oled==0){
    display.ssd1306_command(SSD1306_DISPLAYOFF);
    digitalWrite(fanPin,HIGH);
  }
  
  else
  
  {
    display.ssd1306_command(SSD1306_DISPLAYON);
    digitalWrite(fanPin,LOW);
  }
  
  if(button1_state!=0){
    low_volt = low_volt+0.1;
  }
  
  if(button2_state!=0){
    low_volt = low_volt-0.1;
  }
  
  if(button3_state!=0){
    low_volt = low_volt+1;
  }
  
  if(button4_state!=0){
    low_volt = low_volt-1;
  }
  
  if(button5_state!=0){
  digitalWrite(powerPin,LOW);
  }
  
  if(button6_state!=0){
  digitalWrite(powerPin,HIGH);
  }

  if(low_volt>volt;value=0){
    digitalWrite(powerPin,LOW);
     tone(buzzerPin,freq);
    delay(100);
    noTone(buzzerPin);
    delay(100);
    tone(buzzerPin,freq);
    delay(100);
    noTone(buzzerPin);
    delay(100);
    tone(buzzerPin,freq);
    delay(100);
    noTone(buzzerPin);
    value = value+1;
    }




    
  if(curr==0){
    setTime(0);
  }




  
  int v_sense = analogRead(voltPin);
  float voltage = map(v_sense,0,1024,0,4720);
  volt = voltage/125;
  
  int c_sense = analogRead(currPin);
  float current = map(c_sense,0,1024,0,4721);
  curr = current/690*1.69;
  float watts = volt*curr;
  sample = sample + 1;
  msec = millis();
  time1 = msec / 1000.0;
  totalCharge = totalCharge + curr;
  averageAmps = totalCharge / sample;
  ampSeconds = averageAmps*time1;
  ampHours = ampSeconds/3600;
  miliampHours = ampHours*1000;
  
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print("Voltage: ");
  display.print(volt);
  display.println(" V");
  display.print("Current: ");
  display.print(curr);
  display.println(" A");
  display.print("Power:   ");
  display.print(watts);
  display.println(" W");
  display.print("Capacity:");
  display.print(miliampHours);
  display.println(" mAh");
  display.print("Energy:  ");
  display.print(wattHours);
  display.println(" Wh");
  display.print("Low volt:");
  display.print(low_volt);
  display.println(" V");
  display.print("Temp:    ");
  display.print(temp);
  display.println(" C");
  display.print("Time:    0");
  display.print(hour());
  printDigits(minute());
  printDigits(second());
  display.display();
delay(250);
}
  if(low_volt>volt;value=0){

is probably not what you meant. If you meant "and" you can say

  if(low_volt>volt and value==0){

Notice the extra '=', your '=' is an assignment statement in C, "==" is the comparison for equality.

I suggest you enable all compiler errors in the IDE settings tab, and pay attention to what they tell you.

Yes I meant that thank you, do you suggest any other libraries for measuring time or could I stop the time with Timelib somehow?

You never need to stop time. To 'start' an interval, just store the current time. The length of any interval, is just the time at the end of that interval, minus the start time you saved.

Is this ok? It seems to be working

  if(curr!=0){
 setTime(time2); 
  }
  else{
    int time2=now();
  }

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.