Button input inhibit during power up for a set period

Hi All,

First time here and my coding is primitive as a first timer using Arduino.

I am building a dual volt meter to monitor a vehicle dual battery system.

I have all the code working and driving the OLED at I2C.

The code does the following:-

a. Reads A0 and A1.
b. Sets up the SSD1306 text and loads variables
c. When powers up a countdown timer starts, after 120 seconds the system shuts down & releases FET gate power.
d. A press during power up, another 120 seconds is added.

The a to d is coded and works using basic code.

Part two:-

To minimise quiescent current consumption I am controlling the whole voltmeter and display power control with a FET .

I want a single button to serve as three functions:-

  1. Turn the volt meter on by controlling the NANO power up, the LED out D13 latches the FET on via 1K and diode. This works.

  2. I am also using the button to control the off time by holding the button for a set loop period and the counter defaults to 0 and the FET latch from LED D13 is released and the system shuts down.

  3. While powering up via the single button, the system starts, however as the button is the power-up/start input, it also increments the initial 120 second counter from 120 to 180 as a single push increments count up by 60 seconds.

Question:-

Is there a way to hold off reading D2 for an initial time directly after power up?

Please excuse the code mess and inefficiencies, this is the first time using Arduino.

Regards

Gerald

/*----------------------------------------------------------------------------------
  Program:      Dual Volt Meter with OLED 0.96" SSD1306 using I2C

  Description:  Reads value on analog input A0 and A1 and calculates
                the voltage assuming that a voltage divider
                network on the pin divides by 11.

  Hardware:     Arduino Uno with voltage divider on A0 and A1.

  Sketch by:     VK3GJM, 6th August 2016

  Saved as:     VK3GJM_Dual_Volt_Meter_V1.11, Ensure version is added to line 23

  ---------------------------------------------------------------------------------*/
//Libraries included

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// OLED display definitions

#define SW_Version 1.11           //Change version here as required

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define Lled 13
#define textsize1 1
#define textsize2 2
#define buttonPin 2

//Analogue measurement definitions

#define NUM_SAMPLES 10            // number of analog samples to take per reading
#define DIV_1 3.1471              //Sets the divider voltage ratio R1/R2, R1 is series, R2 is shunt, use 6 digit for must acurate
#define DIV_2 3.1471              //Sets the divider voltage ratio R1/R2, R1 is series, R2 is shunt, use 6 digit for must acurate
#define VREF  4.9936              //Measure 5V and add value here, use 6 digit for must accurate

//Analogue variables

int sum_1 = 0;                    // sum of samples taken of A0
int sum_2 = 0;                    // sum of samples taken of A1
int counter = 120;                // enter count down time value
int debounce = 0;
int loopcounter = 0;

unsigned char sample_count_1 = 0; // current sample number of A0
unsigned char sample_count_2 = 0; // current sample number of A1
float voltage_1 = 0.0;            // calculated voltage A0
float voltage_2 = 0.0;            // calculated voltage A1
long previousMillis = 0;          // enter minimum count down time
long previousMillis2 = 0;         // enter minimum count down time


void setup()
{

  pinMode(13, OUTPUT);            // sets up pin 13 with LED as indicator
  digitalWrite(13, HIGH);         // sets up pin 13 initially as low
  pinMode(buttonPin, INPUT);      // Sets the pin number the button is connected too
  digitalWrite(buttonPin, HIGH);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3c);  // initialise with the I2C addr 0x3c (for the 128x64)
  display.clearDisplay();         // clears OLED display

  setup_display();                  //Call up fixed display data

}

void loop()
{
  //sets up count down time parameters
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis > 1000)
  {
    if (counter > 0) counter--;
    previousMillis = currentMillis;

    //Serial.println(loopcounter);
    loopcounter = 0;
  }
  // take a number of analog samples and add them up for A0
  while (sample_count_1 < NUM_SAMPLES) {
    sum_1 += analogRead(A0);
    ++sample_count_1;
  }
 // take a number of analog samples and add them up for A0
  while (sample_count_2 < NUM_SAMPLES) {
    sum_2 += analogRead(A1);
    ++sample_count_2;
  }
  // calculate the voltage 
  voltage_1 = ((float)sum_1 / (float)NUM_SAMPLES * VREF) / 1024.0;
  voltage_2 = ((float)sum_2 / (float)NUM_SAMPLES * VREF) / 1024.0;

  if (currentMillis - previousMillis2 > 200)
  {
    previousMillis2 = currentMillis;

    setup_display();                          //Large text only for voltage display for main and aux and V
    display.setTextSize(textsize2);
    display.setTextColor(WHITE);
    display.setCursor(53, 20);
    display.print(voltage_1 * DIV_1, 2);     //Enters main batt voltage
    display.setTextSize(textsize2);
    display.setTextColor(WHITE);
    display.setCursor(53, 37);
    display.print(voltage_2 * DIV_2, 2);     //Enter Aux batt voltage
    display.display();
    display.clearDisplay();
  }

  sample_count_1 = 0;
  sum_1 = 0;
  sample_count_2 = 0;
  sum_2 = 0;

  if (digitalRead(buttonPin) == LOW)
  {
    debounce++;
  }
  else
  {
    debounce = 0;
  }

  if (debounce == 10)
  {
    counter = counter + 60;   //Increment counter by 60 each time button is pressed within power up state.

  }
  if (debounce == 2000)
  {
    counter = 0;
  }
  if (counter == 0) digitalWrite(13, LOW);

  loopcounter++;

}

void setup_display() {                      // adds fixed text to display into specific locations to suit the SSD1306 128 x 64 and project needs.
  display.setTextSize(textsize1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("12-24VDC Dual Battery");
  display.print  ("monitor system: V"); display.print(SW_Version);
  display.drawLine(0, 17, 127, 17, WHITE);
  display.setCursor(0, 20);
  display.setTextSize(textsize2);
  display.setTextColor(WHITE);
  display.print("Main");
  display.setCursor(0, 37);
  display.setTextSize(textsize2);
  display.setTextColor(WHITE);
  display.print("Aux");
  display.setCursor(115, 20);
  display.print("V");
  display.setTextSize(textsize2);
  display.setTextColor(WHITE);
  display.setCursor(115, 37);
  display.print("V");
  display.setCursor(0, 57);
  display.setTextSize(textsize1);
  display.setTextColor(WHITE);
  display.drawLine(0, 53, 127, 53, WHITE);
  display.print("Power off in: ");
  display.print(counter);                   // count down timer, set int value and this counts down, when zero, L LED is on Pin Digital pin 13 goes high.
  display.setCursor(109, 57);
  display.print("Sec");

}

To initially disable the button, I'd use a flag that is set after the initial delay has elapsed, i.e. when millis()>initalDelay. Then check that flag and skip or perform the button reading and processing accordingly.

Hi Dr Diettrich,

Thanks for that. next step figure out how!

Regards

Gerald

vk3gjm:
Thanks for that. next step figure out how!

A flag is just a global (or static) variable that is conditionally set. You essentially just need to wrap your button checking code with an if-else statement which checks the flag.

There are probably plenty of better ways to do it but I would roughly do something like this in the loop()

  static bool initialStartupFlag = true;
  
  if (initialStartupFlag)
  {
    if (millis() > initialStartupDelay)
    {
      initialStartupFlag = false;
    }
  }
  else
  {
    //Put your buttonPin checking code here
  }

All sorted and working. Thank you for the flag tip and code snippet.

Regards

Gerald