Interruptions digitalRead iteration

I use interrupt function to fill array with values received from digitalRead.

 void setup() {
      Serial.begin(115200);
       attachInterrupt(0, test_func, CHANGE);
    }

    void test_func(){
      if(digitalRead(pin)==HIGH){
          test_array[x]=1;  
        } else if(digitalRead(pin)==LOW){
          test_array[x]=0;  
        }
         x=x+1;
    }

That problem is that when i print test_array there are values like: 111010 or 1011000. As i understand if i use CHANGE option in attachInterrupt function data sequence should be always 0101010101 without repeating.

Can you post all your code?

How to use this forum

http://snippets-r-us.com/

How fast are these changes arriving?

As i understand if i use CHANGE option in attachInterrupt function data sequence should be always 0101010101 without repeating.

Depends on if you are keeping up.

int rxPin = 2; 
int pulse[360];
int width[360];
int x=0;
int timer;
char found=0;
 
void setup() {
  Serial.begin(115200);
  Serial.println("Serial initialized");
  attachInterrupt(0, parse_wave, CHANGE);
}

void loop(){
  if (pulse[x]==0 && width[x] > 500 && width[x] < 800){
    detachInterrupt(0);
    if (x>73){
      for (int i=x-72; i<=x; i++){
       Serial.print(pulse[i]);
      } 
       Serial.println();
    }
    Serial.println(width[x]);
    x=0;
    attachInterrupt(0, parse_wave, CHANGE);
  }
}

void parse_wave(){
  if(digitalRead(rxPin)==HIGH){
      width[x]=micros()-timer;  
      pulse[x]=0;  
    } else if(digitalRead(rxPin)==LOW){
      width[x]=micros()-timer;  
      pulse[x]=1;  
    }
    x=x+1; timer=micros();
    if (x==360){x=0;}

Changes are arriving quite fast, since it is RF data from radio module.

Edit: May not be bounce, as you have since suggested more info

You are experiencing 'bounce' - where the button contacts trigger the interrupt multiple times.

My method for this is to set a flag on entering the method and unset on exit. You may need to delay on exit fractionally too.

You are also seeing the delay from interrupt -> read, hence the 111010 readings, due to the contact bounce.

Try:

boolean debounce = false;

void test_func() {
  if(debounce)
    return;

  debounce = true;

  //Read pins, do other code, etc.
  
  debounce = false;
}

This is really only suitable for button presses - if you're measuring relay contact chatter, you will need to add a low-pass filter to your input.

Note: detachInterrupt() appear to cache interrupts, triggering them all when re-attaching, so may not be worth a try. I did try re-routing attachInterrupt() to a dummy method, but was far simpler to just set a bool until complete.

detachInterrupt() appear to cache interrupts,

One interrrupt may be cached. You can clear that.

http://www.gammon.com.au/interrupts

width, pulse and x should be volatile.

http://www.gammon.com.au/interrupts

Changes are arriving quite fast, since it is RF data from radio module.

How fast? Put a figure on it.

So whats the problem with your CURRENT code. It's total different for the first lot you posted.

Mark