How to create a command buffer or interrupt :smiley-roll-sweat:

for background i want to state that i have an array that represents the ground and i have an encoder that measures position. because i will be pulling solenoids over the ground, i need to use the encoder to check postion when it first reads the array and then account for where the solenoids need to be on the ground inorder to spray upon the area marked on the array. eventually i need a running loop to constantly accept input but for now, i will use a sigle, planned array to test accuracy.

Ok, so i have had some good help writing this code an now i can finally try to finish the last part. I am reading an array as well as an encoder. My question is, how do i make a decision based on the array that will let me check the position of the encoder sometime later. I have seen attatching interrupts and i heard metion of buffers but i am unfamiliar with both so i have to ask what the best method is. currently, the encoder i am using is too sensitive so i am turning it by hand to avoid missfires. my goal is to read the array, mark certain values of it, and then have the DUE that i am using check the encoder until it needs to fire a solenoid based on what the arduino says.

For example if i recieve a 1 at encoder pulse 500, i want to fire a solenoid at encoder pulse 580. how can i account for such an offset?

also, im using a quadreture encoder but order a much less accurate one though it still hsnt arrived. i just need an index, direction is not an issue so if soeone knows a simpler or more efficient way to read this encoder, that would be very helpful as well. here is my code along with the description of what i want it to do rather than what i got it to do for testing:

[code]
const int quad_A = 2;
const int quad_B = 13;
const unsigned int mask_quad_A = digitalPinToBitMask(quad_A);
const unsigned int mask_quad_B = digitalPinToBitMask(quad_B);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  int i = 0;
  int allie[] = {1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1};
  for (int i = 0; i < 4; i++) {

    if (allie[4 * i] == 1) {
      Serial.println("on");
      /*rather than use a delay, i want to use a buffer or interrupt that is dependant on the encoder reading. that is, i want create a command that now waits and checks the encoder. */
    }
    if (allie[4 * i + 1] == 1) {
      Serial.println("2on");
      //delay(1000);
    }
    if (allie[4 * i + 2] == 1) {
      Serial.println("3on");
      //delay(1000);
    }
    if (allie[4 * i + 3] == 1) {
      Serial.println("4on");
      //delay(1000);
    }
    Serial.println("breaktime");
    }

  // activate peripheral functions for quad pins
  REG_PIOB_PDR = mask_quad_A;     // activate peripheral function (disables all PIO functionality)
  REG_PIOB_ABSR |= mask_quad_A;   // choose peripheral option B
  REG_PIOB_PDR = mask_quad_B;     // activate peripheral function (disables all PIO functionality)
  REG_PIOB_ABSR |= mask_quad_B;   // choose peripheral option B

  // activate clock for TC0
  REG_PMC_PCER0 = (1 << 27);
  // select XC0 as clock source and set capture mode
  REG_TC0_CMR0 = 5;
  // activate quadrature encoder and position measure mode, no filters
  REG_TC0_BMR = (1 << 9) | (1 << 8);
  // enable the clock (CLKEN=1) and reset the counter (SWTRG=1)
  // SWTRG = 1 necessary to start the clock!!
  REG_TC0_CCR0 = 5;

}




void loop() {
  // put your main code here, to run repeatedly:
  Serial.println(REG_TC0_CV0, DEC);
  delay(500);

}

[/code]

// Aslo note that the printing of the step is not necessary, this is just from the first code i could get to even read the encoder. any and all suggestions are appreciated. Thanks in advance!

pamoreno:
because i will be pulling solenoids over the ground, i need to use the encoder to check postion when it first reads the array and then account for where the solenoids need to be on the ground inorder to spray upon the area marked on the array.

That sounds like you need to

  record the initial encoder position 
     regularly compare the new encoder position with the previous position
     if the difference is equal to some predetermined value
         do something
     repeat

Would it work if every encoder pulse were to increment an unsigned long variable which you could then use to keep track of distance in the same way that millis() is used to keep track of time ?

...R