Bahasa Pemrograman

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Bahasa Pemrograman

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include "MAX30105.h"
#include "heartRate.h"

MAX30105 particleSensor;

const int buttonPin = 2;

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;
int count;
int perCent;
int avgIr;

//Change this Value for Calibration


int irOffset = 1215;
long samplesTaken = 0; //Counter for calculating the Hz or read rate
long unblockedValue; //Average IR at power up
long startTime; //Used to calculate measurement rate

int ledState = HIGH;


int buttonState;
int lastButtonState = LOW;

unsigned long lastDebounceTime = 0;


unsigned long debounceDelay = 50;

#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)

static const unsigned char PROGMEM litleHeart[] =


{ 0x03, 0xC0, 0xF0, 0x06, 0x71, 0x8C, 0x0C, 0x1B, 0x06, 0x18, 0x0E, 0x02,
0x10, 0x0C, 0x03, 0x10,
0x04, 0x01, 0x10, 0x04, 0x01, 0x10, 0x40, 0x01, 0x10, 0x40, 0x01, 0x10, 0xC0,
0x03, 0x08, 0x88,
0x02, 0x08, 0xB8, 0x04, 0xFF, 0x37, 0x08, 0x01, 0x30, 0x18, 0x01, 0x90, 0x30,
0x00, 0xC0, 0x60,
0x00, 0x60, 0xC0, 0x00, 0x31, 0x80, 0x00, 0x1B, 0x00, 0x00, 0x0E, 0x00, 0x00,
0x04, 0x00, };

const unsigned char fullHeart [] PROGMEM =


{
0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x18, 0x00, 0x0f, 0xe0, 0x7f, 0x00, 0x3f,
0xf9, 0xff, 0xc0,
0x7f, 0xf9, 0xff, 0xc0, 0x7f, 0xff, 0xff, 0xe0, 0x7f, 0xff, 0xff, 0xe0, 0xff, 0xff,
0xff, 0xf0,
0xff, 0xf7, 0xff, 0xf0, 0xff, 0xe7, 0xff, 0xf0, 0xff, 0xe7, 0xff, 0xf0, 0x7f, 0xdb,
0xff, 0xe0,
0x7f, 0x9b, 0xff, 0xe0, 0x00, 0x3b, 0xc0, 0x00, 0x3f, 0xf9, 0x9f, 0xc0, 0x3f,
0xfd, 0xbf, 0xc0,
0x1f, 0xfd, 0xbf, 0x80, 0x0f, 0xfd, 0x7f, 0x00, 0x07, 0xfe, 0x7e, 0x00, 0x03,
0xfe, 0xfc, 0x00,
0x01, 0xff, 0xf8, 0x00, 0x00, 0xff, 0xf0, 0x00, 0x00, 0x7f, 0xe0, 0x00, 0x00,
0x3f, 0xc0, 0x00,
0x00, 0x0f, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00
};

static const unsigned char PROGMEM fingerTouch [] = {


0x00, 0x00, 0x00, 0x00, 0xF0, 0x00, 0x03, 0x08, 0x00, 0x06, 0x64, 0x00, 0x05,
0x92, 0x00, 0x09,
0x0A, 0x00, 0x0A, 0xEA, 0x00, 0x0A, 0x9A, 0x00, 0x08, 0x9A, 0x00, 0x04,
0x92, 0x00, 0x06, 0x94,
0x00, 0x02, 0x98, 0x00, 0x00, 0x90, 0x00, 0x00, 0x9C, 0x00, 0x00, 0x97, 0x80,
0x00, 0x96, 0xE0,
0x00, 0x96, 0xD0, 0x04, 0x96, 0x90, 0x0E, 0x80, 0x90, 0x09, 0x80, 0x10, 0x09,
0x80, 0x10, 0x09,
0x80, 0x10, 0x04, 0x80, 0x10, 0x04, 0x00, 0x10, 0x04, 0x00, 0x10, 0x02, 0x00,
0x10, 0x02, 0x00,
0x20, 0x03, 0x00, 0x20, 0x01, 0x80, 0x40, 0x00, 0xE1, 0x80, 0x00, 0x3E, 0x00,
0x00, 0x00, 0x00
};

void setup()
{
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C
addr 0x3D (for the 128x64)
pinMode(buttonPin, INPUT_PULLUP);

// 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
particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED

//Setup to sense up to 18 inches, max LED brightness


byte ledBrightness = 25; //Options: 0=Off to 255=50mA=0xFF hexadecimal.
100=0x64; 50=0x32 25=0x19
byte sampleAverage = 4; //Options: 1, 2, 4, 8, 16, 32
byte ledMode = 2; //Options: 1 = Red only, 2 = Red + IR, 3 = Red + IR +
Green
int sampleRate = 400; //Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
int pulseWidth = 411; //Options: 69, 118, 215, 411
int adcRange = 2048; //Options: 2048, 4096, 8192, 16384

particleSensor.setup(ledBrightness, sampleAverage, ledMode, sampleRate,


pulseWidth, adcRange); //Configure sensor with these settings

//Take an average of IR readings at power up


const byte avgAmount = 64;
long baseValue = 0;
for (byte x = 0 ; x < avgAmount ; x++)
{
baseValue += particleSensor.getIR(); //Read the IR value
}
baseValue /= avgAmount;

//Pre-populate the plotter so that the Y scale is close to IR values


for (int x = 0 ; x < 500 ; x++) {

}
}

void buttonChange()
{
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {


if (reading != buttonState) {
buttonState = reading;

if (buttonState == HIGH) {
ledState = !ledState; // HIGH to LOW
tone(3,1000);
delay(100);
noTone(3);
}
}
}
//.....
lastButtonState = reading;
}

void startDisplay()
{
display.clearDisplay();

display.drawBitmap(5, 5, litleHeart, 24, 21, WHITE);


display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(40,5); // x,y
display.println("Heart Rate");
display.setTextSize(2);
display.setCursor(40,15);
display.print(beatAvg);
display.setTextSize(1);
display.print(" BPM");

display.setTextSize(1);
display.setCursor(40,40);
display.println("Blood Oxygen");
display.setTextSize(2);
display.setCursor(40,50);
display.print(perCent);
display.setTextSize(1);
display.print(" %SpO2");

display.display();
}

void loop()
{
buttonChange();
long irValue = particleSensor.getIR();
if (checkForBeat(irValue) == true && ledState == HIGH)
{
display.clearDisplay();

display.drawBitmap(5, 5, fullHeart, 28, 28, 1);


display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(40,5); // x,y
display.println("Heart Rate");
display.setTextSize(2);
display.setCursor(40,15);
display.print(beatAvg);
display.setTextSize(1);
display.print(" BPM");

perCent = irValue / irOffset;


display.setTextSize(1);
display.setCursor(40,40);
display.println("Blood Oxygen");
display.setTextSize(2);
display.setCursor(40,50);
display.print(perCent);
display.setTextSize(1);
display.print(" %SpO2");

display.display();
tone(3,1000);
delay(100);
noTone(3);

//We sensed a beat!


long delta = millis() - lastBeat;
lastBeat = millis();

beatsPerMinute = 60 / (delta / 1000.0);

if (beatsPerMinute < 255 && beatsPerMinute > 20)


{
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 > 50000 && ledState == LOW)


{
startDisplay();
}

if (irValue < 50000 && ledState == LOW)


{
beatAvg=0;
perCent=0;
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(30,0);
display.println("Please Place ");
display.drawBitmap(50, 10, fingerTouch, 20, 30, 1);
display.setCursor(25,45);
display.println("For the Sensor");
display.setCursor(15,55);
display.println("Klik START Button");
display.display();
noTone(3);
}

else{
count = count+1;
avgIr = avgIr + irValue;
}
//Get an average IR value over 100 loops
if (count == 100) {
avgIr = avgIr / count;

//reset for the next 100


count = 0;
avgIr = 0;
}
}

You might also like