Arduino nano - TimerOne.h

Hi,

I'm trying to work with the TimerOne library (Arduino Playground - Timer1) but there are some instructions that doesn't work. I hope you can give me a hint :slight_smile:

The serial port is used as a debugger and it's toggling all the time between cases 1 and 2 even though I don't press the buttom. In other words, the program is entering in the ISRs all the time and I don't have any remote idea why.

Thank you, coummunity.

// Every time the pushbuttom is pressed, the LED should turn on during 3 seconds
// After this time, the ISR of the timer turn off the LED.

#include <TimerOne.h>           // http://playground.arduino.cc/Code/Timer1


//Variables
const int pushButton = 2;       // digital pin 2 has a pushbutton attached to it. Give it a name:
volatile int ledPin = 13;       // select the pin for the LED
volatile int writeSerial = 0;

void setup(void)
{
  pinMode(pushButton, INPUT);                 // make the pushbutton's pin an input:
  pinMode(ledPin, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(pushButton),turnon_LED,FALLING);
  Timer1.initialize(3000000);                 // timer = 3 seconds
  Timer1.stop();                              //stop the counter
  Timer1.restart();                           //set the clock to zero
  Timer1.attachInterrupt(ISR_turnoff_LED);    // ISR to turn off the led
  Serial.begin(9600);
}

void ISR_turnoff_LED()
{
  digitalWrite(ledPin, LOW);
  Timer1.stop();                            //stop the counter
  Timer1.restart();                         //set the clock to zero
  writeSerial = 1;                          // flag for the serial port
}

void turnon_LED()
{
  delay(10);
  Timer1.restart();                         //set our stopwatch to 0
  Timer1.start();                           //and start it up
  digitalWrite(ledPin, HIGH);
  //Timer1.initialize(3000000);
  writeSerial = 2;                          // flag
}

// the loop function runs over and over again forever
void loop() {
  switch(writeSerial)       // simulating debugger
  {
    case 0:
      {
          Serial.print("Waiting for first interaction\n");
          delay(1);        // delay in between reads for stability
      }
      case 1:
      {
          Serial.print("led off\n");
          delay(1);        // delay in between reads for stability
      }
      case 2:
      {
          Serial.print("led on\n");
          delay(1);        // delay in between reads for stability
      }
  } // end of switch

}

What is wired to pin 2?

Hi AWOL.

It is a pushbuttom. Every time the pushbuttom is pressed, the LED should turn on during 3 seconds. After this time, the ISR of the timer turn off the LED.

Just a pushbutton?
No pull-up or pulldown resistor?

Yes, an external pull-up resistor is also wired.
Sorry I don't reply quickly but I have a 1 message/5min limitation.

Anybody else had problems with the TimerOne lib and arduino nano? Please.

If I have understood your post, your algorithm stands as:

1. Initialise everything as needed.
1. Keep polling button of DPin-2 for close condition
2. Ignite LED at DPin-13 and continue for 3-sec; start Timer1.
3. In the ISR(TIMER1_OVF_vect) sub routine, extinguish the LED

I have tried to compile and upload your codes of OP in my NANO; unfortunately, I could not compile them -- errors. As I have not understood the logic of your codes, I did not proceed to debug them.

I have created, compiled, and uploaded the following register level instructions to implement the above-mentioned algorithm, and they appeared as working!

void setup() 
{
  pinMode(2, INPUT_PULLUP);  //internal pull-up
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);

  TCCR1A = 0x00;  //TC1 in normal counting mode
  TCCR1B = 0x00;   //TC1 is OFF
  TCNT1 = 0x48E5;  //pre-set value for 3-sec time delay = 
  TIMSK1 = 0x01;   //TC1's interrupt logic is enabled
  interrupts();
  
  while (digitalRead(2) != LOW)  //polling button for closed condition
    ;
  digitalWrite(13, HIGH);  //LED is ON for 3-sec
  TCCR1B = 0x05;     //start TC1 at clkTC1 = clcSYS/1024
}

void loop() 
{
  // wait for interrupt/looping for nothing

}

ISR(TIMER1_OVF_vect) //ISR for Timer1 Overflow
{
  digitalWrite(13, LOW);  //3-wsec gone; LED is OFF/goto loop()
}

Thanks, GolamMostafa.

What I'm trying to do is just to light an LED during 3 seconds when a pushbuttom is pressed. If the LED is on and you press again the buttom, the timer should restart from zero. Moreover, I tried to use the timerone.h library in order to ease things for my students... but it seems the instructions Timer1.Start() or Timer1.Stop() don't work with arduino nano.

You did it by using the registers. It works only the first time you push the buttom and not anymore.

I'll keep trying using preset libraries. Thanks for your work, though. It's opened my eyes.