noInterrupts() not working

This is my code

volatile long lastduration;
volatile long duration;
const int interruptPin = 2; 
const int pinB = 3;
const int pinA = 2;
volatile long counter = 0; 
volatile int pinAState;
volatile int pinBState;
volatile int32_t incr = 1;

//=========================================
void State()
{
  //Save time of this pulse
  noInterrupts();
  lastduration = millis();
  interrupts();
  // Reads the current state of pinA
  pinAState = digitalRead(2); 
  // Reads the "current" state of the pinB
  if (digitalRead(3) != pinAState)
  {
    //Clockwise rotation
    counter += incr ;
  } else {
    //counter clockwise rotation
    counter -= incr ;
  }
  lastduration = duration
  noInterrupts();    <++++++++++++++++++++++++ here
  Serial.print(" Counter ==> "); Serial.println(counter);
  Serial.print("duration ==> "); Serial.println(duration);
  interrupts();
}

//=========================================
void setup() { 
  pinMode(pinA, INPUT_PULLUP);
  pinMode(pinB, INPUT_PULLUP);
  // Start the serial port
  Serial.begin (115200);
  attachInterrupt(digitalPinToInterrupt( interruptPin), State, RISING);
  delay(1000);
}

//==========================================
void loop() 
{
  interrupts();
}

The error occurs at the second occurrence of the noInterrupts() command in my ISR

I checked the libraries mentioned found nothing that gave me any clues.

This is the error message I’m getting

In file included from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:30:0,

from sketch\sketch_jun22a.ino.cpp:1:

C:\Users\Mel\Documents\Current\Arduino\Arduino Nano\sketch_jun22a\sketch_jun22a.ino: In function ‘void State()’:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:102:24: error: expected ‘;’ before ‘asm

#define noInterrupts() cli() // this is not mine part of error

^

C:\Users\Mel\Documents\Current\Arduino\Arduino Nano\sketch_jun22a\sketch_jun22a.ino:36:3: note: in expansion of macro ‘noInterrupts’

noInterrupts();

^~~~~~~~~~~~

exit status 1
Error compiling for board Arduino Nano.

Thanks
pamam

Look at the line above.

You forgot the semicolon

 lastduration = duration

By the way, you never need to disable interrupts just to call millis():

 //Save time of this pulse
  noInterrupts();
  lastduration = millis();
  interrupts();

Additionally, interrupts are already disabled by default inside an ISR.

Why are you continually enabling interrupts in loop()? Also there is no serial transmission in an ISR, you can only place data in the output buffer and it will be sent after the return from the ISR. That makes it dangerous to use. You can't use serial reliably with interrupts turned off with noInterrupts, either.

How stupid can I get. I know this is just an excuse but I was so focused on it having something to do with my interrupts, that I did not look any further.

Thanks y'all
pamam

As long as you are having fun . . . :slight_smile:

Definitely having fun, maybe too much fun.

Though I'd surely like a piece of code that could speed up the counter as you rotate the shaft faster manually. I need to get the 60000000 pulses in a few seconds, yet have a 1 pulse accuracy. So far I've only found it in hardware using 3 chips one of which costs $45-$75 yes that's right I said $: which I can't afford for just project.

Thanks
pamam

The Arduino can easily count 60000000 pulses in a few seconds.

Use Timer1 with external count input to accumulate the low order 16 bits of the count, and an overflow routine that increments the high order 16 bits of the count. The maximum clock rate is higher than 20 MHz.

Excellent tutorial at https://www.gammon.com.au/timers