How to read 2 analog sensors to trigger 2 solenoids separately?

Hi,
I am trying to modify this sketch (made up of 2 files) to have Heart Pulse Sensor A trigger Solenoid 1, and Heart Pulse Sensor B trigger Solenoid 2 on the same Arduino Uno. As the sketch stands now it only has Heart Pulse Sensor A trigger Solenoid 1.

How am I able to add in the second sensor and keep the data streams from the 2 sensors separate? It is confusing because this sketch is multiple files. I have watched tutorials on how to make classes/libraries and it seems promising, but I am not able to figure it out by myself and am a very novice programmer.

The first pair useas pin A0, and digital pin 11. The second pair will use pin A1 with digital pin 5.

Sensor I am using: http://pulsesensor.com/

NOTE: Most comments on this sketch are from an original heart pulse sensor code that has now been modified.

/*
>> Pulse Sensor Amped <<
This code is for Pulse Sensor Amped by Joel Murphy and Yury Gitman
    www.pulsesensor.com 
    >>> Pulse Sensor purple wire goes to Analog Pin 0 <<<
Pulse Sensor sample aquisition and processing happens in the background via Timer 1 interrupt. 1mS sample rate.
PWM on pins 9 and 10 will not work when using this code!
The following variables are automatically updated:
Pulse :     boolean that is true when a heartbeat is sensed then false in time with pin13 LED going out.
Signal :    int that holds the analog signal data straight from the sensor. updated every 1mS.
HRV  :      int that holds the time between the last two beats. 1mS resolution.
BPM  :      int that holds the heart rate value. derived every pulse from averaging previous 10 HRV values.
QS  :       boolean that is made true whenever Pulse is found and BPM is updated. User must reset.

This code is designed with output serial data to Processing sketch "PulseSensorAmped_Processing-xx"
The Processing sketch is a simple data visualizer. 
All the work to find the heartbeat and determine the heartrate happens in the code below.
Pin 13 LED will blink with heartbeat.
It will also fade an LED on pin 11 with every beat. Put an LED and series resistor from pin 11 to GND

See the README for more information and known issues.
Code Version 0.1 by Joel Murphy & Yury Gitman  Summer 2012     
*/


//  VARIABLES
int pulsePin = 0;          // pulse sensor purple wire connected to analog pin 0
int fadeRate = 0;          // used to fade LED on PWM pin 11  ** Connecting solenoid to pin 11

                                    // these are volatile because they are used during the interrupt!
volatile int BPM;                   // used to hold the pulse rate
volatile int Signal;                // holds the incoming raw data
volatile int HRV;                   // holds the time between beats
volatile boolean Pulse = false;     // true when pulse wave is high, false when it's low
volatile boolean QS = false;        // becomes true when pulse rate is determined. every 20 pulses


void setup(){
  pinMode(13,OUTPUT);    // pin 13 will blink to your heartbeat!
  pinMode(11,OUTPUT);    // pin 11 will fade to your heartbeat!
  Serial.begin(115200);  // we agree to talk fast!
  interruptSetup();      // sets up to read Pulse Sensor signal every 1mS 
   // UN-COMMENT THE NEXT LINE IF YOU ARE POWERING THE PulseSensor AT LOW VOLTAGE, 
   // AND APPLY THAT VOLTAGE TO THE A-REF PIN
   //analogReference(EXTERNAL);   
}



void loop(){

  sendDataToProcessing('S', Signal);   // send Processing the raw Pulse Sensor data
  
  if (QS == true){                     // Quantified Self flag is true when arduino finds a heartbeat
    fadeRate = 255;                    // Set 'fadeRate' Variable to 255 to fade LED with pulse
    sendDataToProcessing('B',BPM);     // send the time between beats with a 'B' prefix
    sendDataToProcessing('Q',HRV);     // send heart rate with a 'Q' prefix
    QS = false;                        // reset the Quantified Self flag for next time    
   }

   ledFadeToBeat();  
   
   delay(20);                          //  take a break

}


void ledFadeToBeat(){
  fadeRate -= 15;                         //  set LED fade value
  fadeRate = constrain(fadeRate,0,255);   //  keep LED fade value from going into negative numbers!
  analogWrite(11,fadeRate*1.2);               //  fade LED mod bosted for toroid jakub for toroidal
}


void sendDataToProcessing(char symbol, int data ){
  Serial.print(symbol);      // symbol prefix tells Processing what type of data is coming
  Serial.println(data);      // the data to send
}

Interrupt part of code:

volatile int rate[10];                    // used to get running average of HRV values
volatile unsigned long lastBeatTime = 0;  // used to find the time between beats
volatile int sampleCounter;               // used to determine pulse timing
volatile int runningTotal;                // used to keep track of pulses
volatile boolean firstBeat = true;        // used to seed rate array so we startup with reasonable BPM
volatile boolean secondBeat = true;       // used to seed rate array so we startup with reasonable BPM


void interruptSetup(){

  // Initializes Timer1 to throw an interrupt every 1mS.
  TCCR1A = 0x00; // DISABLE OUTPUTS AND PWM ON DIGITAL PINS 9 & 10
  TCCR1B = 0x11; // GO INTO 'PHASE AND FREQUENCY CORRECT' MODE, NO PRESCALER
  TCCR1C = 0x00; // DON'T FORCE COMPARE
  TIMSK1 = 0x01; // ENABLE OVERFLOW INTERRUPT (TOIE1)
  ICR1 = 8000;   // TRIGGER TIMER INTERRUPT EVERY 1mS  
  sei();         // MAKE SURE GLOBAL INTERRUPTS ARE ENABLED
 
}


// THIS IS THE TIMER 1 INTERRUPT SERVICE ROUTINE. 
ISR(TIMER1_OVF_vect){ // triggered every time Timer 1 overflows
  // Timer 1 makes sure that we take a reading every milisecond
  Signal = analogRead(pulsePin);  // read the Pulse Sensor 
  sampleCounter++;                // keep track of the time with this variable (ISR triggered every 1mS

  //  NOW IT'S TIME TO LOOK FOR THE HEART BEAT
  int H = sampleCounter-lastBeatTime;      // monitor the time since the last beat to avoid noise
  if ( (Signal > 520) && (Pulse == false) && (H > 500) ){  
    // signal surges up in value every time there is a pulse    
    Pulse = true;                       // set the Pulse flag when we think there is a pulse
    digitalWrite(13,HIGH);              // turn on pin 13 LED
    HRV = sampleCounter - lastBeatTime; // measure time between beats in mS
    lastBeatTime = sampleCounter;       // keep track of time for next pulse
    if(firstBeat){                      // if it's the first time we found a beat
      firstBeat = false;                // clear firstBeat flag
      return;                           // HRV value is unreliable so discard it
    }   
    if(secondBeat){                     // if this is the second beat
      secondBeat = false;               // clear secondBeat flag
      for(int i=0; i<=9; i++){          // seed the running total to get a realisitic BPM at startup
        rate[i] = HRV;                      
      }
    }                          
    // keep a running total of the last 10 HRV values
    for(int i=0; i<=8; i++){
      rate[i] = rate[i+1];          // shift data in the rate array and drop the oldest HRV value 
    }
  rate[9] = HRV;                    // add the latest HRV to the rate array
  runningTotal = 0;                 // clear the runningTotal variable
  for(int i=0; i<=9; i++){
    runningTotal += rate[i];        // add up the last 10 HRV values
  }  
  runningTotal /= 10;               // average the last 10 HRV values 
  BPM = 60000/runningTotal;         // how many beats can fit into a minute? that's BPM!
  QS = true;                        // set Quantified Self flag when beat is found and BPM gets updated. 
  // QS FLAG IS NOT CLEARED INSIDE THIS ISR
  }                       
 

  if (Signal < 500 && Pulse == true){   // when the values are going down, it's the time between beats
    digitalWrite(13,LOW);               // turn off pin 13 LED
    Pulse = false;                      // reset the Pulse flag so we can do it again!
  }
  
}// end isr

Okay, I simplified my posting, hope this will help me get some responses.. thank you!

The way I see it is you have two choices. Wrap up all that into a class, then make 2 of them, or, double up on everything, literally every used variable needs to become an array of 2.

Also this bit of code looks dodgy:

void ledFadeToBeat(){
  fadeRate -= 15;                         //  set LED fade value
  fadeRate = constrain(fadeRate,0,255);   //  keep LED fade value from going into negative numbers!
  analogWrite(11,fadeRate*1.2);               //  fade LED mod bosted for toroid jakub for toroidal
}

fadeRate can be set to 255, it comes in and 15 is deducted, we are now at 240, we then constrain this to the range 0-255, which its already within, we then multiply it by 1.2 which is 288 (240*1.2 = 288), then pass it into a function that takes the range 0-255.

Well I'm not an experienced programmer so I don't know exactly how that dodgy code works, but all I know is that it does. It works great when it's just one sensor to one solenoid. I just need to get it to be sensor A triggers solenoid A, and sensor B triggers solenoid B. I have the whole hardware circuit side figured out, now it is just this code :confused:

I don't know how an array works, but from what I could kind of gather from Arduino's explanation, is that it would like combine them and not be able to separate between solenoids...
After researching I still don't understand how I would create a class for this or how that would work either.

This is my attempt, but it seems to either be combining the sensor data, or just reading one sensor for both solenoids. I think it has something to do with the "Signal" part of the code. How do I have the data signal from the sensors calculated separately??:

/*
>> Pulse Sensor Amped <<
This code is for Pulse Sensor Amped by Joel Murphy and Yury Gitman
    www.pulsesensor.com 
    >>> Pulse Sensor purple wire goes to Analog Pin 0 <<<
Pulse Sensor sample aquisition and processing happens in the background via Timer 1 interrupt. 1mS sample rate.
PWM on pins 9 and 10 will not work when using this code!
The following variables are automatically updated:
Pulse :     boolean that is true when a heartbeat is sensed then false in time with pin13 LED going out.
Signal :    int that holds the analog signal data straight from the sensor. updated every 1mS.
HRV  :      int that holds the time between the last two beats. 1mS resolution.
BPM  :      int that holds the heart rate value. derived every pulse from averaging previous 10 HRV values.
QS  :       boolean that is made true whenever Pulse is found and BPM is updated. User must reset.

This code is designed with output serial data to Processing sketch "PulseSensorAmped_Processing-xx"
The Processing sketch is a simple data visualizer. 
All the work to find the heartbeat and determine the heartrate happens in the code below.
Pin 13 LED will blink with heartbeat.
It will also fade an LED on pin 11 with every beat. Put an LED and series resistor from pin 11 to GND

See the README for more information and known issues.
Code Version 0.1 by Joel Murphy & Yury Gitman  Summer 2012     
*/


//  VARIABLES
int pulsePin = 0;          // pulse sensor purple wire connected to analog pin 0
int pulsePin2 = 1;
int fadeRate = 0;          // used to fade LED on PWM pin 11
int fadeRate2 = 0;     // fade to pin 5?

                                    // these are volatile because they are used during the interrupt!
volatile int BPM;                   // used to hold the pulse rate
volatile int Signal;                // holds the incoming raw data
volatile int HRV;                   // holds the time between beats
volatile boolean Pulse = false;     // true when pulse wave is high, false when it's low
volatile boolean QS = false;        // becomes true when pulse rate is determined. every 20 pulses


void setup(){
  pinMode(13,OUTPUT);    // pin 13 will blink to your heartbeat!
  pinMode(11,OUTPUT);    // pin 11 will fade to your heartbeat!
  pinMode(5,OUTPUT);  // pin 10 fade
  Serial.begin(115200);  // we agree to talk fast!
  interruptSetup();      // sets up to read Pulse Sensor signal every 1mS 
   // UN-COMMENT THE NEXT LINE IF YOU ARE POWERING THE PulseSensor AT LOW VOLTAGE, 
   // AND APPLY THAT VOLTAGE TO THE A-REF PIN
   //analogReference(EXTERNAL);   
}



void loop(){

  sendDataToProcessing('S', Signal);   // send Processing the raw Pulse Sensor data
  
  if (QS == true){                     // Quantified Self flag is true when arduino finds a heartbeat
    fadeRate = 255;                    // Set 'fadeRate' Variable to 255 to fade LED with pulse
    fadeRate2 = 255;
    sendDataToProcessing('B',BPM);     // send the time between beats with a 'B' prefix
    sendDataToProcessing('Q',HRV);     // send heart rate with a 'Q' prefix
    QS = false;                        // reset the Quantified Self flag for next time    
   }

   ledFadeToBeat();  
   
   delay(20);                          //  take a break

}


void ledFadeToBeat(){
  fadeRate -= 15;                         //  set LED fade value
  fadeRate = constrain(fadeRate,0,255);   //  keep LED fade value from going into negative numbers!
  fadeRate2 -= 15;                         //  set LED fade value
  fadeRate2 = constrain(fadeRate2,0,255);   //  keep LED fade value from going into negative numbers!
  analogWrite(11,fadeRate*1.2);               //  fade LED mod BOOSTED FOR SOLENOID A
   analogWrite(5,fadeRate2*1.2);     //  fade LED mod BOOSTED FOR SOLENOID B?
}


void sendDataToProcessing(char symbol, int data ){
  Serial.print(symbol);      // symbol prefix tells Processing what type of data is coming
  Serial.println(data);      // the data to send
}

Interrupt part:

volatile int rate[10];                    // used to get running average of HRV values
volatile unsigned long lastBeatTime = 0;  // used to find the time between beats
volatile int sampleCounter;               // used to determine pulse timing
volatile int runningTotal;                // used to keep track of pulses
volatile boolean firstBeat = true;        // used to seed rate array so we startup with reasonable BPM
volatile boolean secondBeat = true;       // used to seed rate array so we startup with reasonable BPM


void interruptSetup(){

  // Initializes Timer1 to throw an interrupt every 1mS.
  TCCR1A = 0x00; // DISABLE OUTPUTS AND PWM ON DIGITAL PINS 9 & 10
  TCCR1B = 0x11; // GO INTO 'PHASE AND FREQUENCY CORRECT' MODE, NO PRESCALER
  TCCR1C = 0x00; // DON'T FORCE COMPARE
  TIMSK1 = 0x01; // ENABLE OVERFLOW INTERRUPT (TOIE1)
  ICR1 = 8000;   // TRIGGER TIMER INTERRUPT EVERY 1mS  
  sei();         // MAKE SURE GLOBAL INTERRUPTS ARE ENABLED
 
}


// THIS IS THE TIMER 1 INTERRUPT SERVICE ROUTINE. 
ISR(TIMER1_OVF_vect){ // triggered every time Timer 1 overflows
  // Timer 1 makes sure that we take a reading every milisecond
  Signal = analogRead(pulsePin);  // read the Pulse Sensor 
  Signal = analogRead(pulsePin2); //??
  sampleCounter++;                // keep track of the time with this variable (ISR triggered every 1mS

  //  NOW IT'S TIME TO LOOK FOR THE HEART BEAT
  int H = sampleCounter-lastBeatTime;      // monitor the time since the last beat to avoid noise
  if ( (Signal > 520) && (Pulse == false) && (H > 500) ){  
    // signal surges up in value every time there is a pulse    
    Pulse = true;                       // set the Pulse flag when we think there is a pulse
    digitalWrite(13,HIGH);              // turn on pin 13 LED
    HRV = sampleCounter - lastBeatTime; // measure time between beats in mS
    lastBeatTime = sampleCounter;       // keep track of time for next pulse
    if(firstBeat){                      // if it's the first time we found a beat
      firstBeat = false;                // clear firstBeat flag
      return;                           // HRV value is unreliable so discard it
    }   
    if(secondBeat){                     // if this is the second beat
      secondBeat = false;               // clear secondBeat flag
      for(int i=0; i<=9; i++){          // seed the running total to get a realisitic BPM at startup
        rate[i] = HRV;                      
      }
    }                          
    // keep a running total of the last 10 HRV values
    for(int i=0; i<=8; i++){
      rate[i] = rate[i+1];          // shift data in the rate array and drop the oldest HRV value 
    }
  rate[9] = HRV;                    // add the latest HRV to the rate array
  runningTotal = 0;                 // clear the runningTotal variable
  for(int i=0; i<=9; i++){
    runningTotal += rate[i];        // add up the last 10 HRV values
  }  
  runningTotal /= 10;               // average the last 10 HRV values 
  BPM = 60000/runningTotal;         // how many beats can fit into a minute? that's BPM!
  QS = true;                        // set Quantified Self flag when beat is found and BPM gets updated. 
  // QS FLAG IS NOT CLEARED INSIDE THIS ISR
  }                       
 

  if (Signal < 500 && Pulse == true){   // when the values are going down, it's the time between beats
    digitalWrite(13,LOW);               // turn off pin 13 LED
    Pulse = false;                      // reset the Pulse flag so we can do it again!
  }
  
}// end isr

Can anyone please explain how I would make this a class/library? I have watched tutorials but still don't understand, especially when this sketch uses multiple files.