AndrewMascolo / CountUpDownTimer

MIT License
28 stars 20 forks source link

Timer not working in a button press callback. #16

Open saurabhreigns opened 7 years ago

saurabhreigns commented 7 years ago

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 commented 7 years ago

How can it? A4 is set to an output.

Sent from my iPhone

On Nov 26, 2016, at 11:41 AM, Saurabh Srivastava notifications@github.com wrote:

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

//Library

include "SevSeg.h"

include

include

//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();

}

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

saurabhreigns commented 7 years ago

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

AndrewMascolo commented 7 years ago

T.Timer() needs to be outside that else if statement, otherwise it will not count down.

Sent from my iPhone

On Nov 26, 2016, at 4:44 PM, Saurabh Srivastava notifications@github.com wrote:

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

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

saurabhreigns commented 7 years ago

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

//Library

include "SevSeg.h"

include

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 commented 7 years ago

Subtract 1 from Sec when you set the timer and multiply the milliseconds by 10 to give centiseconds

Sent from my iPhone

On Nov 27, 2016, at 11:42 AM, Saurabh Srivastava notifications@github.com wrote:

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

//Library

include "SevSeg.h"

include

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((secs1000000)-(T.ShowMilliSeconds())); //Serial.println(secvalue); secs = T.ShowSeconds(); decivalue = secs100; //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]

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.