UTFT variable displays not updating

I just posted this in general forum but this may be more appropriate location.

Hi all,

I am having an issue with displaying different outputs on a TFT. I’m measuring the speed of a motor using an encoder with 1000 pulses per revolution. I am capturing this data using an interrupt and trying to display the speed on a 480*320 tft using the UTFT and UTOUCH libraries. I am hoping to be able to toggle between different displays (rads/rps/RPM) when the user touches appropriate button on touch-screen.

The problem I’m having is that when a button is touched it brings up the value I want but then it does not update until the button is retouched. Is there anyway for the variable to update by itself.
I’m quite a noob so I know the code is prob a mess. Any and all advice will be received graciously.

#include <UTFT.h>
#include <UTouch.h>

// period of pulse accumulation and serial output, milliseconds
#define MainPeriod 500
long previousMillis = 0; // will store last time of the cycle end
volatile unsigned long duration = 0; // accumulates pulse width
volatile unsigned int pulsecount = 0;
volatile unsigned long previousMicros = 0;

int x, y;

// Declare which fonts we will be using
extern uint8_t SmallFont[];
//extern uint8_t SevenSegNumFont[];
extern uint8_t SevenSeg_XXXL_Num[];
extern uint8_t BigFont[];
extern uint8_t Inconsola[];

// Set the pins to the correct ones for the shield
// ------------------------------------------------------------
// The model parameters for display & touch module

UTFT myGLCD(ILI9481, 38, 39, 40, 41);
UTouch  myTouch( 6, 5, 4, 3, 2);

void drawButtons()
{
  // Draw the buttons to choose the display type
  for (int x = 0; x < 5; x++)
  {
    myGLCD.setColor(VGA_YELLOW);
    myGLCD.fillRoundRect (10 + (x * 94), 210, 94 + (x * 94), 260);
    myGLCD.setColor(VGA_WHITE);
    myGLCD.drawRoundRect (10 + (x * 94), 210, 94 + (x * 94), 260);
    myGLCD.drawRoundRect (9 + (x * 94), 209, 95 + (x * 94), 261);
  }
  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(VGA_YELLOW);
  myGLCD.setColor(VGA_BLUE);
  myGLCD.print("RPM", 30 + (0 * 94), 227);
  myGLCD.print("Hz", 36 + (1 * 94), 227);
  myGLCD.print("rads", 13 + (2 * 94), 227);
  myGLCD.print("Tach", 23 + (3 * 94), 227);
  myGLCD.print("Torq", 23 + (4 * 94), 227);
  myGLCD.setFont(SmallFont);
  myGLCD.print("-1", 77 + (2 * 94), 224);
}


// Draw a coloured frame while a button is touched
void ButtonFrame(int xA1, int yA1, int xA2, int yA2, int xB1, int yB1, int xB2, int yB2)
{
  for (int i = 0; i < 5; i++)
  {
    myGLCD.setColor(VGA_WHITE);
    myGLCD.drawRoundRect (10 + (i * 94), 210, 94 + (i * 94), 260);
    myGLCD.drawRoundRect (9 + (i * 94), 209, 95 + (i * 94), 261);
  }

  myGLCD.setColor(0, 255, 255);
  myGLCD.drawRoundRect (xA1, yA1, xA2, yA2);
  myGLCD.drawRoundRect (xB1, yB1, xB2, yB2);

}



void setup()
{
  //int sec=millis()/1000;

  randomSeed(analogRead(0));
  // Setup the LCD
  myGLCD.InitLCD();
  myGLCD.setFont(SmallFont);
  Serial.begin(57600);
  myGLCD.clrScr();

  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);

  drawButtons();

  attachInterrupt(2, myinthandler, RISING);
}


void loop()
{
  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(0, 0, 0);
  myGLCD.setColor(255, 255, 255);
  if (myTouch.dataAvailable())
  {
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis >= MainPeriod)
    {

      previousMillis = currentMillis;
      // need to bufferize to avoid glitches
      unsigned long _duration = duration;
      unsigned long _pulsecount = pulsecount;
      duration = 0; // clear counters
      pulsecount = 0;
      float Freq = 1e6 / float(_duration);
      Freq *= _pulsecount; // calculate F
      int rps = Freq / 1000;
      int RPM = rps * 60;
      const int pi = 314;
      int rads = (rps * 2 * pi) / 100;

      myTouch.read();
      x = myTouch.getX();
      y = myTouch.getY();

      if ((y >= 210) && (y <= 260)) // Y coordinates of the buttons
      {
        if ((x >= 10) && (x <= 94)) // Button: 1
        {
          ButtonFrame(10, 210, 94, 260, 9 , 209, 95, 261);
          myGLCD.setColor(0, 0, 0);
          myGLCD.fillRect(50, 50, 477, 205);
          myGLCD.setColor(255, 255, 255);
          myGLCD.setFont(SevenSeg_XXXL_Num);
          myGLCD.printNumI(RPM, 50, 70, 4, '0');
          myGLCD.setFont(Inconsola);
          myGLCD.print("RPM", 330, 104);
        }
        if ((x >= 104) && (x <= 188)) // Button: 2
        {
          ButtonFrame(104, 210, 188, 260, 103, 209, 189, 261);
          myGLCD.setColor(0, 0, 0);
          myGLCD.fillRect(30, 30, 477, 205);
          myGLCD.setColor(255, 255, 255);
          myGLCD.setFont(SevenSeg_XXXL_Num);
          myGLCD.printNumI(rps, 50, 70, 4, '0');
          myGLCD.setFont(Inconsola);
          myGLCD.print("Hz", 330, 104);
        }
        if ((x >= 198) && (x <= 282)) // Button: 3
        {
          ButtonFrame(198, 210, 282, 260, 197, 209, 283, 261);
          myGLCD.setColor(0, 0, 0);
          myGLCD.fillRect(30, 30, 477, 205);
          myGLCD.setColor(255, 255, 255);
          myGLCD.setFont(SevenSeg_XXXL_Num);
          myGLCD.printNumI(rads, 50, 70, 4, '0');
          myGLCD.setFont(BigFont);
          myGLCD.print("-1", 426, 96);
          myGLCD.setFont(Inconsola);
          myGLCD.print("rads", 330, 104);
        }
        if ((x >= 292) && (x <= 376)) // Button: 4
        {
          ButtonFrame(292, 210, 376, 260, 291, 209, 377, 261);
          myGLCD.setColor(0, 0, 0);
          myGLCD.fillRect(30, 30, 477, 205);
          myGLCD.setColor(255, 255, 255);
          myGLCD.setFont(SevenSeg_XXXL_Num);
          myGLCD.printNumI(millis() / 1000, 50, 70, 4, '0'); //millis is a placeholder until tachometer is figured out
          myGLCD.setFont(Inconsola);
          myGLCD.print("sec", 330, 104);
        }
        if ((x >= 386) && (x <= 470)) // Button: 5
        {
          ButtonFrame(386, 210, 470, 260, 385, 209, 471, 261);
          myGLCD.setColor(0, 0, 0);
          myGLCD.fillRect(30, 30, 477, 205);
          myGLCD.setColor(255, 255, 255);
          myGLCD.setFont(SevenSeg_XXXL_Num);
          myGLCD.printNumI(Freq, 50, 70, 4, '0'); // Freq is a placeholder until load cells are worked out
          myGLCD.setFont(Inconsola);
          myGLCD.print("NM", 330, 104);
        }
      }
    }
  }
  myGLCD.setFont(SmallFont);
}// end void loop


void myinthandler() // interrupt handler
{
  unsigned long currentMicros = micros();
  duration += currentMicros - previousMicros;
  previousMicros = currentMicros;
  pulsecount++;
}

Your logic seems wrong way round..

Check time then check button, otherwise unless you are touching button the code to update does not run since..

if (myTouch.dataAvailable()) will only be true if you are touching button..

tgsuperspec: Your logic seems wrong way round..

Check time then check button, otherwise unless you are touching button the code to update does not run since..

if (myTouch.dataAvailable()) will only be true if you are touching button..

Okay I think I get what you're saying, but I guess I'm unsure on how to go about executing it.

Something a bit like this, but you will need to write some code to prevent screen writing EVERY time around loop. ( Something like, last=x, if( now !=last) drawscreen(); )

#include <UTFT.h>
#include <UTouch.h>

// period of pulse accumulation and serial output, milliseconds
#define MainPeriod 500
long previousMillis = 0; // will store last time of the cycle end
volatile unsigned long duration = 0; // accumulates pulse width
volatile unsigned int pulsecount = 0;
volatile unsigned long previousMicros = 0;
byte dispScreen = 1;
int x, y;
int rps, RPM, rads;
const int pi = 314;
float Freq;
// Declare which fonts we will be using
extern uint8_t SmallFont[];
//extern uint8_t SevenSegNumFont[];
extern uint8_t SevenSeg_XXXL_Num[];
extern uint8_t BigFont[];
extern uint8_t Inconsola[];

// Set the pins to the correct ones for the shield
// ------------------------------------------------------------
// The model parameters for display & touch module

UTFT myGLCD(ILI9481, 38, 39, 40, 41);
UTouch  myTouch( 6, 5, 4, 3, 2);

void drawButtons()
{
  // Draw the buttons to choose the display type
  for (int x = 0; x < 5; x++)
  {
    myGLCD.setColor(VGA_YELLOW);
    myGLCD.fillRoundRect (10 + (x * 94), 210, 94 + (x * 94), 260);
    myGLCD.setColor(VGA_WHITE);
    myGLCD.drawRoundRect (10 + (x * 94), 210, 94 + (x * 94), 260);
    myGLCD.drawRoundRect (9 + (x * 94), 209, 95 + (x * 94), 261);
  }
  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(VGA_YELLOW);
  myGLCD.setColor(VGA_BLUE);
  myGLCD.print("RPM", 30 + (0 * 94), 227);
  myGLCD.print("Hz", 36 + (1 * 94), 227);
  myGLCD.print("rads", 13 + (2 * 94), 227);
  myGLCD.print("Tach", 23 + (3 * 94), 227);
  myGLCD.print("Torq", 23 + (4 * 94), 227);
  myGLCD.setFont(SmallFont);
  myGLCD.print("-1", 77 + (2 * 94), 224);
}


// Draw a coloured frame while a button is touched
void ButtonFrame(int xA1, int yA1, int xA2, int yA2, int xB1, int yB1, int xB2, int yB2)
{
  for (int i = 0; i < 5; i++)
  {
    myGLCD.setColor(VGA_WHITE);
    myGLCD.drawRoundRect (10 + (i * 94), 210, 94 + (i * 94), 260);
    myGLCD.drawRoundRect (9 + (i * 94), 209, 95 + (i * 94), 261);
  }

  myGLCD.setColor(0, 255, 255);
  myGLCD.drawRoundRect (xA1, yA1, xA2, yA2);
  myGLCD.drawRoundRect (xB1, yB1, xB2, yB2);

}

void screen1()
{
  ButtonFrame(10, 210, 94, 260, 9 , 209, 95, 261);
  myGLCD.setColor(0, 0, 0);
  myGLCD.fillRect(50, 50, 477, 205);
  myGLCD.setColor(255, 255, 255);
  myGLCD.setFont(SevenSeg_XXXL_Num);
  myGLCD.printNumI(RPM, 50, 70, 4, '0');
  myGLCD.setFont(Inconsola);
  myGLCD.print("RPM", 330, 104);
}

void screen2()
{ ButtonFrame(104, 210, 188, 260, 103, 209, 189, 261);
  myGLCD.setColor(0, 0, 0);
  myGLCD.fillRect(30, 30, 477, 205);
  myGLCD.setColor(255, 255, 255);
  myGLCD.setFont(SevenSeg_XXXL_Num);
  myGLCD.printNumI(rps, 50, 70, 4, '0');
  myGLCD.setFont(Inconsola);
  myGLCD.print("Hz", 330, 104);
}

void screen3()
{
  ButtonFrame(198, 210, 282, 260, 197, 209, 283, 261);
  myGLCD.setColor(0, 0, 0);
  myGLCD.fillRect(30, 30, 477, 205);
  myGLCD.setColor(255, 255, 255);
  myGLCD.setFont(SevenSeg_XXXL_Num);
  myGLCD.printNumI(rads, 50, 70, 4, '0');
  myGLCD.setFont(BigFont);
  myGLCD.print("-1", 426, 96);
  myGLCD.setFont(Inconsola);
  myGLCD.print("rads", 330, 104);
}

void screen4()
{
  ButtonFrame(292, 210, 376, 260, 291, 209, 377, 261);
  myGLCD.setColor(0, 0, 0);
  myGLCD.fillRect(30, 30, 477, 205);
  myGLCD.setColor(255, 255, 255);
  myGLCD.setFont(SevenSeg_XXXL_Num);
  myGLCD.printNumI(millis() / 1000, 50, 70, 4, '0'); //millis is a placeholder until tachometer is figured out
  myGLCD.setFont(Inconsola);
  myGLCD.print("sec", 330, 104);
}

void screen5()
{
  ButtonFrame(386, 210, 470, 260, 385, 209, 471, 261);
  myGLCD.setColor(0, 0, 0);
  myGLCD.fillRect(30, 30, 477, 205);
  myGLCD.setColor(255, 255, 255);
  myGLCD.setFont(SevenSeg_XXXL_Num);
  myGLCD.printNumI(Freq, 50, 70, 4, '0'); // Freq is a placeholder until load cells are worked out
  myGLCD.setFont(Inconsola);
  myGLCD.print("NM", 330, 104);
}

void setup()
{
  //int sec=millis()/1000;

  randomSeed(analogRead(0));
  // Setup the LCD
  myGLCD.InitLCD();
  myGLCD.setFont(SmallFont);
  Serial.begin(57600);
  myGLCD.clrScr();

  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);

  drawButtons();

  attachInterrupt(2, myinthandler, RISING);
}


void loop()
{
  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(0, 0, 0);
  myGLCD.setColor(255, 255, 255);
  if (myTouch.dataAvailable())
  {
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis >= MainPeriod)
    {

      previousMillis = currentMillis;
      // need to bufferize to avoid glitches
      unsigned long _duration = duration;
      unsigned long _pulsecount = pulsecount;
      duration = 0; // clear counters
      pulsecount = 0;
      Freq = 1e6 / float(_duration);
      Freq *= _pulsecount; // calculate F
      rps = Freq / 1000;
      RPM = rps * 60;
      rads = (rps * 2 * pi) / 100;

      myTouch.read();
      x = myTouch.getX();
      y = myTouch.getY();

      if ((y >= 210) && (y <= 260)) // Y coordinates of the buttons
      {
        if ((x >= 10) && (x <= 94)) // Button: 1
        {
          dispScreen = 1;
        }
        if ((x >= 104) && (x <= 188)) // Button: 2
        {
          dispScreen = 2;
        }
        if ((x >= 198) && (x <= 282)) // Button: 3
        {
          dispScreen = 3;
        }
        if ((x >= 292) && (x <= 376)) // Button: 4
        {
          dispScreen = 4;
        }
        if ((x >= 386) && (x <= 470)) // Button: 5
        {
          dispScreen = 5;
        }
      }
    }
  }
  switch (dispScreen)
  {
    case 1:
      screen1();
      break;
    case 2:
      screen2();
      break;
    case 3:
      screen3();
      break;
    case 4:
      screen4();
      break;
    case 5:
      screen5();
      break;
  }
}// end void loop

void myinthandler() // interrupt handler
{
  unsigned long currentMicros = micros();
  duration += currentMicros - previousMicros;
  previousMicros = currentMicros;
  pulsecount++;
}

Regards,

Graham

Hi Graham,
Thanks very much for reply. It definetly has made a difference.
However, only the variable in button 4 is updating without a button being pressed. This is the one assigned to millis/1000. I’m not sure what the reason for this would be?

ghlawrence2000:
Something a bit like this, but you will need to write some code to prevent screen writing EVERY time around loop. ( Something like, last=x, if( now !=last) drawscreen(); )

I had a background coloured rectangle drawn to clear the screen from the previous button. You moved it to the Screen() functions. This meant it was clearing screen every time.

I moved this part back to the main loop and it is now working well, e.g.

if ((x >= 10) && (x <= 94)) // Button: 1
        {
          myGLCD.setColor(0, 0, 0);
          myGLCD.fillRect(30, 30, 477, 205);
          dispScreen = 1;
        }

I only compiled the code, I didn't run it. The technique is good, I can assure you. You also don't want to be setting font and colour in the loop either, but I didn't wish to complete your project for you, just point you in the right direction :wink:

If you really do get stuck, I will have another look at it for you.

Regards,

Graham

Hi Graham,

That’s fair enough, sure half the fun is struggling through these problems.

Anyway been playing around with it last few hours and got it working. As you said, the technique was correct.

The code is a mess and prob really inefficient but I’ll include it in case it helps anyone in the future.

#include <UTFT.h>
#include <UTouch.h>

// period of pulse accumulation and serial output, milliseconds
#define MainPeriod 100
long previousMillis = 0; // will store last time of the cycle end
volatile unsigned long duration = 0; // accumulates pulse width
volatile unsigned int pulsecount = 0;
volatile unsigned long previousMicros = 0;
byte dispScreen = 1;
int x, y;
int rps, RPM, rads;
const int pi = 314;
float Freq;
// Declare which fonts we will be using
extern uint8_t SmallFont[];
//extern uint8_t SevenSegNumFont[];
extern uint8_t SevenSeg_XXXL_Num[];
extern uint8_t BigFont[];
extern uint8_t Inconsola[];

// Set the pins to the correct ones for the shield
// ------------------------------------------------------------
// The model parameters for display & touch module

UTFT myGLCD(ILI9481, 38, 39, 40, 41);
UTouch  myTouch( 6, 5, 4, 3, 2);

void drawButtons()
{
  // Draw the buttons to choose the display type
  for (int x = 0; x < 5; x++)
  {
    myGLCD.setColor(VGA_YELLOW);
    myGLCD.fillRoundRect (10 + (x * 94), 210, 94 + (x * 94), 260);
    myGLCD.setColor(VGA_WHITE);
    myGLCD.drawRoundRect (10 + (x * 94), 210, 94 + (x * 94), 260);
    myGLCD.drawRoundRect (9 + (x * 94), 209, 95 + (x * 94), 261);
  }
  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(VGA_YELLOW);
  myGLCD.setColor(VGA_BLUE);
  myGLCD.print("RPM", 30 + (0 * 94), 227);
  myGLCD.print("Hz", 36 + (1 * 94), 227);
  myGLCD.print("rads", 13 + (2 * 94), 227);
  myGLCD.print("Tach", 23 + (3 * 94), 227);
  myGLCD.print("Torq", 23 + (4 * 94), 227);
  myGLCD.setFont(SmallFont);
  myGLCD.print("-1", 77 + (2 * 94), 224);
}


// Draw a coloured frame while a button is touched
void ButtonFrame(int xA1, int yA1, int xA2, int yA2, int xB1, int yB1, int xB2, int yB2)
{
  for (int i = 0; i < 5; i++)
  {
    myGLCD.setColor(VGA_WHITE);
    myGLCD.drawRoundRect (10 + (i * 94), 210, 94 + (i * 94), 260);
    myGLCD.drawRoundRect (9 + (i * 94), 209, 95 + (i * 94), 261);
  }

  myGLCD.setColor(0, 255, 255);
  myGLCD.drawRoundRect (xA1, yA1, xA2, yA2);
  myGLCD.drawRoundRect (xB1, yB1, xB2, yB2);

}

void screen1()
{
  ButtonFrame(10, 210, 94, 260, 9 , 209, 95, 261);
  myGLCD.setColor(255, 255, 255);
  myGLCD.setFont(SevenSeg_XXXL_Num);
  myGLCD.printNumI(RPM, 50, 70, 4, '0');
  myGLCD.setFont(Inconsola);
  myGLCD.print("RPM", 330, 104);
}

void screen2()
{ ButtonFrame(104, 210, 188, 260, 103, 209, 189, 261);
  myGLCD.setColor(255, 255, 255);
  myGLCD.setFont(SevenSeg_XXXL_Num);
  myGLCD.printNumI(rps, 50, 70, 4, '0');
  myGLCD.setFont(Inconsola);
  myGLCD.print("Hz", 330, 104);
}

void screen3()
{
  ButtonFrame(198, 210, 282, 260, 197, 209, 283, 261);
  myGLCD.setColor(255, 255, 255);
  myGLCD.setFont(SevenSeg_XXXL_Num);
  myGLCD.printNumI(rads, 50, 70, 4, '0');
  myGLCD.setFont(BigFont);
  myGLCD.print("-1", 426, 96);
  myGLCD.setFont(Inconsola);
  myGLCD.print("rads", 330, 104);
}

void screen4()
{
  ButtonFrame(292, 210, 376, 260, 291, 209, 377, 261);
  myGLCD.setColor(255, 255, 255);
  myGLCD.setFont(SevenSeg_XXXL_Num);
  myGLCD.printNumI(millis() / 1000, 50, 70, 4, '0'); //millis is a placeholder until tachometer is figured out
  myGLCD.setFont(Inconsola);
  myGLCD.print("sec", 330, 104);
}

void screen5()
{
  ButtonFrame(386, 210, 470, 260, 385, 209, 471, 261);

  myGLCD.setColor(255, 255, 255);
  myGLCD.setFont(SevenSeg_XXXL_Num);
  myGLCD.printNumI(Freq, 50, 70, 4, '0'); // Freq is a placeholder until load cells are worked out
  myGLCD.setFont(Inconsola);
  myGLCD.print("Pulses", 330, 104);
}

void setup()
{
  //int sec=millis()/1000;

  randomSeed(analogRead(0));
  // Setup the LCD
  myGLCD.InitLCD();
  myGLCD.setFont(SmallFont);
  Serial.begin(57600);
  myGLCD.clrScr();

  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);

  drawButtons();

  attachInterrupt(2, myinthandler, RISING);
}


void loop()
{
  myGLCD.setFont(BigFont);
  myGLCD.setBackColor(0, 0, 0);
  myGLCD.setColor(255, 255, 255);
  if (myTouch.dataAvailable())
  {
      myTouch.read();
      x = myTouch.getX();
      y = myTouch.getY();

      if ((y >= 210) && (y <= 260)) // Y coordinates of the buttons
      {
        if ((x >= 10) && (x <= 94)) // Button: 1
        {
          myGLCD.setColor(0, 0, 0);
          myGLCD.fillRect(30, 30, 477, 205);
          dispScreen = 1;
        }
        if ((x >= 104) && (x <= 188)) // Button: 2
        {
          myGLCD.setColor(0, 0, 0);
          myGLCD.fillRect(30, 30, 477, 205);
          dispScreen = 2;
        }
        if ((x >= 198) && (x <= 282)) // Button: 3
        {
          myGLCD.setColor(0, 0, 0);
          myGLCD.fillRect(30, 30, 477, 205);
          dispScreen = 3;
        }
        if ((x >= 292) && (x <= 376)) // Button: 4
        {
          myGLCD.setColor(0, 0, 0);
          myGLCD.fillRect(30, 30, 477, 205);
          dispScreen = 4;
        }
        if ((x >= 386) && (x <= 470)) // Button: 5
        {
          myGLCD.setColor(0, 0, 0);
          myGLCD.fillRect(30, 30, 477, 205);
          dispScreen = 5;
        }
      
    }
  }
  
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis >= MainPeriod)
    {

      previousMillis = currentMillis;
      // need to bufferize to avoid glitches
      unsigned long _duration = duration;
      unsigned long _pulsecount = pulsecount;
      duration = 0; // clear counters
      pulsecount = 0;
      Freq = 1e6 / float(_duration);
      Freq *= _pulsecount; // calculate F
      rps = Freq / 1000;
      RPM = rps * 60;
      rads = (rps * 2 * pi) / 100;
    }
      switch (dispScreen)
    
  {
    case 1:
      screen1();
      break;
    case 2:
      screen2();
      break;
    case 3:
      screen3();
      break;
    case 4:
      screen4();
      break;
    case 5:
      screen5();
      break;
  }
  
}// end void loop

void myinthandler() // interrupt handler
{
  unsigned long currentMicros = micros();
  duration += currentMicros - previousMicros;
  previousMicros = currentMicros;
  pulsecount++;
}

Well done! :grin:

Regards,

Graham