im making a pulse oximeter, thermometer and clock 3 in 1. I'm using an arduino uno r3. Here is my code.
#include <Wire.h>
#include <RTClib.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>
#include "MAX30100_PulseOximeter.h"
#define REPORTING_PERIOD_MS 1000
RTC_DS3231 rtc;
Adafruit_SH1106 display(-1);
int state = 1;
const int lm35_pin = A1;
PulseOximeter pox;
uint32_t tsLastReport = 0;
void onBeatDetected() {
Serial.println("Beat!");
}
void drawUI() {
display.clearDisplay();
display.setTextSize(1);
display.setCursor(2, 2);
display.println("Digital Clock ON");
display.drawLine(0, 11, 128, 11, WHITE);
display.drawLine(94, 0, 94, 10, WHITE);
display.drawLine(26, 44, 102, 44, WHITE);
}
void setup() {
pinMode(5, INPUT_PULLUP);
display.begin(SH1106_SWITCHCAPVCC, 0x3C);
display.setTextColor(WHITE);
display.display();
if (!rtc.begin()) {
Serial.println("No RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, set the time!");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
Serial.begin(9600);
Serial.print("Initializing pulse oximeter..");
if (!pox.begin()) {
Serial.println("FAILED");
for (;;);
} else {
Serial.println("SUCCESS");
}
pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
pox.setOnBeatDetectedCallback(onBeatDetected);
}
void loop() {
pox.update();
if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
Serial.print(F("Heart rate:"));
Serial.print(pox.getHeartRate());
Serial.print(F("bpm / SpO2:"));
Serial.print(pox.getSpO2());
Serial.println(F("%"));
tsLastReport = millis();
}
int temp_adc_val;
float temp_val;
temp_adc_val = analogRead(lm35_pin);
temp_val = (temp_adc_val * 4.88);
temp_val = (temp_val / 10);
Serial.print(F("Temperature = "));
Serial.print(temp_val);
Serial.print(F(" Degree Celsius\n"));
if (digitalRead(5) == LOW) {
if (state == 0) {
delay(500);
drawUI();
state = 1;
} else {
delay(500);
display.drawRect(4, 7, 120, 50, WHITE);
display.fillRect(5, 8, 118, 48, BLACK);
display.setTextSize(1);
display.setCursor(6, 12);
display.println("Clock turn");
display.println(" off in a moment.");
display.println("");
display.println(" To turn back on,");
display.println(" press button.");
display.display();
delay(3000);
display.clearDisplay();
display.display();
state = 0;
}
}
if (state == 1) {
DateTime now = rtc.now();
drawUI();
display.setTextColor(WHITE);
display.setTextSize(3);
display.setCursor(1, 19);
if (now.hour() < 10) {
display.print("0");
}
display.print(now.hour());
display.print(":");
if (now.minute() < 10) {
display.print("0");
}
display.print(now.minute());
display.setTextSize(2);
display.print(":");
if (now.second() < 10) {
display.print("0");
}
display.print(now.second());
display.setTextSize(1);
display.setCursor(26, 48);
switch (now.dayOfTheWeek()) {
case 1: display.print("Monday,"); break;
case 2: display.print("Tuesday,"); break;
case 3: display.print("Wednesday,"); break;
case 4: display.print("Thursday,"); break;
case 5: display.print("Friday,"); break;
case 6: display.print("Saturday,"); break;
case 7: display.print("Sunday,"); break;
}
display.setCursor(26, 56);
if (now.day() < 10) {
display.print("0");
}
display.print(now.day());
display.print(".");
if (now.month() < 10) {
display.print("0");
}
display.print(now.month());
display.print(".");
display.print(now.year());
display.display();
delay(999);
}
}
instead of showing the coded clock, it just show this: