help w/ code

how can i modify this code to use in my sh1106 spi oled display ?

note: i have almost no knowledge on C and programming in general

code =
#include <Adafruit_SSD1306.h>

#define OLED_Address 0x3C
Adafruit_SSD1306 oled(1);

int x=0;
int lastx=0;
int lasty=0;
int LastTime=0;
bool BPMTiming=false;
bool BeatComplete=false;
int BPM=0;

#define UpperThreshold 550
#define LowerThreshold 500

void setup() {
oled.begin(SSD1306_SWITCHCAPVCC, OLED_Address);
oled.clearDisplay();
oled.setTextSize(2);
}

void loop()
{
if(x>127)
{
oled.clearDisplay();
x=0;
lastx=x;
}

int value=analogRead(0);
oled.setTextColor(WHITE);
int y=60-(value/16);
oled.writeLine(lastx,lasty,x,y,WHITE);
lasty=y;
lastx=x;
// calc bpm

if(value>UpperThreshold)
{
if(BeatComplete)
{
BPM=millis()-LastTime;
BPM=int(60/(float(BPM)/1000));
BPMTiming=false;
BeatComplete=false;
}
if(BPMTiming==false)
{
LastTime=millis();
BPMTiming=true;
}
}
if((value<LowerThreshold)&(BPMTiming))
BeatComplete=true;
// display bpm
oled.writeFillRect(0,50,128,16,BLACK);
oled.setCursor(0,50);
oled.print(BPM);
oled.print(" BPM");
oled.display();
x++;
}

nanoR4K:
how can i modify this code to use in my sh1106 spi oled display ?

note: i have almost no knowledge on C and programming in general

Well, look for working examples for your display and then find out, what and how it does things.

If you want somebody to do it for you, without learning yourself, you have to pay that somebody.

Strange breed, these open license folks... they are eager to help someone who has bothered to do some research and experimentation, and tried to learn on their own. they freely exchange information and experiences on how to use the hardware and software, but want to get paid for doing somebody's work for them....

The rule here is 'show that you have tried to learn by doing, then ask for help (show your work on the back of the exam)'.

Probably not what you wanted to hear, but sometimes, life is a bear.

couka:
Well, look for working examples for your display and then find out, what and how it does things.

If you want somebody to do it for you, without learning yourself, you have to pay that somebody.

to be honest i don´t even know where i should start learning, its just to much unknown words to just do a "how this display works" yt search .

Hi,
Try this link;

It covers the I2C and SPI types.

Tom..... :slight_smile:

For starts you read the Read Before Posting thread and see to put code tags around your code so it's readable in html.

If you are going to time with 16 bit values then use word instead of int. You can time up to 65.535 seconds with type word (Arduino unsigned 16 bit) counting the low 16 bits of millis. Note that occasionally you will be 1 ms off using millis(), when it matters use micros().

#include <Adafruit_SSD1306.h>
 
#define OLED_Address 0x3C
Adafruit_SSD1306 oled(1);

int x=0;
int lastx=0;
int lasty=0;
int LastTime=0;
bool BPMTiming=false;
bool BeatComplete=false;
int BPM=0;

#define UpperThreshold 550
#define LowerThreshold 500

void setup() {
  oled.begin(SSD1306_SWITCHCAPVCC, OLED_Address);
  oled.clearDisplay();
  oled.setTextSize(2);
}


void loop() 
{
  if(x>127)  
  {
    oled.clearDisplay();
    x=0;
    lastx=x;     
  }

  int value=analogRead(0);
  oled.setTextColor(WHITE);
  int y=60-(value/16);
  oled.writeLine(lastx,lasty,x,y,WHITE);
  lasty=y;
  lastx=x;
  // calc bpm

  if(value>UpperThreshold)
  {
    if(BeatComplete)
    {
      BPM=millis()-LastTime;
      BPM=int(60/(float(BPM)/1000));
      BPMTiming=false;
      BeatComplete=false;
    }
    if(BPMTiming==false)
    {
      LastTime=millis();
      BPMTiming=true;
    }
  }
  if((value<LowerThreshold)&(BPMTiming))
    BeatComplete=true;
    // display bpm
  oled.writeFillRect(0,50,128,16,BLACK);
  oled.setCursor(0,50);
  oled.print(BPM);
  oled.print(" BPM");
  oled.display();
  x++;
}

About these parts:

int x=0;
int lastx=0;
int lasty=0;
int LastTime=0;
bool BPMTiming=false;
bool BeatComplete=false;
int BPM=0;

  ......

    if(BeatComplete)
    {
      BPM=millis()-LastTime;  
      BPM=int(60/(float(BPM)/1000));
      BPMTiming=false;
      BeatComplete=false;
    }
    if(BPMTiming==false)
    {
      LastTime=millis();
      BPMTiming=true;
    }

is better written as

int x=0;
int lastx=0;
int lasty=0;  // howcome there's a lasty but no y?

bool BPMTiming;
bool BeatComplete=false;
unsigned long LastTime;  // declared variables default to zero
unsigned long BPM;  // is unsigned long to be same type math with timer variables and millis() UL return.

  ......

    if(BeatComplete)
    {
      BPM=millis()-LastTime;  // now the ms time since last beat, it was -- not always going to work like you thought.
      BPM=60000/BPM;  // y=a/(b/c)=a*c/b, algebra. [b]with integers do your multiplies before divides to get less roundoff[/b]
      BPMTiming=false;
      BeatComplete=false;
    }
    if(BPMTiming==false)  //  this will always run if (BeatComplete)
    {
      LastTime=millis();  
      BPMTiming=true;  // this switches BPMTiming as set in if (BeatComplete) from false to true
    }

Code is a DIY logic puzzle. If it works without glitches then you solved it. Till then use logic to craft and debug
your code.