Using same interrupt pin , get two different prints

Using same interrupt pin, I want to get the serial prints in two different functions. One serial print at one time.
But the following code gives both serial prints at once.
Is there any method to avoid printing both at the same time?
Please consider this as an example.

Example:

#define INTERRUPTED_PIN 18

volatile bool changed=false;

void keyPressed() {
  //  Serial.println("keyPressedOnPCF8574");
  changed = true;
}

void setup()
{
  Serial.begin(9600);
  
  pinMode(INTERRUPTED_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(INTERRUPTED_PIN), keyPressed, CHANGE);

pinMode(5,INPUT);

void loop()
{
  stateChanged1();
  stateChanged2();

}

void stateChange1(){

if(changed){
Serial.println("first state change");
changed=false;
}
}

void stateChange2(){

if(changed){
Serial.println("second state change");
changed=false;
}
}

Many thanks in advance.

Don't do serial I/O in interrupt context.
I know it is commented-out, but don't be tempted.

I'm not sure I understand your issue, but how
about setting "changed" to false?

(Your example doesn't compile,so this is all moot)

You haven't said how to identify which print you want.

Raweesha:
Using same interrupt pin, I want to get the serial prints in two different functions. One serial print at one time.
But the following code gives both serial prints at once.
Is there any method to avoid printing both at the same time?
Please consider this as an example.

Many thanks in advance.

It is not clear what you want given your description and your code.

You have told it to interrupt on change. It will interrupt on HIGH->LOW and LOW->HIGH transitions. You do not reset the changed flag anywhere. When changed gets set to true then it will stay true until you restart the Arduino.

If you want to distinguish between the transitions you can either use 2 different interrupts and 2 different flags OR check the state of the input in the interrupt and set the appropriate flag.

By the way, that code does not compile.

Here is a way to do it:

#define INTERRUPTED_PIN 18

volatile bool lo_to_hi = false;
volatile bool hi_to_lo = false;

void keyPressed()
{
  if (digitalRead(INTERRUPTED_PIN) == HIGH) lo_to_hi = true;
  else hi_to_lo = true;
}

void setup()
{
  Serial.begin(9600);
  pinMode(INTERRUPTED_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(INTERRUPTED_PIN), keyPressed, CHANGE);
}

void loop()
{
  if (lo_to_hi)
  {
    Serial.println("lo to hi");
    lo_to_hi = false;
  }
  if (hi_to_lo)
  {
    Serial.println("hi to lo");
    hi_to_lo = false;
  }
}

Since you haven't described your application I have no way of knowing if this is sufficient or overkill. If you are just monitoring a button then this is overkill. I don't recommend interrupts for reading buttons.

ToddL1962:
Here is a way to do it:

#define INTERRUPTED_PIN 18

volatile bool lo_to_hi = false;
volatile bool hi_to_lo = false;

void keyPressed()
{
  if (digitalRead(INTERRUPTED_PIN) == HIGH) lo_to_hi = true;
  else hi_to_lo = true;
}

void setup()
{
  Serial.begin(9600);
  pinMode(INTERRUPTED_PIN, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(INTERRUPTED_PIN), keyPressed, CHANGE);
}

void loop()
{
  if (lo_to_hi)
  {
    Serial.println("lo to hi");
    lo_to_hi = false;
  }
  if (hi_to_lo)
  {
    Serial.println("hi to lo");
    hi_to_lo = false;
  }
}




Since you haven't described your application I have no way of knowing if this is sufficient or overkill. If you are just monitoring a button then this is overkill. I don't recommend interrupts for reading buttons.

Thanks a lot this is what I needed :slight_smile:

Todd1962 has addressed a likely solution already, but what you never seem to have considered previously, is that interrupts don’t eliminate contact bounce.
Testing ‘first’ edges is different from detecting ‘every’ edge.

Remember the code can do maybe 10,000 ‘things’ while the button is settling high or low.

lastchancename:
Todd1962 has addressed a likely solution already, but what you never seem to have considered previously, is that interrupts don’t eliminate contact bounce.
Testing ‘first’ edges is different from detecting ‘every’ edge.

Remember the code can do maybe 10,000 ‘things’ while the button is settling high or low.

Agree. If you are actually processing a mechanical switch then you need to account for bounce.

Thankyou ToddL1962 and lastchancename :slight_smile: