Timer.h Library printing 3 times

Hey there,

Below is the code for my Arduino which I will use to control an ultrasonic cleaner. The problem is when I press the button it is supposed to count down in seconds the remaining time and print that to the serial monitor, function BlinkLedTimeRemaining. An example of the output it is currently outputting is as follows

3719
3718
3717

3716
3715
3714

3713
3712
3711

I’ve added spaces to show that every second it subtracts 1 from the timeRemains and prints it, however it does this 3 times every second when it should be doing it one time every second… I’ve looked at my code for around 2 hours and cannot fathom WHY it would repeat this 3 times.

If anyone has had experience with this library, and can help, please do!

#include <Timer.h>


Timer t;
long userTime;
int timeRemaining = 0;
int running = 0;
long sensorValue = analogRead(A0 / 16.4);


const int buttonPin = 2;
int bluePin = 4;
int redPin = 3;
int greenPin = 5;

int currPin = 0;

int ultrasonicRelay = 6;

int buttonState = 0;

void setup() {
  // set up the LCD's number of columns and rows:
  pinMode(redPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  pinMode(ultrasonicRelay, OUTPUT);
  Serial.begin(9600);


}


void loop()
{
buttonState = digitalRead(buttonPin);
  t.update();

  sensorValue = analogRead(A0) / 16.4;


  //Serial.println(sensorValue); 
 // Serial.println(timeRemaining);

  
 

 
if (buttonState == HIGH) {
    // turn LED on:
    userTime = sensorValue * 60;
    timeRemaining = userTime; // time remaining in seconds

    runUltrasonic();
    running = 1;
  }

  
  if(running == 1)
  {
  digitalWrite(redPin, HIGH);
  digitalWrite(greenPin, LOW);
  digitalWrite(bluePin, LOW);
  
  }
  else
  {
    ledColourTimeValue();
  }

}


void runUltrasonic()
{

      
       t.pulseImmediate(ultrasonicRelay, userTime * 1000 * 60, HIGH);   //Turns on the Ultrasonic Cleaner Relay for userTime minutes
       t.after(userTime * 1000 * 60, doAfter);                          // After the userTime runs out, turns off the Ultrasonic cleaner and sets running to 0
       t.every(1000, blinkLedTimeRemaining, userTime * 60);             //Updates the LED to show time remaining

}


void ledColourTimeValue()
{
   if(sensorValue >= 0 && sensorValue <= 20)
{
  digitalWrite(greenPin, HIGH);
}
else
{
  digitalWrite(greenPin, LOW);
}
if(sensorValue >= 21 && sensorValue <= 40)
{
  digitalWrite(bluePin, HIGH);
}
else
{
  digitalWrite(bluePin, LOW);
}
if(sensorValue >= 41)
{
  digitalWrite(redPin, HIGH);
}
else
{
  digitalWrite(redPin, LOW);
}



}


void blinkLedTimeRemaining()
{

if (timeRemaining >=41 && timeRemaining <= 65)
{
  currPin = redPin;
  digitalWrite(redPin, HIGH);
}
else
{
  digitalWrite(redPin, LOW);
}

if (timeRemaining >=21 && timeRemaining <= 40)
{
  currPin = bluePin;
  digitalWrite(bluePin, HIGH);
}
else
{
  digitalWrite(bluePin, LOW);
}

if (timeRemaining >=0 && timeRemaining <= 20)
{
  currPin = greenPin;
  digitalWrite(greenPin, HIGH);
}
else
{
  digitalWrite(greenPin, LOW);
}



timeRemaining--;
Serial.println(timeRemaining);


}


void doAfter()
{
  digitalWrite(ultrasonicRelay, LOW);
  running = 0;
  //timeRemaining = 0;
}

I bet you're falling victim to integer math rolling over when you set up your timers. In the three lines in runUltrasonic function, change all the 1000 and 60 to 1000ul and 60ul and see if it behaves better. If not you'll have to wait for someone else who knows that library. I've never used any of the timing libraries. I just write my own timing code with millis so stuff isn't hiding from me behind OO and I can see when something goes wrong.

Mostly of the Arduino libraries just hide the inner things, timer is one of them.. It can appear easier when you begin, but on the long run you will bennefit alot from doing the maximum you can without libraries (except from core libraries of course). And you don't really need it since you can program the timer directly (if it's really a timer that you need) or you can easily use millis.

On your specific case I don't see the need to you even be using a timer, since you want things done once per second.

Have a look at the Blink Without Delay tutorial, it's a good start point at using millis...