Search For a Timer Library For Arduino Nano Board to use two timers in A code

in my project pin0,Pin1 are used to Serail communication For MIDI. A4,A5 Are used to Display connection. other can be used for Button input.

i want to perform two different MIDI functions with interval. so i need two timers for arduino nano.
right as below library work on Arduino Due only.

Due Timer post

is there any library Like obove for Aruino Nano to work????/

Thank you

Hello charnjit
Take a view into the BLINKWITHOUTDELAY to be found in the IDE and design a time function by your self.

Have a nice day and enjoy coding in C++.
Дайте миру шанс!

i think you are about use of millis(); function. but i have tried it in this code

#include <Wire.h>
#include <Adafruit_SSD1306.h>   
#include <Adafruit_GFX.h>

#define OLED_ADDR 0x3C 

const int midiClock = 0xf8; // midic clock pulse byte
const int startByte = 0xfa; // midi clock start byte
const int stopByte = 0xFC; // midi clock stop byte
const uint8_t NOTE_OFF = 0x80;


Adafruit_SSD1306 display(-1);
const int plus = A1;
const int reset = A2;
const int save = A3;

int P1 = 0;
int Last_P1=0;
int P2=0;
int Last_P2=0;
int P3=0;
int Last_P3=0;

unsigned long old_millis = 0;
int tempo = 145;

void setup() {

pinMode(plus, INPUT_PULLUP);
pinMode(reset, INPUT_PULLUP);
pinMode(save, INPUT_PULLUP);
Serial.begin(31250);
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
display.clearDisplay();
display.display();
}
void loop () {

unsigned long new_millis = micros();
unsigned long interval = (60000000/(tempo*24));

 P1 = digitalRead(plus);
 P2 = digitalRead(reset);
 P3 = digitalRead(save);

   if (P1 != Last_P1) {
if (P1 == LOW) 
{    tempo++;       screen  (); } 
else
{     }
Last_P1 = P1;
}

// --------------------------------------------------------------------------
if (P2 != Last_P2) {
if (P2 == LOW) { tempo--;  screen (); }
else { }
Last_P2 = P2;
}
//---------------------------------------------------------------------------
if (P3 != Last_P3) {
if (P3 == LOW) { tempo = 100;   screen (); }
else { }
Last_P3 = P3;
}

if (new_millis - old_millis >= interval) {  old_millis = new_millis;    Serial.write(midiClock);  }     //     send midi clock every interval
}

   void screen  ()   {    
display.clearDisplay();
display.setTextSize(4);
display.setTextColor(WHITE);
display.setCursor(1, 1);
display.print(tempo);
display.display();
}

as in above code , when Midi clock is increased or decreased, the Running Rhythm beat or hold key sound's tempo is cracked badly with ultra low tempo.
Now am going to follow this example using two timers timer1 & Timer2.
Arduini Due timer
but Timer Due library for only Arduino Due board
And i am searching for two timer Library For arduino nano board
please suggust me or give hint to make (arduino nano midi clock sync device) . if possible ??????

There are several hardware timer interrupt libraries you might find useful

sorry,,,
i can not understand large coding of timer (TimerInterrupt:) for nano
please told about a such timer library inwhich can be written very simple in as in below example

#include <????????????????.h>

void setup()
{
  Timer1.initialize(150000);
  Timer2.initialize(300000);
  Timer1.attachInterrupt(printing_T1); 
  Timer2.attachInterrupt(printing_T2);
  Serial.begin(9600);
Timer1.start();
Timer2.start();
}
void loop ()   {      
if    (happen something)   { 
      Timer1.stop();
      do something;
     Timer1.start();
     }
void printing_T1  ()  {  Serial.println("Timer1 Printed"); }
void printing_T2  ()  {  Serial.println("Timer2 Printed");  }

Hello
Take a search engine of your choice and ask the WWW for a tutorial.
Have a nice day and enjoy coding in C++.
Дайте миру шанс!

1 Like

You can not have serial printing from within a Timer Interrupt isr. Have the isr set a flag and print/clear the flag in loop().

Rather than trying to use the more complicated generic library, I would use the TimerOne and the msTimer2 libraries. Both libraries have examples for periodic interrupts.

1 Like

thank you
can i use both library as i wrote below

#include "TimerOne.h"
#include <MsTimer2.h>

void setup()
{
  Timer1.initialize(500000);         // initialize timer1, and set a 1/2 second period
  Timer1.attachInterrupt(callback);  // attaches callback() as a timer overflow interrupt
  MsTimer2::set(500, flash); // 500ms period
  MsTimer2::start();
}
void loop()
{
  // your program here...
if (??? ==????)     { Timer1.start(); }
if (??? ==????)     { MsTimer2.start(); }
if (??? ==????)     { Timer1.stop(); }
}
void callback ()   {    something1 about Serial.write  }
void flash ()   {    something2  about read button state }

can i use both library as i wrote below

Looks about right (except for the Serial.write() in the ISR) but what happens when you try it?

The Serial.write() command does not execute from within the ISR and will happen after the code exits from the ISR. It's not clear to me that the situation you saw with the millis() timer will not happen as well when using the timer interrupt ISR.

Therefore, I have doubts that the timer interrupt version of code will work better than the millis() timer version in Post #3.
The linked Due code did not have the oled display, and I think it is the blocking aspects of the display which disrupt the midi performance.

1 Like

Instead of understanding timer interrupt complicated coding, can I use millis() to make a pocket midi clock sync device from Nano board+rotary encoder or buttons

If yes , why it not working in post#3.
How will it work?? What part code

If no, what suggestion after??

Try that code with Serial monitor output instead of the OLED display.

For faster output, use Serial.begin(115200) in the code and monitor settings.

when Midi clock is increased or decreased, the Running Rhythm beat or hold key sound's tempo is cracked badly with ultra low tempo.

Can you explain more about the problems you saw.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.