Go Down

Topic: Countdown timer while button is held (Read 245 times) previous topic - next topic

logansprung

Hello,

I'm very new to coding and am working on what seems to be a simple project but i'm having trouble.

What i would like is to have a 4 digit 7 seg display be populated with a number is that user defined. Then i need that number to count to zero only if a button is being held (switch is closed). I've got the basic principal working but i'm using my "number" - millis() which i know uses the time since program started making it wrong already.

I've looked at other timers and counters but none seem to accomplish the task I need. I also tried the decrement but wans't able to get it working. Any help is appreciated.

I'm interfacing it with MegunoLink which is what sends my number to the display. My code is below, forgive the noob code;





#include "SevSeg.h"
#include "MegunoLink.h"
#include "CommandHandler.h"

SevSeg sevseg;
CommandHandler<> SerialCommandHandler;

int FuelNumber = 0000.0000;
int p = digitalRead(A3);



  void Cmd_SetFuelNumber(CommandParameter &Parameters) 
{
  FuelNumber=Parameters.NextParameterAsInteger();
}

  void Cmd_ResetFuelNumber(CommandParameter &Parameters) 
{
  FuelNumber=Parameters.NextParameterAsInteger();
}

void setup() {

 
  Serial.begin(115200);   //begin serial comms
  Serial.println(" Welcome To Fuel Number Display ");
  Serial.println(" Code v1.1 ");
  Serial.println(" Display v1.1 ");
  Serial.println("-----------------------------");

  SerialCommandHandler.AddCommand(F("SetFuelNumber"), Cmd_SetFuelNumber);
  SerialCommandHandler.AddCommand(F("ResetFuelNumber"), Cmd_ResetFuelNumber);
   
  byte numDigits = 4;
  byte digitPins[] = {9, 10, 11, 12};
  byte segmentPins[] = {2, 3, 4, 5, 6, 7, 8, 13};


  bool resistorsOnSegments = true;
  bool updateWithDelaysIn = true;
  byte hardwareConfig = COMMON_CATHODE;
  sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
  sevseg.setBrightness(100);



}

void loop() {
  // put your main code here, to run repeatedly:

  int p = digitalRead(A3);

 

   if (p == HIGH)
    {

      sevseg.setNumber(FuelNumber - (millis()/10), 2);
     
    }
 

 
 
  sevseg.refreshDisplay();
  SerialCommandHandler.Process();
  sprintf(FuelNumber,0);



}

Go Up