Mkr 1010 attachInterrupt help

I'm trying to use interrupts for the mkr 1010 wifi. The attached code works for a nano 33 IoT using the associated interrupt pins for that model. I cant seem to get it to run at all for the mkr 1010 wifi using the interrupt pins 1 and 4, as shown in the code.

Is there a slightly different syntax or pin naming convention for the mkr?

Any help would be appreciated, thanks.

volatile unsigned long voltageTime;
volatile unsigned long currentTime;

volatile boolean valid1 = false;
volatile boolean valid2 = false;

//mathematical constants
const float rads = 57.29577951;  // 1 radian = approx 57 deg.
const float degree = 360;
const float frequency = 50;

float phaseAngle;




void setup() {
  pinMode(1, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);

  // voltage & current interrupts for time difference
  attachInterrupt(1, voltagePhaseTime, RISING);
  attachInterrupt(4, currentPhaseTime, RISING);
  Serial.begin(9600);
}

void loop() {
  if (valid2) {
    // time difference between waveforms
    volatile unsigned long phaseTimeDifference = currentTime - voltageTime;

    // conversion to degrees
    phaseAngle = (((phaseTimeDifference * pow(10, -6)) * degree) * frequency);

    Serial.print(phaseAngle);
    Serial.print("     ");
    Serial.println(phaseTimeDifference);

    // reset logic
    noInterrupts();
    valid1 = valid2 = false;
    interrupts();
  }
}

// voltage rising edge time
void voltagePhaseTime() {
  if (!valid1) {
    voltageTime = micros();
    valid1 = true;
  }
}

// current rising edge time
void currentPhaseTime() {
  if (valid1 && !valid2) {
    currentTime = micros();
    valid2 = true;
  }
}

Thanks for that. I read it before posting. Still cant pin point what is the root cause of the issue.

As i said in the main post, the code works on a nano 33 IoT. I'm new to mkr 1010 wifi. I was under the impression they were similar boards

digitalPinToInterrupt?

|MKR Family boards|0, 1, 4, 5, 6, 7, 8, 9, A1, A2|
|Nano 33 IoT|2, 3, 9, 10, 11, 13, A1, A5, A7|

Tried that. I used the syntax described, although not shown in the code posted, and the interrupt pins 1 and 4 on the mkr but the serial does not output anything.

Using pins 2 and 3 on the nano 33 the serial outputs the angle and time difference as expected.

The syntax posted should work on samd boards according to the documentation. I will try adding digitalPinToInterrupt(1) and digitalPinToInterrupt(4) in place of the pin number in the attachInterrupt function again to check i never made an error and report back.

so I wondered that 1 and 4 work for you on Nano IoT

Pins 2 and 3 work on the nano 33 iot as interrupt pins. If i ammend the code to reflect this and run it on the nano it works as expected.

Pins 1 and 4 on the mkr 1010 wifi are documented as interrupt pins but when i ammend the code for those pins the code does not work as it should.

For reference, the code is measuring the time difference between two square waves outputted from a compatator.

Maybe the logic levels are different between boards? The square waves have an amplitude of 1.9V.

But yeah, the nano 33 works fine but the mkr 1010 doesnt. I was hoping to use the mkr instead of the nano 33 as the nano 33 has an issue with noisy analog input pins. Having to use moving averages to combat it. But thats a seperate issue.

I'll be able to try that suggestion you posted very soon and I'll teport back on the outcome.

Found the issue, tested all pins and it seems that pin 1 does not act as an interrupt pin on my mkr 1010.

Why remains a mystery but at least the issue is resolved for now.

Thanks for the help.

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