Pulse Sensor & SSD1331 & Arduino Uno Board

Hello,
I am new to arduino and I am having problems with the code I am using. I can have a pulse sensor and a OLED SSD1331 attached.

I am using this program:
#include <Wire.h>
#include <U8glib.h>
#include <string.h>
U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);
const int WIDTH=128; //width of OLED screen is 128 pixels.
const int HEIGHT=35; //set height of graph
const int LENGTH=WIDTH; //size of the y array
const int analogInPin = A0;
int x;
int y[LENGTH];
// Volatile Variables, used in the interrupt service routine
volatile int BPM; // int that holds raw Analog in 0. updated every 2mS
volatile int Signal; // holds the incoming raw data
volatile int IBI = 600; // int that holds the time interval between beats!Must be seeded!
volatile boolean Pulse = false; // “True” when User’s live heartbeat is detected.“False” when not a “live beat”.
volatile boolean QS = false; // becomes true when Arduoino finds a beat.
volatile int rate[10]; // array to hold last ten IBI values
volatile unsigned long sampleCounter = 0; // used to determine pulse timing
volatile unsigned long lastBeatTime = 0; // used to find IBI
volatile int P =512; // used to find peak in pulse wave, seeded
volatile int T = 512; // used to find trough in pulse wave, seeded
volatile int thresh = 525; // used to find instant moment of heart beat,seeded
volatile int amp = 100; // used to hold amplitude of pulse waveform,seeded
volatile boolean firstBeat = true; // used to seed rate array so we startup withreasonable BPM
volatile boolean secondBeat = false; // used to seed rate array so we startup with reasonable BPM
void clearY(){ // Clears graph. Starts with first element, check if i is less than the length of the array, increment i
for(int i=0; i<LENGTH; i++){
y = -1; // Make the y array empty
}
}
void draw() { // Prints BPM and IBI on screen with designated font.
u8g.setFont(u8g_font_unifont);
char bpmStr[3]; // Declares a char* of size 3
char bpmOutput[81]; // Declares a char* of size 81 (this can probablybe smaller and still work fine)
strcpy(bpmOutput, “BPM:”); // Copies “BPM:” into the bpmOutput char*
sprintf(bpmStr,"%d", BPM); // Converts the integer BPM into a string andstore it in bpmStr (size 3)
strcat(bpmOutput, bpmStr); // Puts bpmStr at the end of bpmOutput (resultsin a string of “BPM:” followed by bpmStr. This stores the combined char* inside output.Output must be long enough (81 is much more than long enough).
u8g.drawStr( 0, 64, bpmOutput); // Draws final output string
char ibiStr[3]; // Same procedure to concatenate IBI value to “IBI:”
char ibiOutput[81];
strcpy(ibiOutput, “IBI:”);
sprintf(ibiStr, “%d”, IBI);
strcat(ibiOutput, ibiStr);
u8g.drawStr(62, 64, ibiOutput);
}
void drawY(){
u8g.drawPixel(0, y[0]);
for(int x=1; x<LENGTH; x++){
if(y[x]!=-1){ // if the y array is not empty
u8g.drawLine(x-1, y[x-1], x, y[x]); // Draws line from point (i-1, y[i-1]) to point(i,y*). (0, y[0], 1, y[1]) → (1, y[1], 2, y[2])…*
}else{ // if a value does not exist in the y array, drawY() function stops
break;
}
}
}
void interruptSetup(){
// Initializes Timer2 to throw an interrupt every 2mS.
TCCR2A = 0x02; // DISABLE PWM ON DIGITAL PINS 3 AND 11,AND GO INTO CTC MODE
TCCR2B = 0x06; // DON’T FORCE COMPARE, 256 PRESCALER
OCR2A = 0X7C; // SET THE TOP OF THE COUNT TO 124 FOR500Hz SAMPLE RATE
TIMSK2 = 0x02; // ENABLE INTERRUPT ON MATCH BETWEEN TIMER2 AND OCR2A
sei(); // MAKE SURE GLOBAL INTERRUPTS ARE ENABLED
}
// THIS IS THE TIMER 2 INTERRUPT SERVICE ROUTINE.
// Timer 2 makes sure that we take a reading every 2 miliseconds
ISR(TIMER2_COMPA_vect){ // triggered when Timer2 counts to 124
cli(); // disable interrupts while we do this, otherwise function might interrupt itself.
Signal = analogRead(analogInPin); // read the Pulse Sensor
sampleCounter += 2; // keep track of the time in milliseconds with this variable
int N = sampleCounter - lastBeatTime; // monitor the time since the last beat to avoid noise
// find the peak and trough of the pulse wave
if(Signal < thresh && N > (IBI/5)*3){ // avoid dicrotic noise by waiting 3/5 of last IBI
if (Signal < T){ // T is the trough
T = Signal; // keep track of lowest point in pulse wave
}
}
if(Signal > thresh && Signal > P){ // thresh condition helps avoid noise
P = Signal; // P is the peak
} // keep track of highest point in pulse wave
// LOOK FOR THE HEART BEAT
// signal surges up in value every time there is a pulse
if (N > 250){ // avoid high frequency noise
if ( (Signal > thresh) && (Pulse == false) && (N > (IBI/5)*3) ){
Pulse = true; // set the Pulse flag when we think there is a pulse
IBI = sampleCounter - lastBeatTime; // measure time between beats in mS
lastBeatTime = sampleCounter; // keep track of time for next pulse
if(secondBeat){ // if this is the second beat, if secondBeat == TRUE
secondBeat = false; // clear secondBeat flag
for(int i=0; i<=9; i++){ // seed the running total to get a realisitic BPM at startup
rate = IBI;
}
}
if(firstBeat){ // if it’s the first time we found a beat, if firstBeat == TRUE
firstBeat = false; // clear firstBeat flag
secondBeat = true; // set the second beat flag
sei(); // enable interrupts again
return;
}
// keep a running total of the last 10 IBI values
word runningTotal = 0; // clear the runningTotal variable
for(int i=0; i<=8; i++){ // shift data in the rate array
rate = rate[i+1]; // and drop the oldest IBI value
runningTotal += rate*; // add up the 9 oldest IBI values*
}
rate[9] = IBI; // add the latest IBI to the rate array
runningTotal += rate[9]; // add the latest IBI to runningTotal
runningTotal /= 10; // average the last 10 IBI values and store value in runningTotal
BPM = 60000/runningTotal; // how many beats can fit into a minute? that’s BPM!
QS = true; // set Quantified Self flag
// QS FLAG IS NOT CLEARED INSIDE THIS ISR
}
}
if (Signal < thresh && Pulse == true){ // when the values are going down, the beat is over
Pulse = false; // reset the Pulse flag so we can do it again
amp = P - T; // get amplitude of the pulse wave
thresh = amp/2 + T; // set thresh at 50% of the amplitude
P = thresh; // reset these for next time
T = thresh;
}
if (N > 2500){ // if 2.5 seconds go by without a beat
thresh = 512; // set thresh default
P = 512; // set P default
T = 512; // set T default
lastBeatTime = sampleCounter; // bring the lastBeatTime up to date
firstBeat = true; // set these to avoid noise
secondBeat = false; // when we get the heartbeat back
BPM = 0; // BPM is 0 when there is no heartbeat detected
IBI = 0; // IBI is 0 when there is no heartbeat detected
}
sei(); // enable interrupts at the end
} // end isr
void setup() {
x = 0; // initialize x at 0 so that the graph starts on the left side of OLED
for(int x = 0; x < LENGTH; x++){
y[x] = 35;
}
interruptSetup(); // set up timer2
// IMPORTANT!
// It allows to set the time and date of the clock.
// Once configured, delete (or comment) the code and rewrite the code in the Arduino.
// Otherwise whenever you turn on the Arduino will modify the time.
}
void loop() {
if (QS = true) {
y[x] = map(Signal, 0, 1023, HEIGHT, 15); // Maps Signal on screen. 0 is mapped at Height, 1023 is mapped at 0.
u8g.firstPage(); // u8glib picture loop procedure starts.
do {
drawY();
draw();
} while( u8g.nextPage() );
x++; // Increments x
if(x >= WIDTH){
x = 0; // Reset x back to 0
clearY();
}
} QS = false;
} //end of picture loop.
I dont know what is wrong, I wanted to program it so the pulse reading is seen on the screen but it does not show up.
Please Help!

Please add code tags to your post - it is illegible.

Okay Sorry about that!

#include <Wire.h>
#include <U8glib.h>
#include <string.h>
U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);
const int WIDTH=128; //width of OLED screen is 128 pixels.
const int HEIGHT=35; //set height of graph
const int LENGTH=WIDTH; //size of the y array
const int analogInPin = A0;
int x;
int y[LENGTH];
// Volatile Variables, used in the interrupt service routine
volatile int BPM; // int that holds raw Analog in 0. updated every 2mS
volatile int Signal; // holds the incoming raw data
volatile int IBI = 600; // int that holds the time interval between beats!Must be seeded!
volatile boolean Pulse = false; // "True" when User's live heartbeat is detected."False" when not a "live beat".
volatile boolean QS = false; // becomes true when Arduoino finds a beat.
volatile int rate[10]; // array to hold last ten IBI values
volatile unsigned long sampleCounter = 0; // used to determine pulse timing
volatile unsigned long lastBeatTime = 0; // used to find IBI
volatile int P =512; // used to find peak in pulse wave, seeded
volatile int T = 512; // used to find trough in pulse wave, seeded
volatile int thresh = 525; // used to find instant moment of heart beat,seeded
volatile int amp = 100; // used to hold amplitude of pulse waveform,seeded
volatile boolean firstBeat = true; // used to seed rate array so we startup withreasonable BPM
volatile boolean secondBeat = false; // used to seed rate array so we startup with reasonable BPM
void clearY(){ // Clears graph. Starts with first element, check if i is less than the length of the array, increment i
 for(int i=0; i<LENGTH; i++){
 y[i] = -1; // Make the y array empty
 }
}
void draw() { // Prints BPM and IBI on screen with designated font.
 u8g.setFont(u8g_font_unifont);
 char bpmStr[3]; // Declares a char* of size 3
 char bpmOutput[81]; // Declares a char* of size 81 (this can probablybe smaller and still work fine)
 strcpy(bpmOutput, "BPM:"); // Copies "BPM:" into the bpmOutput char*
 sprintf(bpmStr,"%d", BPM); // Converts the integer BPM into a string andstore it in bpmStr (size 3)
 strcat(bpmOutput, bpmStr); // Puts bpmStr at the end of bpmOutput (resultsin a string of "BPM:" followed by bpmStr. This stores the combined char* inside output.Output must be long enough (81 is much more than long enough).
 u8g.drawStr( 0, 64, bpmOutput); // Draws final output string
 char ibiStr[3]; // Same procedure to concatenate IBI value to "IBI:"
 char ibiOutput[81];
 strcpy(ibiOutput, "IBI:");
 sprintf(ibiStr, "%d", IBI);
 strcat(ibiOutput, ibiStr);
 u8g.drawStr(62, 64, ibiOutput);
}
void drawY(){

 u8g.drawPixel(0, y[0]);
 for(int x=1; x<LENGTH; x++){
 if(y[x]!=-1){ // if the y array is not empty
 u8g.drawLine(x-1, y[x-1], x, y[x]); // Draws line from point (i-1, y[i-1]) to point(i,y[i]). (0, y[0], 1, y[1]) -> (1, y[1], 2, y[2])...
 }else{ // if a value does not exist in the y array, drawY() function stops
 break;
 }
 }
}
void interruptSetup(){
 // Initializes Timer2 to throw an interrupt every 2mS.
 TCCR2A = 0x02; // DISABLE PWM ON DIGITAL PINS 3 AND 11,AND GO INTO CTC MODE
 TCCR2B = 0x06; // DON'T FORCE COMPARE, 256 PRESCALER
 OCR2A = 0X7C; // SET THE TOP OF THE COUNT TO 124 FOR500Hz SAMPLE RATE
 TIMSK2 = 0x02; // ENABLE INTERRUPT ON MATCH BETWEEN TIMER2 AND OCR2A
 sei(); // MAKE SURE GLOBAL INTERRUPTS ARE ENABLED
}
// THIS IS THE TIMER 2 INTERRUPT SERVICE ROUTINE.
// Timer 2 makes sure that we take a reading every 2 miliseconds
ISR(TIMER2_COMPA_vect){ // triggered when Timer2 counts to 124
 cli(); // disable interrupts while we do this, otherwise function might interrupt itself.
 Signal = analogRead(analogInPin); // read the Pulse Sensor
 sampleCounter += 2; // keep track of the time in milliseconds with this variable
 int N = sampleCounter - lastBeatTime; // monitor the time since the last beat to avoid noise
 // find the peak and trough of the pulse wave
 if(Signal < thresh && N > (IBI/5)*3){ // avoid dicrotic noise by waiting 3/5 of last IBI
 if (Signal < T){ // T is the trough
 T = Signal; // keep track of lowest point in pulse wave
 }
 }
 if(Signal > thresh && Signal > P){ // thresh condition helps avoid noise
 P = Signal; // P is the peak
 } // keep track of highest point in pulse wave
 // LOOK FOR THE HEART BEAT
 // signal surges up in value every time there is a pulse
 if (N > 250){ // avoid high frequency noise
 if ( (Signal > thresh) && (Pulse == false) && (N > (IBI/5)*3) ){
 Pulse = true; // set the Pulse flag when we think there is a pulse
 IBI = sampleCounter - lastBeatTime; // measure time between beats in mS
 lastBeatTime = sampleCounter; // keep track of time for next pulse
 if(secondBeat){ // if this is the second beat, if secondBeat == TRUE
 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] = IBI;
 }
 }
 if(firstBeat){ // if it's the first time we found a beat, if firstBeat == TRUE
 firstBeat = false; // clear firstBeat flag
 secondBeat = true; // set the second beat flag
 sei(); // enable interrupts again
 return; // IBI value is unreliable so discard it, because there is no previous beat before the first beat to calculate the IBI.
 }
 // keep a running total of the last 10 IBI values
 word runningTotal = 0; // clear the runningTotal variable
 for(int i=0; i<=8; i++){ // shift data in the rate array
 rate[i] = rate[i+1]; // and drop the oldest IBI value
 runningTotal += rate[i]; // add up the 9 oldest IBI values
 }
 rate[9] = IBI; // add the latest IBI to the rate array
 runningTotal += rate[9]; // add the latest IBI to runningTotal
 runningTotal /= 10; // average the last 10 IBI values and store value in runningTotal
 BPM = 60000/runningTotal; // how many beats can fit into a minute? that's BPM!
 QS = true; // set Quantified Self flag
 // QS FLAG IS NOT CLEARED INSIDE THIS ISR
 }
 }
 if (Signal < thresh && Pulse == true){ // when the values are going down, the beat is over
 Pulse = false; // reset the Pulse flag so we can do it again
 amp = P - T; // get amplitude of the pulse wave
 thresh = amp/2 + T; // set thresh at 50% of the amplitude
 P = thresh; // reset these for next time
 T = thresh;
 }
 if (N > 2500){ // if 2.5 seconds go by without a beat
 thresh = 512; // set thresh default
 P = 512; // set P default
 T = 512; // set T default
 lastBeatTime = sampleCounter; // bring the lastBeatTime up to date
 firstBeat = true; // set these to avoid noise
 secondBeat = false; // when we get the heartbeat back
 BPM = 0; // BPM is 0 when there is no heartbeat detected
 IBI = 0; // IBI is 0 when there is no heartbeat detected
 }
 sei(); // enable interrupts at the end
} // end isr
void setup() {
 x = 0; // initialize x at 0 so that the graph starts on the left side of OLED
 for(int x = 0; x < LENGTH; x++){ // initialize y array so that every y is initially mapped at 35 the first time through
 y[x] = 35;
 }
 interruptSetup(); // set up timer2
 // IMPORTANT!
 // It allows to set the time and date of the clock.
 // Once configured, delete (or comment) the code and rewrite the code in the Arduino.
 // Otherwise whenever you turn on the Arduino will modify the time.
}
void loop() {

 if (QS = true) {
 y[x] = map(Signal, 0, 1023, HEIGHT, 15); // Maps Signal on screen. 0 is mapped at Height, 1023 is mapped at 0.
 u8g.firstPage(); // u8glib picture loop procedure starts.
 do {
 drawY();
 draw();
 } while( u8g.nextPage() );
 x++; // Increments x
 if(x >= WIDTH){ // when x value exceeds width of display
 x = 0; // Reset x back to 0
 clearY(); // clear graph
 }
 } QS = false;
} //end of picture loop.