Help to put 3 codes together!

Hello, im trying to put 3 codes into one sketch onto arduino UNO and to show all info neatly placed onto OLED 128x64 display. Though, i tried to put together a heart rate sensor and temperature sensor sketches into one, it only displays 'no data' (thats what i wrote in to say when there is no BPM sensor). Can anyone help me put these 3 codes into a single one in a way, that they dont fight with each other while displaying their separate info on single oled display??

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SPI.h>
 //Connections for the OLED display
#define sclk 13 //SCL  (blue wire)
#define mosi 11 //SDA  (white wire)
#define cs 10 //CS   (grey wire)
#define rst 9 //RES  (green wire)
#define dc 8 //DC   (yellow wire)
#define LOGtime 1000 //Logging time in milliseconds (1 second)
#define Minute 60000 //the period of 1 minute for calculating the CPM values
#define show endWrite
#define clear() fillScreen(0)
// Color definitions
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
Adafruit_SSD1306 display = Adafruit_SSD1306(cs, dc, rst);
int Counts = 0; //variable containing the number of GM Tube events withinthe LOGtime
unsigned long previousMillis= 0; //variablefor storing the previous time
int AVGCPM = 0; //variable containing the floating average number ofcounts over a fixed moving windo period
int TenSecCPM = 0;
int units = 0;
int tens = 0;
int hundreds = 0;
int thousands = 0;
float Sievert = 0;
int COUNTS[10]; // array for storing the measured amounts of impulses in10 consecutive 1 second periods
int t = 0;
////////////////////the setup code that follows,will run once after "Power On" or after a RESET///////////////////
void setup() {
 Serial.begin(115200);
 display.begin();
 
 
 for(int x = 0; x < 10 ; x++) { //put all data in the Array COUNTS to 0 (Array positionsrun from 0 to 10;
 COUNTS[x] = 0; //10 positions covering a period of 10 seconds
 }
 
 attachInterrupt(0, IMPULSE, FALLING); //define external interrupton pin D2/INT0 to start the interupt routine IMPULSE  (green wire)
 
 display.drawRect(0,0,96,64,WHITE);
 display.setCursor(4,4);
 display.setTextColor(RED);
 display.setTextSize(2);
 display.print("CPM");
 display.setCursor(50,4);
 display.setTextSize(1);
 display.print("10 sec");
 display.setCursor(50,12);
 display.print("window");
 
 display.setCursor(4,38);
 display.setTextSize(1);
 display.setTextColor(GREEN);
 display.print("uSv/hr");
 
 display.drawRect(0,48, 96, 16, YELLOW);
}
////////////////////////the loop code that follows,will run repeatedly until "Power Off" or a RESET/////////
void loop()
{
 unsignedlong currentMillis= millis();
 if(currentMillis - previousMillis >LOGtime)
 {
 previousMillis = currentMillis;
 COUNTS[t] = Counts;
 for (int y = 0; y < 10 ; y++) { //add all data in the Array COUNTS together
 TenSecCPM = TenSecCPM + COUNTS[y]; //and calculate the rolling average CPM over a 10 secondperiod
 }
 AVGCPM = 6* TenSecCPM;
 TenSecCPM = 0;
 t++ ;
 if (t > 9) { t = 0 ;}
 
 //Serial.print ("COUNTS "); Serial.print(t);Serial.print (" = ");Serial.println (COUNTS[t]);
 display.fillRect(4,20,90,17,BLACK); // clear the CPM value field on the display
 display.setCursor(4,20);
 display.setTextColor(RED);
 display.setTextSize(2);
 display.println(AVGCPM);
 //Serial.print ("AVGCPM = "); Serial.print(AVGCPM); //Serial.print ("   CPM = "); Serial.println(CPM);
 display.fillRect(45,38,50,10,BLACK); //clear the uSv/Hr value field on the display
 display.setCursor(45,38);
 display.setTextColor(GREEN);
 display.setTextSize(1);
 
 Sievert = (AVGCPM /151.0) ; //Serial.print ("  Sievert = ");Serial.println (Sievert);
 units = int (Sievert); //Serial.print ("units = "); Serial.println(units);
 tens = int ((10*Sievert) - (10*units)); //Serial.print ("tens = "); Serial.println(tens);
 hundreds = int ((100*Sievert) - (100*units) - (10* tens)); //Serial.print ("hundreds = "); Serial.println(hundreds);
 thousands = int ((1000*Sievert) - (1000*units) - (100*tens) - (10*hundreds)); //Serial.print ("thousands ="); Serial.println (thousands);
 display.print (units); display.print("."); display.print (tens); display.print (hundreds);display.println (thousands);
 
 display.fillRect(1,49,94,14,BLACK); // clear the CPM indicator field on the display
 display.fillRect(1,49,Counts,14,RED); //fill the CPM indicator field on the display
 
 Counts = 0;
 }
}
//////////////////END ofLOOP////////////////////////////////////
/////////////////////////////////Hereafter follows the Function for counting the number of impulses from Geiger Counter kit
void IMPULSE()
 {
 Counts++;
 }



////// This one has different display as far as i know

it only displays 'no data' (thats what i wrote in to say when there is no BPM sensor).

are you sure that you posted the full code?
there is not a single place where there is a string "no data" printed

best regards Stefan

What 3 codes?

I don't see anywhere in your sketch where you display 'no data'. Also, Counts should be declared with the volatile keyword and you should disable interrupts in loop() when reading or writing Counts.

[ltr][color=#222222][code]#include "DHT.h"
#define DHT11Pin 2
#define DHTType DHT11
//OLED
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

DHT HT(DHT11Pin,DHTType);
float humi;
float tempC;
float tempF;

//OLED define
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void setup() {
  Serial.begin(9600);
  //For DHT11
  HT.begin();
  //For OLED I2C
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  display.display(); //Display logo
  delay(1000);
  display.clearDisplay();
}

void loop() {
 delay(1000);
 humi = HT.readHumidity();
 tempC = HT.readTemperature();
 tempF = HT.readTemperature(true);

 Serial.print("Humidity:");
 display.setTextSize(2);
 display.setCursor(0, 30);
 Serial.print(humi,0);
 Serial.print("%");
 Serial.print("Temp");
 Serial.print(tempC,1);
 Serial.print("C");

 display.clearDisplay();
 oledDisplayHeader();
 

 oledDisplay(3,5,28,humi,"%");
 oledDisplay(2,70,16,tempC,"C");
 
 display.display();
 
}
void oledDisplayHeader(){
 display.setTextSize(2);
 display.setCursor(0, 0);
 display.print("Hum");
 display.setCursor(60, 0);
 display.print("Temp");
}
void oledDisplay(int size, int x,int y, float value, String unit){
 int charLen=12;
 int xo=x+charLen*3.2;
 int xunit=x+charLen*3.6;
 int xval = x;
 display.setTextSize(size);
 display.setTextColor(WHITE);
 
 if (unit=="%"){
   display.setCursor(x, y);
   display.print(value,0);
   display.print(unit);
 } else {
   if (value>99){
    xval=x;
   } else {
    xval=x+charLen;
   }
   display.setCursor(xval, y);
   display.print(value,0);
   display.drawCircle(xo, y+2, 2, WHITE);  // print degree symbols (  )
   display.setCursor(xunit, y);
   display.print(unit);
 }
 
}

#include <Adafruit_GFX.h> //OLED libraries
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include "MAX30105.h" //MAX3010x library
#include "heartRate.h" //Heart rate calculating algorithm

MAX30105 particleSensor;

const byte RATE_SIZE = 4; //Increase this for more averaging. 4 is good.
byte rates[RATE_SIZE]; //Array of heart rates
byte rateSpot = 0;
long lastBeat = 0; //Time at which the last beat occurred
float beatsPerMinute;
int beatAvg;

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //Declaring the display name (display)

void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display
display.display();
delay(2000);
// Initialize sensor
particleSensor.begin(Wire, I2C_SPEED_FAST); //Use default I2C port, 400kHz speed
particleSensor.setup(); //Configure sensor with default settings
particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running

}

void loop() {
long irValue = particleSensor.getIR(); //Reading the IR value it will permit us to know if there's a finger on the sensor or not
//Also detecting a heartbeat
if(irValue > 7000){ //If a finger is detected
display.clearDisplay(); //Clear the display
display.setTextSize(2); //Near it display the average BPM you can display the BPM if you want
display.setTextColor(WHITE);
display.setCursor(50,0);
display.println("BPM");
display.setCursor(50,18);
display.println(beatAvg);
display.display();

if (checkForBeat(irValue) == true) //If a heart beat is detected
{
display.clearDisplay(); //Clear the display
display.setTextSize(2); //And still displays the average BPM
display.setTextColor(WHITE);
display.setCursor(50,0);
display.println("BPM");
display.setCursor(50,18);
display.println(beatAvg);
display.display();
tone(3,1000); //And tone the buzzer for a 100ms you can reduce it it will be better
delay(100);
noTone(3); //Deactivate the buzzer to have the effect of a "bip"
//We sensed a beat!
long delta = millis() - lastBeat; //Measure duration between two beats
lastBeat = millis();

beatsPerMinute = 60 / (delta / 1000.0); //Calculating the BPM

if (beatsPerMinute < 255 && beatsPerMinute > 20) //To calculate the average we strore some values (4) then do some math to calculate the average
{
rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
rateSpot %= RATE_SIZE; //Wrap variable

//Take average of readings
beatAvg = 0;
for (byte x = 0 ; x < RATE_SIZE ; x++)
beatAvg += rates[x];
beatAvg /= RATE_SIZE;
}
}

}
if (irValue < 7000){ //If no finger is detected it inform the user and put the average BPM to 0 or it will be stored for the next measure
beatAvg=0;
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(30,5);
display.println("NoDATA");
display.display();
noTone(3);
}

}[/color][/ltr][/code]

Oops! You missed the code tags...

Sorry! Its my 2nd post, i dont really know how to properly use this site yet. And i didnt post all codes right away because, again, im new here, and i cant post quicker than each 5 mins

There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

StefanL38:
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

oh, thank you! il know next time! So, can anyone help me out with merging the sketches?

If you would post the full sketch including your printing 'no data" output so it becomes easy to just click on select to copy your full sketch into the clipboard and paste into the arduino-IDE for much easier examining your code instead of a code-section-window you might get help

best regards Stefan

dainyzas:
So, can anyone help me out with merging the sketches?

I would offer not to attempt to do all three at once. Do two and prove the result, then add the third and prove again.

https://arduinoinfo.mywikis.net/wiki/CombiningArduinoSketches

http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html

Here, i merged temperature/humidity and heartbeat sensor codes.

[code]
#include "DHT.h"
#define DHT11Pin 2
#define DHTType DHT11
//OLED
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "MAX30105.h"           //MAX3010x library
#include "heartRate.h"          //Heart rate calculating algorithm

MAX30105 particleSensor;

const byte RATE_SIZE = 4; //Increase this for more averaging. 4 is good.
byte rates[RATE_SIZE]; //Array of heart rates
byte rateSpot = 0;
long lastBeat = 0; //Time at which the last beat occurred
float beatsPerMinute;
int beatAvg;

DHT HT(DHT11Pin,DHTType);
float humi;
float tempC;
float tempF;

//OLED define
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void setup() {
  Serial.begin(9600);
  //For DHT11
  HT.begin();
  //For OLED I2C
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  display.display(); //Display logo
  delay(1000); 
  display.clearDisplay();
  // Initialize sensor
  particleSensor.begin(Wire, I2C_SPEED_FAST); //Use default I2C port, 400kHz speed
  particleSensor.setup(); //Configure sensor with default settings
  particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running
}

void loop() {
 delay(1000);
 humi = HT.readHumidity();
 tempC = HT.readTemperature();
 tempF = HT.readTemperature(true);

 Serial.print("Humidity:");
 display.setTextSize(2);
 display.setCursor(0, 30);
 Serial.print(humi,0);
 Serial.print("%");
 Serial.print("Temp");
 Serial.print(tempC,1);
 Serial.print("C");

 display.clearDisplay();
 oledDisplayHeader();
 

 oledDisplay(3,5,28,humi,"%");
 oledDisplay(2,70,16,tempC,"C");
 
 display.display(); 
 
}
void oledDisplayHeader(){
 display.setTextSize(2);
 display.setCursor(0, 0);
 display.print("Hum");
 display.setCursor(60, 0);
 display.print("Temp");
}
void oledDisplay(int size, int x,int y, float value, String unit){
 int charLen=12;
 int xo=x+charLen*3.2;
 int xunit=x+charLen*3.6;
 int xval = x; 
 display.setTextSize(size);
 display.setTextColor(WHITE);
 
 if (unit=="%"){
   display.setCursor(x, y);
   display.print(value,0);
   display.print(unit);
 } else {
   if (value>99){
    xval=x;
   } else {
    xval=x+charLen;
   }
   display.setCursor(xval, y);
   display.print(value,0);
   display.drawCircle(xo, y+2, 2, WHITE);  // print degree symbols (  )
   display.setCursor(xunit, y);
   display.print(unit);

{
 long irValue = particleSensor.getIR();    //Reading the IR value it will permit us to know if there's a finger on the sensor or not
                                           //Also detecting a heartbeat
if(irValue > 7000){                                           //If a finger is detected
    display.clearDisplay();                                   //Clear the display
    display.setTextSize(2);                                   //Near it display the average BPM you can display the BPM if you want
    display.setTextColor(WHITE); 
    display.setCursor(50,0);                
    display.println("BPM");             
    display.setCursor(50,18);                
    display.println(beatAvg); 
    display.display();
    
  if (checkForBeat(irValue) == true)                        //If a heart beat is detected
  {
    display.clearDisplay();                                //Clear the display
    display.setTextSize(2);                                //And still displays the average BPM
    display.setTextColor(WHITE);             
    display.setCursor(50,0);                
    display.println("BPM");             
    display.setCursor(50,18);                
    display.println(beatAvg); 
    display.display();
    tone(3,1000);                                        //And tone the buzzer for a 100ms you can reduce it it will be better
    delay(100);
    noTone(3);                                          //Deactivate the buzzer to have the effect of a "bip"
    //We sensed a beat!
    long delta = millis() - lastBeat;                   //Measure duration between two beats
    lastBeat = millis();

    beatsPerMinute = 60 / (delta / 1000.0);           //Calculating the BPM

    if (beatsPerMinute < 255 && beatsPerMinute > 20)               //To calculate the average we strore some values (4) then do some math to calculate the average
    {
      rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
      rateSpot %= RATE_SIZE; //Wrap variable

      //Take average of readings
      beatAvg = 0;
      for (byte x = 0 ; x < RATE_SIZE ; x++)
        beatAvg += rates[x];
      beatAvg /= RATE_SIZE;
    }
  }

}
  if (irValue < 7000){       //If no finger is detected it inform the user and put the average BPM to 0 or it will be stored for the next measure
     beatAvg=0;
     display.clearDisplay();
     display.setTextSize(1);                    
     display.setTextColor(WHITE);             
     display.setCursor(30,5);                
     display.println("NoDATA");   
     display.display();
     noTone(3);
     }

} }
 
}

[/code]

So does it work as expected? if not what error or unwanted behaviour does your code do?

And additionally make it a habit to press Ctrl-T every few minutes for automatic indention of the code
It will make you recongnize misplaced curly brackets.

You are way too lazy with formatting code. There is a reason why you should format your code pretty close to ideal
Of course the compiler doesn't care as long as there are no syntax-errors but you the user keep a much better overview
than without clean formatting.

You may think I don't have any overview of the code. Well then it is even more imprtant.
best regards Stefan

These 2 while compiled, seem to work, it just displays 'no data' and thats all. It should say "%" and "C", as well as "Hum" and "Temp" along side it, but it doesnt. I have a feeling one code might overpower the other with 'cleardisplay' or something, but i have no clue... Its almost as if they cant or dont want to share one screen

So next step is to deleting empty lines
and then really press Ctrl-T for autoformatting your code.

You will notice a significant change in the code's indention

examining your code for the too much inserted curly brackets
which already might solve the problem

best regards Stefan

It sort of worked. The OLED display still shows 'no data'

such short answers do not help analysing the problem.
As I don't have your hardware to re-build everything I depend on your input.

Each time you do some tests you have to describe in details all the steps that you are doing.
I have no chystal-sphere to watch from distance.

Post the actual version of your code From the very first to very last line using this method

just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

How much do you know about programming serial output?

"I mean do you know what it means do add serial-output to your code?"

best regards Stefan

It seems to me that you a problem managing what the display should be showing.

In your oledDisplay function, you show temperature or humidity, whichever was passed. However, in the same function you immediately try to show BPM which clears the display so you never notice the other data.

I assume that you don't have your finger on the sensor since it's showing NoData.

You will need to figure out when to show BPM and when to show the other data. Or if your display has room for all of them, don't keep clearing the display.

wildbill:
It seems to me that you a problem managing what the display should be showing.

In your oledDisplay function, you show temperature or humidity, whichever was passed. However, in the same function you immediately try to show BPM which clears the display so you never notice the other data.

I assume that you don't have your finger on the sensor since it's showing NoData.

You will need to figure out when to show BPM and when to show the other data. Or if your display has room for all of them, don't keep clearing the display.

Alright, so i just have to delete 'cleardisplay'? il try that now. thanks. Tho wont it just overlap different numbers till its just a blob of numbers??

StefanL38:
such short answers do not help analysing the problem.
As I don't have your hardware to re-build everything I depend on your input.

Each time you do some tests you have to describe in details all the steps that you are doing.
I have no chystal-sphere to watch from distance.

Post the actual version of your code From the very first to very last line using this method

just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

How much do you know about programming serial output?

"I mean do you know what it means do add serial-output to your code?"

best regards Stefan

Here you go, my code. It now works, and there isnt much to describe. The screen just doesnt display everything i need all at once. But i think ive figured this out already

#include "DHT.h"
#define DHT11Pin 2
#define DHTType DHT11
//OLED
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "MAX30105.h"           //MAX3010x library
#include "heartRate.h"          //Heart rate calculating algorithm
MAX30105 particleSensor;
const byte RATE_SIZE = 4; //Increase this for more averaging. 4 is good.
byte rates[RATE_SIZE]; //Array of heart rates
byte rateSpot = 0;
long lastBeat = 0; //Time at which the last beat occurred
float beatsPerMinute;
int beatAvg;
DHT HT(DHT11Pin, DHTType);
float humi;
float tempC;
float tempF;
//OLED define
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
  Serial.begin(9600);
  //For DHT11
  HT.begin();
  //For OLED I2C
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }
  display.display(); //Display logo
  delay(1000);
  display.clearDisplay();
  // Initialize sensor
  particleSensor.begin(Wire, I2C_SPEED_FAST); //Use default I2C port, 400kHz speed
  particleSensor.setup(); //Configure sensor with default settings
  particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running
}
void loop() {
  delay(1000);
  humi = HT.readHumidity();
  tempC = HT.readTemperature();
  tempF = HT.readTemperature(true);
  Serial.print("Humidity:");
  display.setTextSize(2);
  display.setCursor(0, 30);
  Serial.print(humi, 0);
  Serial.print("%");
  Serial.print("Temp");
  Serial.print(tempC, 1);
  Serial.print("C");
  display.clearDisplay();
  oledDisplayHeader();
  oledDisplay(3, 5, 28, humi, "%");
  oledDisplay(2, 70, 16, tempC, "C");
  display.display();
}
void oledDisplayHeader() {
  display.setTextSize(2);
  display.setCursor(0, 0);
  display.print("Hum");
  display.setCursor(60, 0);
  display.print("Temp");
}
void oledDisplay(int size, int x, int y, float value, String unit) {
  int charLen = 12;
  int xo = x + charLen * 3.2;
  int xunit = x + charLen * 3.6;
  int xval = x;
  display.setTextSize(size);
  display.setTextColor(WHITE);
  if (unit == "%") {
    display.setCursor(x, y);
    display.print(value, 0);
    display.print(unit);
  } else {
    if (value > 99) {
      xval = x;
    } else {
      xval = x + charLen;
    }
    display.setCursor(xval, y);
    display.print(value, 0);
    display.drawCircle(xo, y + 2, 2, WHITE); // print degree symbols (  )
    display.setCursor(xunit, y);
    display.print(unit);
    {
      long irValue = particleSensor.getIR();    //Reading the IR value it will permit us to know if there's a finger on the sensor or not
      //Also detecting a heartbeat
      if (irValue > 7000) {                                         //If a finger is detected
        //Clear the display
        display.setTextSize(2);                                   //Near it display the average BPM you can display the BPM if you want
        display.setTextColor(WHITE);
        display.setCursor(50, 0);
        display.println("BPM");
        display.setCursor(50, 18);
        display.println(beatAvg);
        display.display();
        if (checkForBeat(irValue) == true)                        //If a heart beat is detected
        {
          //Clear the display
          display.setTextSize(2);                                //And still displays the average BPM
          display.setTextColor(WHITE);
          display.setCursor(50, 0);
          display.println("BPM");
          display.setCursor(50, 18);
          display.println(beatAvg);
          display.display();
          tone(3, 1000);                                       //And tone the buzzer for a 100ms you can reduce it it will be better
          delay(100);
          noTone(3);                                          //Deactivate the buzzer to have the effect of a "bip"
          //We sensed a beat!
          long delta = millis() - lastBeat;                   //Measure duration between two beats
          lastBeat = millis();
          beatsPerMinute = 60 / (delta / 1000.0);           //Calculating the BPM
          if (beatsPerMinute < 255 && beatsPerMinute > 20)               //To calculate the average we strore some values (4) then do some math to calculate the average
          {
            rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array
            rateSpot %= RATE_SIZE; //Wrap variable
            //Take average of readings
            beatAvg = 0;
            for (byte x = 0 ; x < RATE_SIZE ; x++)
              beatAvg += rates[x];
            beatAvg /= RATE_SIZE;
          }
        }
      }
      if (irValue < 7000) {      //If no finger is detected it inform the user and put the average BPM to 0 or it will be stored for the next measure
        beatAvg = 0;
        display.setTextSize(1);
        display.setTextColor(WHITE);
        display.setCursor(30, 5);
        display.println("NoDATA");
        display.display();
        noTone(3);
      }
    }
  }
}