Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timer not working in a button press callback. #16

Open
saurabhreigns opened this issue Nov 26, 2016 · 5 comments
Open

Timer not working in a button press callback. #16

saurabhreigns opened this issue Nov 26, 2016 · 5 comments

Comments

@saurabhreigns
Copy link

saurabhreigns commented Nov 26, 2016

Please help me find why the timer does not start when button at A3 is pressed?

//Library
#include "SevSeg.h"
#include <CountUpDownTimer.h>
#include <Button.h>

//create a Button objects at pin a0-a3
// Uses debounce mode. You may need to experiment with the debounce duration
// for your particular switch.
#define ledPin A4
CountUpDownTimer T(DOWN);

Button setPin = Button(A0, BUTTON_PULLUP_INTERNAL, true, 10);
Button plusPin = Button(A1, BUTTON_PULLUP_INTERNAL, true, 10);
Button minusPin = Button(A2, BUTTON_PULLUP_INTERNAL, true, 10);
Button startTimerPin = Button(A3, BUTTON_PULLUP_INTERNAL, true, 10);

int seconds=1234;

SevSeg sevseg; //Instantiate a seven segment controller object

//Variables
  int secvalue =0;
  int milvalue =0;

void setup() {
  byte numDigits = 4;   
  byte digitPins[] = {5,4,3,2,1};
  byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13};

  pinMode(ledPin,OUTPUT);

  sevseg.begin(COMMON_ANODE, numDigits, digitPins, segmentPins);
  sevseg.setBrightness(50);
  //sevseg.setNumber(1234,0);
  //while(1){sevseg.refreshDisplay();};
//      digitalWrite(ledPin,HIGH);//indicator LED test
//      delay(500);
//      digitalWrite(ledPin,LOW);
      }

void loop(){
    //sevseg.setNumber(seconds,0);

    if(setPin.uniquePress())
    {
      seconds=0000;//resetting the timer value
    }
          
    else if(plusPin.uniquePress())
    {
      seconds++;
    }
    
    else if(minusPin.uniquePress())
    {
      seconds--;
    } 
    else if(startTimerPin.uniquePress())
    { 
      T.StartTimer();
      T.SetTimer(seconds);
      T.Timer();
    }
    else{
      sevseg.setNumber(seconds,0);
      sevseg.refreshDisplay();
    }
    
      secvalue=T.ShowSeconds();
      milvalue=T.ShowMilliSeconds();
}

@AndrewMascolo
Copy link
Owner

AndrewMascolo commented Nov 26, 2016 via email

@saurabhreigns
Copy link
Author

It was a typo. i corrected it in the comment itself . Thanks

@AndrewMascolo
Copy link
Owner

AndrewMascolo commented Nov 26, 2016 via email

@saurabhreigns
Copy link
Author

Thanks .The following code works but it appears to be counting 1 sec longer than 1 real second. Also , I want to know how can I display centiseconds or milliseconds displayed. Please help

[code]
#include <CountUpDownTimer.h>
//Library
#include "SevSeg.h"
#include <Button.h>

#define ledPin A4 //indicator led

Button setPin = Button(A0, BUTTON_PULLUP_INTERNAL, true, 10);
Button plusPin = Button(A1, BUTTON_PULLUP_INTERNAL, true, 10);
Button minusPin = Button(A2, BUTTON_PULLUP_INTERNAL, true, 10);
Button startTimerPin = Button(A3, BUTTON_PULLUP_INTERNAL, true, 10);

unsigned long secs = 0;

SevSeg sevseg; //Instantiate a seven segment controller object

//Variables
unsigned long secvalue = 0;
unsigned long decivalue = 0;

CountUpDownTimer T(DOWN,LOW);

void setup() {
byte numDigits = 4;
byte digitPins[] = {5, 4, 3, 2, 1};
byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13};
Serial.begin(9600);
pinMode(ledPin, OUTPUT);

sevseg.begin(COMMON_ANODE, numDigits, digitPins, segmentPins);
sevseg.setBrightness(50);
}

void loop() {
if (setPin.uniquePress())
{
secs = 0000; //resetting the timer value
}

else if (plusPin.uniquePress())
{
secs++;
}

else if (minusPin.uniquePress())
{
secs--;
}
else if (startTimerPin.uniquePress())
{ Serial.println("secs");
T.SetTimer(secs);
T.StartTimer();
T.StopTimerAt(0,0,0);

while (secs>0) // this prevents the time from being constantly shown.
{ T.Timer();
  //Serial.print(T.ShowSeconds());
  Serial.println((secs*1000000)-(T.ShowMilliSeconds()));
  //Serial.println(secvalue);
  secs = T.ShowSeconds();
  decivalue = secs*100; 
  //Serial.println(decivalue);
  sevseg.setNumber(decivalue, 0); //sec argument suppressed the decimal point from being displayed
  digitalWrite(ledPin, LOW);
  sevseg.refreshDisplay();
  //digitalWrite(ledPin,HIGH);delay(100);digitalWrite(ledPin,LOW);delay(50);
}
digitalWrite(ledPin, HIGH); //delay(100);digitalWrite(ledPin,LOW);delay(50);
secs=0;

}
else {
sevseg.setNumber(secs, 0); //sec argument suppressed the decimal point from being displayed
sevseg.refreshDisplay();
//secvalue=T.ShowSeconds();
}
}
[/code]

@AndrewMascolo
Copy link
Owner

AndrewMascolo commented Nov 27, 2016 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants