1

Im currently working on a project which displays vital signs such as pulse and oxygen on my display. Now, my sensor is connected through I2C while my OLED runs through SPI. Im trying to display the sensor readings on the OLED but that doesnt seem to work. Tried reading through the threads and wasnt able to come up with a concise solution.

Anyone with a similar case and working solution will truly appreciate the support.

#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Arduino.h>
#include "algorithm.h"
#include "max30102.h"

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for SSD1306 display connected using 
// software SPI (default case):
#define OLED_MOSI   11
#define OLED_CLK   13
#define OLED_DC    9
#define OLED_CS    12
#define OLED_RESET 8
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
  OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors( &oneWire );

#define MAX_BRIGHTNESS 255

#if defined(ARDUINO_AVR_UNO)
// Arduino Uno doesn't have enough SRAM to store 
// 100 samples of IR led data and red led data in 32-bit format
// To solve this problem, 16-bit MSB of the sampled data 
// will be truncated. Samples become 16-bit data.
uint16_t aun_ir_buffer[100]; //infrared LED sensor data
uint16_t aun_red_buffer[100];  //red LED sensor data
#else
uint32_t aun_ir_buffer[100]; //infrared LED sensor data
uint32_t aun_red_buffer[100];  //red LED sensor data
#endif

// data length
int32_t n_ir_buffer_length;
// SPO2 value
int32_t n_spo2;
// indicator to show if the SPO2 calculation is valid
int8_t ch_spo2_valid;

// heart rate value
int32_t n_heart_rate;

// indicator to show if the heart rate calculation is valid
int8_t  ch_hr_valid;  
uint8_t uch_dummy;

void setup()
{
  //resets the MAX30102
  maxim_max30102_reset();

  // initialize serial communication at 115200 bits per second:
  Serial.begin(115200);

  // pin D10 connects to the interrupt output pin of the MAX30102
  pinMode(10, INPUT);
  delay(1000);

  // Reads/clears the interrupt status register
  maxim_max30102_read_reg(REG_INTR_STATUS_1,&uch_dummy);
  // wait until user presses a key 
  while( Serial.available() == 0 ) 
  {
    Serial.write(27);       // ESC command
    Serial.print(F("[2J")); // clear screen command
    Serial.println(F("Press any key to start conversion"));
    delay(1000);
  }

  uch_dummy=Serial.read();
  maxim_max30102_init();

  Serial.begin(115200); 
  sensors.begin();

  if(!display.begin(SSD1306_SWITCHCAPVCC)) 
  {
    Serial.println(F("SSD1306 allocation failed"));
  }

  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(30,30);
  display.print("ATRAL:D");
  display.display();
  delay(1000);
  display.clearDisplay();
}

void loop()
{ 
  // Call sensors.requestTemperatures() to issue 
  // a global temperature and Requests to all devices on the bus
  sensors.requestTemperatures();

  Serial.print("Celsius temperature: ");
  // Why "byIndex"? You can have more than 
  // one IC on the same bus. 0 refers to the first IC on the wire
  Serial.print(sensors.getTempCByIndex(0)); 
  Serial.print(" - Fahrenheit temperature: ");
  Serial.println(sensors.getTempFByIndex(0));
  delay(100);

  // variables to calculate the on-board LED 
  // brightness that reflects the heartbeats
  uint32_t un_min, un_max, un_prev_data, un_brightness;  
  int32_t i;
  float f_temp;

  un_brightness=0;
  un_min=0x3FFFF;
  un_max=0;

  // buffer length of 100 stores 4 seconds 
  // of samples running at 25sps
  n_ir_buffer_length=100;  

  // read the first 100 samples, and determine the signal range
  for( i=0; i < n_ir_buffer_length; i++ )
  {
    // wait until the interrupt pin asserts
    while( digitalRead(10) == 1 );    
    //read from MAX30102 FIFO
    maxim_max30102_read_fifo( ( aun_red_buffer + i ),
                              ( aun_ir_buffer + i ) );

    if(un_min>aun_red_buffer[i])
      un_min=aun_red_buffer[i];  //update signal min
    if(un_max<aun_red_buffer[i])
      un_max=aun_red_buffer[i];  //update signal max
    Serial.print(F("red="));
    Serial.print(aun_red_buffer[i], DEC);
    Serial.print(F(", ir="));
    Serial.println(aun_ir_buffer[i], DEC);
  }
  un_prev_data=aun_red_buffer[i];

  // calculate heart rate and SpO2 after first 100 samples 
  // (first 4 seconds of samples)
  maxim_heart_rate_and_oxygen_saturation( aun_ir_buffer,
         n_ir_buffer_length, aun_red_buffer, 
         &n_spo2, &ch_spo2_valid, &n_heart_rate, 
         &ch_hr_valid ); 

  // Continuously taking samples from MAX30102.  
  // Heart rate and SpO2 are calculated every 1 second
  while(1)
  {
    i=0;
    un_min=0x3FFFF;
    un_max=0;

    // dumping the first 25 sets of samples 
    // in the memory and shift the last 75 sets 
    // of samples to the top
    for( i=25; i<100; i++ )
    {
      aun_red_buffer[i-25]=aun_red_buffer[i];
      aun_ir_buffer[i-25]=aun_ir_buffer[i];

      //update the signal min and max
      if(un_min>aun_red_buffer[i])
        un_min=aun_red_buffer[i];
      if(un_max<aun_red_buffer[i])
        un_max=aun_red_buffer[i];
    }

    //take 25 sets of samples before calculating the heart rate.
    for( i=75; i<100; i++ )
    {
      un_prev_data=aun_red_buffer[i-1];
      while(digitalRead(10)==1);
      digitalWrite(9, !digitalRead(9));
      maxim_max30102_read_fifo((aun_red_buffer+i), (aun_ir_buffer+i));

      //calculate the brightness of the LED
      if(aun_red_buffer[i]>un_prev_data)
      {
        f_temp=aun_red_buffer[i]-un_prev_data;
        f_temp/=(un_max-un_min);
        f_temp*=MAX_BRIGHTNESS;
        f_temp=un_brightness-f_temp;
        if(f_temp<0)
          un_brightness=0;
        else
          un_brightness=(int)f_temp;
      }
      else
      {
        f_temp=un_prev_data-aun_red_buffer[i];
        f_temp/=(un_max-un_min);
        f_temp*=MAX_BRIGHTNESS;
        un_brightness+=(int)f_temp;
        if(un_brightness>MAX_BRIGHTNESS)
          un_brightness=MAX_BRIGHTNESS;
      }
#if defined(ARDUINO_AVR_LILYPAD_USB)  
      analogWrite(13, un_brightness);
#endif

#if defined(ARDUINO_AVR_FLORA8)
      LED.setPixelColor(0, un_brightness/BRIGHTNESS_DIVISOR, 0, 0);
      LED.show();
#endif

      //send samples and calculation result to terminal program through UART
      Serial.print(F("red="));
      Serial.print(aun_red_buffer[i], DEC);
      Serial.print(F(", ir="));
      Serial.print(aun_ir_buffer[i], DEC);

      Serial.print(F(", HR="));
      Serial.print(n_heart_rate, DEC);

      Serial.print(F(", HRvalid="));
      Serial.print(ch_hr_valid, DEC);

      Serial.print(F(", SPO2="));
      Serial.print(n_spo2, DEC);

      Serial.print(F(", SPO2Valid="));
      Serial.println(ch_spo2_valid, DEC);
    }
    maxim_heart_rate_and_oxygen_saturation(aun_ir_buffer, n_ir_buffer_length, aun_red_buffer, &n_spo2, &ch_spo2_valid, &n_heart_rate, &ch_hr_valid); 
  }

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.print("Temp: ");
  display.print(sensors.getTempCByIndex(0));
  display.print("C");
  display.setCursor(0,10);
  display.print("Temp: ");
  display.print(sensors.getTempFByIndex(0));
  display.print("F");  
  display.setCursor(0,20);
  display.print("HR: ");
  display.print(n_heart_rate, DEC);
  display.setCursor(0,30);
  display.print("02: ");
  display.print(n_spo2, DEC);
  display.setCursor(0,50);
  display.setTextSize(2);
  display.print("BOX.E");
  display.display(); 
  display.clearDisplay();


}                                    
5
  • Sure thing, Ill share two separate sketches, the sensor sketch that I want to combine with the Oled sketch that has a temperature sensor in it too – Shaukatali Z Hussein Feb 27 '20 at 18:27
  • @VE7JRO I Hope the codes will help – Shaukatali Z Hussein Feb 27 '20 at 18:35
  • 1
    "Doesn't seem to work" is not a good description of the problem. We cannot know, what that means. You need to tell us, what you expected with each code and what exactly is actually happening. Also: You have included 2 seperate sketches, but your question is more why the combination does not work. For answering that, we need to see your combined code. – chrisl Feb 27 '20 at 19:32
  • @chrisl Thank you for highlighting and clarifying, Ill do what you've suggested and get back – Shaukatali Z Hussein Feb 27 '20 at 20:44
  • @chrisl the code has been modified and thank you all for editing it.. kinda new to the whole community and formatting – Shaukatali Z Hussein Feb 28 '20 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.