How to activate two different LEDs depending on data output

Hello, new to arduino… I am using a pulse sensor and I am wondering how I can activate one LED for the first “thump” of the heart beat and then turn on another LED for the second “thump” of the heartbeat whilst turning off the first LED. The code below works as follows, the sensor reads the pulse, saves 256 samples, out of those samples there will be a top most max which will activate the first led, and a second most max that should activate the second LED. )Everything in the middle can simply be ignored all it is doing is processing flat tops) the final output(s) or max’s are stored to the array val2.

int LED13 = 13;
int LED12 = 12;

void setup()
{
  pinMode(LED13,OUTPUT);         
  pinMode(LED12,OUTPUT);
  Serial.begin(9600);  
}
void loop()
{
    float dt = 0.01;
    int ind1[100], mult[100];
    int inp[256], val1[100], val2[100], ind2[100];
    val1[0] = -1.2;
    int i;
    int f;
    
    for (i = 0 ; i < 256 ; i ++) // save 256 samples
    {     
      inp[i] = analogRead(A0);
      Serial.println(inp[i]);
      delay(10);

    if (val2[i] > 650)
    {
      digitalWrite(LED12,HIGH);
      digitalWrite(LED13,LOW);
      }
    else
     if (val2[i] < 650)
    {
      digitalWrite(LED12,LOW);
      digitalWrite(LED13,HIGH);
      }
    
    }// close for i
    
    int max_i = 0;
    int max_v = 0;
    
    for (int i = 0; i < 256; i++)
    {
     if ( inp[i] > max_v )
      {
       max_v = inp[i];
       max_i = i;
      }
    }
       
int N = 10;   // loc max neighborhood size
int cnt1 = 0;  // count number of local maxima
int cnt2 = 0;  // count number of distinct local maxima
  
 for (int i = N-1; i < 255-N; i++) 
  {
      bool loc = true;
      for (int j = 1; j < N; j++) // look N-1 back and N-1 ahead
      { 
        if (inp[i] < inp[i-j] || inp[i] < inp[i+j]) loc = false;
      }
        if (loc == true)
        {         
        cnt1 = cnt1 + 1;
        val1[cnt1] = inp[i];
        ind1[cnt1] = i;
        
        if (inp[i] != val1[cnt1-1]) 
          {
           cnt2 = cnt2 + 1;
           mult[cnt2] = 1;          
          }
        else 
        {
           mult[cnt2] = mult[cnt2] + 1;  
        }
      } //End loc == true
      
        //Serial.println(cnt1);
        //Serial.println(cnt2);
     
   } // end for i

  // now process flat tops
   int cnt3 = 0;
   
   for (int i = 1; i <= cnt2; i++) 
   {
     int mid = 0;
     for (int j = 1; j <= mult[i]; j++) 
     {
        mid = mid + ind1[cnt3 + j];
     }
          
     val2[i] = val1[cnt3 + mult[i]];
     ind2[i] = dt*mid/mult[i];
      
     Serial.println(val2[i]);
     //Serial.print("        ");
     //Serial.println(0.005*ind2[i]);    
     cnt3 = cnt3 + mult[i]; 
     
   }      
  //Serial.println("----------------------------------");
  
} // close main loop

Your code doesn't quite do what you described. Would you like it to?

After taking 256 samples, then doing the processing, both LEDs will come on at the same time. Yes there was a higher peak and yes there was a lower one. That doesn't give you any indication of when that occurred.

Additionally, while you're in the sample-taking loop, the val2 array never gets updated so the LEDs will switch on or off based on random numbers remaining in memory. For more randomness, you're addressing locations past the end of val2, since i can go up to 255 and val2 only has 100 elements.

Give your arrays meaningful names. inputSamples and outputHighPeaks or something like that. There's no penalty for longer names.

What do you mean it doesn't do what I want it to? Everything seems just fine on the serial monitor. The val2 array seems to update just fine also if I print it there's values in there and they update. what do you think needs to be fixed?

Normally people post code here when it is not doing what they want it to. The usual complaint is "It doesn't work!" Which is completely unhelpful.

You are making decisions on the values in val2 before you put any values into that array. The array is local to loop() which means it goes out of scope when loop() ends. Any other code may use that area of memory at that point. Now the Arduino is so simple that there isn't any other code that will overwrite that memory, so the LEDs will turn on and off based on the values you put in there on the previous iteration of loop().

A bigger problem is running off the end of val2 and expecting to find values there. Additionally, cnt1 and cnt2 seem to be unbounded and they can also run off the end of their arrays and start writing values to nearby areas of memory. Even though there aren't 100 local maxima or minima in a 256-sample array, you might increase the number of samples in the future and then you'll have some very difficult bugs to find.

I'm sure there's more bugs. I just can't keep ind1 and val2 in my head long enough to find them. Please give them names. Also give the array sizes names, so you don't have "256" and "100" scattered throughout the code. Replace them with inputArraySize and localMaxArraySize or other names which are meaningful to you.

You declare val2 as a 100-element array, but you use it as 256-element array. val1, ind1 and ind2 are also at risk of being addressed out of range.

I understand you get an output - but is this output even correct? You're lucky it doesn't crash. Reading/writing variables outside of their assigned memory is a recipe for disaster.

Without clear variable names or comments on what is what, I can't figure out what it is supposed to do, nor what it is actually doing.

val2 stores between 1-10 values at a times more but to be safe I’m using 100, after about a half second everything is erased and re-written am I wrong?

"To be safe" you check that the array index doesn't go outside the limits of the array. Just allocating 200bytes of precious SRAM memory is not safe.

optimaK5:
val2 stores between 1-10 values at a times

You use it in a loop, where the index may go up to 255. That means you need room for 256 elements in your array no matter whether you store data in an element or not.