SPST Spontaneous Action

I'm using arduino code to create a digital gas gauge. Basically a flow sensor in the fuel line emits pulses which have been calibrated to be proportioned to a volume reading. That volume is then subtracted from a constant representing the gas tank's full volume. This "difference" is then displayed on an HT16K33 display. All of that works fine, I am just having difficulty with an on/off switch. Since the circuit runs off of the car battery and needs to stay on constantly so that the difference reading is not lost between power cycles, I want to turn off the display and reduce the mAh draw. (I am aware of EEPROM but its short hardware lifetime will not work for my application, any other non-volatile memory suggestions?) I have an SPST switch connected to Pin 3 and GND, however whenever I flip the switch sometimes the display goes on/off and other times it randomly flashes over constant or irregular time intervals. Very new to code so any debugging is appreciated.

#include "HT16K33.h"

HT16K33 seg(0x70);

uint32_t start, stop;

int sensorPin = 2;
int FullTank = 31.00;
volatile long pulse;
float volume;
float difference;
const int switchPin = 3;
int switchState;

void setup() {
  Serial.begin(115200);

  Wire.begin();
  Wire.setClock(100000);
  seg.begin();

  seg.displayOn();
  Serial.println("displayTest()");
  seg.displayTest(1);
  seg.displayOff();
  delay(1000);
  seg.displayOn();
  seg.displayColon(false);
  seg.setBrightness(5);

  pinMode(sensorPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(sensorPin), increase, RISING);
  pinMode(switchPin, INPUT);
}


void loop() {
  volume = pulse * 0.000101112097171;
  delay(500);
  difference = FullTank  - volume;
  Serial.println(difference);
  seg.displayFloat(difference);
  delay(500);
  
  const bool switchState = digitalRead(switchPin);
  if (digitalRead(switchPin) == LOW) {
    delay(1000);
    seg.displayOn();
  } else {
    delay(1000);
    seg.displayOff();
  }

//  volume = pulse * 0.000101112097171;
//  delay(500);
//  difference = FullTank  - volume;
//  Serial.println(difference);
//  seg.displayFloat(difference);
//  delay(500);
}

void increase () {
  pulse++;
}

I2C FRAM ?


make that INPUT_PULLUP

(using a button library would make it easier to deal with bouncing)

You could use a button telling that power will be turned off. Then data are saved in EEPROM. At power up the EEPROM data are read and used.
Cutting off the display Vcc will cause ghost powering via the signal lines. The result is unpredictable.

You may need to protect against pulse getting updated half way through reading it in this line. Unless you are using an Arduino based on a 32-bit chip.

void loop() {
  unsigned long pulseNow;
  noInterrupts();
  pulseNow = pulse;
  interrupts();
  volume = pulseNow * 0.000101112097171;
1 Like

Why would bouncing be a problem?

What does this do?
const bool switchState = digitalRead(switchPin);

Thanks for the help, the UNO R3 does not come with an internal I2C FRAM right? What module and library would you recommend that would give me the most about of rewrite cycles? What library would you recommend for the button?
Thanks

Could you type up some example code please, I've had difficulty understanding what is needed to get the EEPROM to read and write the "difference" value.

In case OP wants less latency than waiting a full second every time the switch is tested

Yes.

Also, it's not terribly difficult to arrange it so the Arduino knows that power is soon to be lost, giving it enough time to throw some data into EEPROM.

FRAM is good, so it's down to what kind of fun you want to have.

I googled

 arduino write eeprom before loss of power

and suggest to you to do the same. Here's a thread that goes into some detail, see what you think:

and another

The DS1307 is a lousy RTC, but it does make available battery backed RAM of a handful of bytes, totally available for any use you might find for them, yet another kind of fun…

HTH

a7

1 Like

The ones from my Adafruit link are good. (There are also other libraries out there like GitHub - RobTillaart/FRAM_I2C: Arduino library for I2C FRAM and you can get cheap modules from the fat east…)

But as explained above adding a capacitor and power sensing to your arduino is not very difficult so you could use also the EEPROM.

So have you decided to use FRAM?

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