본문 바로가기

Arduino

Pulse Sensor LED

Pulse sensor로 심박수 체크해 보는 셋업입니다. 심박수 센서가 원형 모양으로 되어 있는거 말고 MAX30102 센서로 하는게 정확할거 같습니다.

LED는  아래와 같이 작동합니다.

99 < BPM – RED

0 < BPM < 60 – BLUE

59 < BPM < 100 – WHITE


#define USE_ARDUINO_INTERRUPTS true
#include 
 
//  Variables
const int PIN_INPUT = A0;     // Pulse Sensor PURPLE WIRE connected to ANALOG PIN 0
const int THRESHOLD = 550;   // Adjust this number to avoid noise when idle
const int PIN_BLINK = 4;     // Status LED
 
// Time measure variables
uint32_t measureTime = 15000; // 15 seconds to measure pulses
uint32_t startMeasureTime = 0;
boolean pulseReading = false;
 
//RGB pin
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
 
//  button
const int btn = 7;
 
PulseSensorPlayground pulseSensor;
 
//uncomment this line if using a Common Anode LED
#define COMMON_ANODE
 
void setup()
{
  Serial.begin(115200);         // Set's up Serial Communication at certain speed.
 
  pulseSensor.analogInput(PIN_INPUT);
  pulseSensor.setThreshold(THRESHOLD);
  pulseSensor.blinkOnPulse(PIN_BLINK);
 
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  setColor(0, 0, 0);
 
  pinMode(btn, INPUT);
 
  if (!pulseSensor.begin()) {
    for (;;) {
      // Flash the led to show things didn't work.
      digitalWrite(PIN_BLINK, LOW);
      delay(50);
      digitalWrite(PIN_BLINK, HIGH);
      delay(50);
    }
    pinMode(btn, INPUT);
  }
}
 
void loop() {
 
  delay(20);
 
  if (pulseSensor.sawNewSample() && pulseReading) {
    int BPM = pulseSensor.getBeatsPerMinute();
    Serial.println(BPM);
 
    if (99 < BPM) {
      setColor(255, 0, 0);  // red
    } else if (0 < BPM < 60) {
      setColor(0, 0, 255);  // blue
    } else if (59 < BPM < 100) {
      setColor(255, 255, 255);  // white
    }
 
    if (millis() - startMeasureTime > measureTime) {
      pulseReading = false;
      Serial.print("Pulse reading is finished.");
      setColor(0, 0, 0);
    }
 
  }
 
  if (!pulseReading && digitalRead(btn)) {
    pulseReading = true;
    startMeasureTime = millis();
  }
 
}
 
void setColor(int red, int green, int blue)
{
#ifdef COMMON_ANODE
  red = 255 - red;
  green = 255 - green;
  blue = 255 - blue;
#endif
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
}

'Arduino' 카테고리의 다른 글

Arduino Clock – RTC  (0) 2018.12.26
Nulsom Arduino Nano  (0) 2018.12.26
TV 리모컨으로 온도 표시 - Dot Matrix  (0) 2018.12.26
Hardware Debounce 하드웨어 디바운스  (0) 2018.12.26
Pull-Up, Pull-Down에 대해서 알아보자  (0) 2018.12.18