attachInterrupt issue

i have a small problem on interrupt fuction, all i want is to print on monitor when the pin goes from low to high and from high to low,
so i was thinking to use twice this fuction, here is my simple code

void setup()
{
   attachInterrupt(0, rising_CH1, RISING);
  attachInterrupt(0, falling_CH1, FALLING);  
    Serial.begin(115200);
}

void loop()
{

}

void rising_CH1(){
     Serial.println(" state on PIN_2 changed from 0 TO 1"); 
     }

void falling_CH1(){

     Serial.println(" state on PIN_2 changed from 1 TO 0"); 
     }

the resulut is that woriking only the last void falling_CH1

why is that? any solution on my problem?

thanks in advance

Interrupts are disabled during this time, so serial.print won't work

Try using a variable and giving it a number on the interrupts. Then back in the normal program, if any of these numbers are found, print relative message.

if i understoond that, it doesnt work
here is the new code

int state;
void setup()
{
   attachInterrupt(0, rising_CH1, RISING);
  attachInterrupt(0, falling_CH1, FALLING);  
    Serial.begin(115200);
}

void loop()
{
 if (state==1){
      Serial.println(" state on PIN_2 changed from 0 TO 1");   
      state=3;
 }
 
  if (state==0){
      Serial.println(" state on PIN_2 changed from 1 TO 0");   
      state=3;
 }

}

void rising_CH1(){
     state=1;
   
}

void falling_CH1(){
     state=0;

 }

From attachInterrupt() - Arduino Reference

Replaces any previous function that was attached to the interrupt.

You can't have two interrupts on the same pin, even if they are to be triggered by different events.

so how can i know the status of a pin?

so how can i know the status of a pin?

digitalRead? Although the function is probably too slow to call in an interrupt. Look into direct port manipulation.

i changed the code to this

int counter;
int input_1=2;

volatile int state = LOW; 
int val;

void setup()
{
  
  pinMode(input_1, INPUT);  
  attachInterrupt(0, rising_CH1, CHANGE);
  Serial.begin(115200);
}

void loop()
{
  if (state==1){ //if there is change on pin
     counter=counter+1;
     Serial.print("counter nr: "); 
     Serial.print(counter);  
     Serial.print(" state on PIN_2 changed to ");   
     Serial.println(val);    
     state=2; //reset the change
 }
}
void rising_CH1(){
  val=digitalRead(input_1); 
  state=1; //there is change on pin
 }

when i am changing the wire of that pin quickly on vdd (i have pulldown resistor on gnd) i am geting these results:

counter nr: 73 state on PIN_2 changed to 0
counter nr: 74 state on PIN_2 changed to 1
counter nr: 75 state on PIN_2 changed to 1
counter nr: 76 state on PIN_2 changed to 0
counter nr: 77 state on PIN_2 changed to 1
counter nr: 78 state on PIN_2 changed to 1
counter nr: 79 state on PIN_2 changed to 1
counter nr: 80 state on PIN_2 changed to 0
counter nr: 81 state on PIN_2 changed to 1

off course a pin cant chaging from 1 to 1 state ot from 0 to 0,

so am i loosing data? or it can't print all?

Interrupts are disabled during this time, so serial.print won't work

Serial.write (called by serial.print) does this:

void HardwareSerial::write(uint8_t c)
{
  while (!((*_ucsra) & (1 << _udre)))
    ;

  *_udr = c;
}

I'd say that works even with interrupts off. The input buffer to Serial.read won't get any new data during an interrupt, though.

so am i loosing data? or it can't print all?

So shorten you Serial.print so it only prints a "0" or "1" on every line. Should make it at least 10x faster.

Do something simple like this in your interrupt handler:

volatile unsigned int
  fall_count = 0,
  rise_count = 0;
 
void change_CH1(){
  val=digitalRead(input_1);
  if (val)
     rise_count++;
  else
     fall_count++;
 }

Then outside the interrupt handler, read the data (with interrupts disabled):

unsigned my_rise_count, my_fall_count;
uint8_t was_transition = 0;

loop()
{

  cli();
  if (my_rise_count != rise_count ||
      my_fall_count != fall_count) {
    my_rise_count = rise_count;
    my_fall_count = fall_count;
    was_transition = 1;
  }
  sei();


  if (was_transition)
      print something

Now you can have logic to do what you need to do with the transition. You can take your time in terms of generating serial output, and the interrupt handler will still count the transitions.
Your loop code can look at the rise and fall counts to decide what is going on, of if a transition might have been missed.

thanks for the answer but becouse i dont know everything (still noob)

what if (val) means? i know the typical " if (val==1) or something like that, but only (val)?

The if(val == 1) construct evaluates the statement inside the parentheses. If the statement is true the if block is executed. In the if(val == 1) case, the equality operator is applied to the 2 operands to determine true or false. In the if(val) case, there is no operator to apply, so val itself must represent true (non-zero) or false (zero).

Unless the single operand in the if statement is a boolean variable (that is explicitly set to values of true or false), I don't like the if(val) construct. Even after 25 years of coding in C/C++, I still need to look up whether true or false is 0.

with that code after first pin touch one high, it prints that "something" all time

That was psuedo code to give you an idea of the structure of the program. The idea was that you would replace the "something" with something meaningful, like the number of rising edges or falling edges.

ok i got it

i tried to print the rise/fall counts and i saw that they are never the same, 74/100, so i think i am still losing data

i tried to print the rise/fall counts and i saw that they are never the same, 74/100, so i think i am still losing data

They should change every time the interrupt fires.

What is generating the signal that triggers the interrupt? What does the code look like now?