Arduino Interrupt pins problem

Robin2:
Also please post some samples of the output in each case.

...R

well, this is your code and I just added marked line with #### to show the number of pulses in 1 second

const byte fwdPin = 9;
const byte revPin = 10;
const byte potPin = A1;

int potVal;
int pwmVal;

unsigned long revMicros;
unsigned long prevRevMicros;
unsigned long revDuration;
unsigned long revCount;

unsigned long prevDisplayMillis;
unsigned long displayInterval = 1000;

// variables for the ISR
volatile unsigned long isrMicros;
volatile unsigned long isrCount;
volatile bool newIsrMicros = false;

void setup() {
Serial.begin(115200);
Serial.println("SimpleISRdemo.ino");
pinMode (fwdPin, OUTPUT);
pinMode (revPin, OUTPUT);

isrCount = 0;
attachInterrupt(1, revDetectorISR, RISING);
}

//==========

void loop() {
getIsrData();
if (millis() - prevDisplayMillis >= displayInterval) {
prevDisplayMillis += displayInterval;
showData();
isrCount=0; //####
readPot();
updateMotorSpeed();
}
}

//===========

void readPot() {
potVal = analogRead(potPin);
}

//===========

void updateMotorSpeed() {
pwmVal = potVal >> 2;

digitalWrite(revPin,LOW);
analogWrite(fwdPin, pwmVal);
}

//===========

void getIsrData() {
if (newIsrMicros == true) {
prevRevMicros = revMicros; // save the previous value
noInterrupts();
revMicros = isrMicros;
revCount = isrCount;
newIsrMicros = false;
interrupts();
revDuration = revMicros - prevRevMicros;
}
}

//===========

void showData() {
Serial.println();
Serial.println("===============");
Serial.print("PWM Val "); Serial.println(pwmVal);
Serial.print(" Rev Duration ");
Serial.print(revDuration);
Serial.print(" Rev Count ");
Serial.print(revCount);
Serial.println();
}

//===========

void revDetectorISR() {
isrMicros = micros();
isrCount ++;
newIsrMicros = true;

}


and this the result with both pins 2 and 3

Rev Duration 44644 Rev Count 22



Now I changed it just a little:

changes marked with //####

const byte fwdPin = 9;
const byte revPin = 10;
const byte potPin = A1;

int potVal;
int pwmVal;

unsigned long revMicros;
unsigned long prevRevMicros;
unsigned long revDuration;
unsigned long revCount;
unsigned long lastMillis=0; //####

unsigned long prevDisplayMillis;
unsigned long displayInterval = 1000;

// variables for the ISR
volatile unsigned long isrMicros;
volatile unsigned long isrCount;
volatile bool newIsrMicros = false;

void setup() {
Serial.begin(115200);
Serial.println("SimpleISRdemo.ino");
pinMode (fwdPin, OUTPUT);
pinMode (revPin, OUTPUT);

isrCount = 0;
attachInterrupt(1, revDetectorISR, RISING);
}

//==========

void loop() {
getIsrData();
// if (millis() - prevDisplayMillis >= displayInterval) { //####
prevDisplayMillis += displayInterval;
showData();
readPot();
updateMotorSpeed();
// } //####
}

//===========

void readPot() {
potVal = analogRead(potPin);
}

//===========

void updateMotorSpeed() {
pwmVal = potVal >> 2;

digitalWrite(revPin,LOW);
analogWrite(fwdPin, pwmVal);
}

//===========

void getIsrData() {
if (newIsrMicros == true) {
prevRevMicros = revMicros; // save the previous value
noInterrupts();
revMicros = isrMicros;
if(millis() - lastMillis>=1000){ //####
revCount = isrCount; //####
lastMillis += 1000; //####
isrCount=0; //####
} //####
newIsrMicros = false;
interrupts();
revDuration = revMicros - prevRevMicros;
}
}

//===========

void showData() {
Serial.println();
Serial.println("===============");
Serial.print("PWM Val "); Serial.println(pwmVal);
Serial.print(" Rev Duration ");
Serial.print(revDuration);
Serial.print(" Rev Count ");
Serial.print(revCount);
Serial.println();
}

//===========

void revDetectorISR() {
isrMicros = micros();
isrCount ++;
newIsrMicros = true;

}

this is the result with arduino pin 3

Rev Duration 44660 Rev Count 22

but for pin 2

Rev Duration 44684 Rev Count 37



You can see by same code I have defferent results.

But if I apply delay(1000) in void loop, or unmark this line

// if (millis() - prevDisplayMillis >= displayInterval) { //####

the result will be same again.