HELP IN WRITING LIBRARY for D

Hello guys.
have a nice day.

my name is Thanh.
please help me with this issue.
currently i'm trying to create a library (actually this is my first lib :slight_smile: ) for the DS3231 timer.
while im uploading code i got this issue:

C:\Program Files (x86)\Arduino\libraries\DS3231\DS3231.cpp:22:6: error: prototype for 'byte DS3231Timer::convertBCDtoDEC(byte)' does not match any in class 'DS3231Timer'

 byte DS3231Timer::convertBCDtoDEC(byte x){

      ^

In file included from C:\Program Files (x86)\Arduino\libraries\DS3231\DS3231.cpp:1:0:

C:\Program Files (x86)\Arduino\libraries\DS3231\DS3231.h:27:6: error: candidate is: int DS3231Timer::convertBCDtoDEC(byte)

  int convertBCDtoDEC(byte x) {return b;}

      ^

exit status 1
Error compiling for board Arduino Pro or Pro Mini.

seem that i the problem comes from this function "byte DS3231Timer::convertBCDtoDEC(byte x)"
what i wanna do is send number in BIN and this function with return the DEC number.

additonal info: i used arduino nano + FTDI

here is header file .h

#include "Arduino.h"
#include "pins_arduino.h"
#include "Wire.h"
//DS3231 address
#define DS3231 0x68
#define secs 0x00
#define mins 0x01
#define hrs 0x02
#define months 0x03
#define years 0x04

class DS3231Timer{
	public:
	int getSecs() ;	//this function is used to get second data
	void getMins(); //this function is used to get minute data
	void getHrs (uint8_t types); // this function is used to get Hour data
	void getPeriod(); // this function is used to get period of day. AM or PM if set it 12h mode.
	
	
	
	int convertBCDtoDEC(byte x) {return b;}
	//byte convertDECtoBCD(int x)	{return dectobcd;}
	
	protected:
	int k;
	byte b;

};

and .ccp file

#include "DS3231.h"
#include "Arduino.h"
#include "pins_arduino.h"
#include "Wire.h"


int DS3231Timer::getSecs(){
	Wire.beginTransmission(DS3231);
	Wire.write(secs);
	Wire.endTransmission();
	int k = convertBCDtoDEC(Wire.read());
	return k;
}

int DS3231::convertDECtoBCD(byte x){
	int result;
	int MBS = x >> 4;
	int LBS = x & 0b1111;
	return result = (MBS*10)+LBS;
}

byte DS3231Timer::convertBCDtoDEC(byte x){
  int result;
  int MBS = x >> 4;
  int LBS = x & 0b1111;
  return result = (MBS*10)+LBS;
}

and here is the code that i write in arduino IDE in order to test simple function.

#include "DS3231.h"

DS3231Timer timer;

void setup(){
  Serial.begin(9600);
}
void loop(){
  
  Serial.println(timer.getSecs());
  
}

full picture when i got this error:

the question is how do i fix it?
i thinks it's too simple for person writes the library very day :slight_smile: :slight_smile:
but to me it's huge problem :o :o :o

Thank you so much for caring about my topic
anyway, my email is :nhatthanh228@gmail.com
if there is any infor please update here

"byte" is not a standard data type in C/C++.

Hi Thanh

In your header file, you have this ...

int convertBCDtoDEC(byte x) {return b;}
	//byte convertDECtoBCD(int x)	{return dectobcd;}

Delete the implementation parts to leave this ...

int convertBCDtoDEC(byte x);
byte convertDECtoBCD(int x);

Then, you have a mismatch with the .cpp file. Compare the arguments and return types in your header file with what you have here:

int DS3231::convertDECtoBCD(byte x){
	int result;
	int MBS = x >> 4;
	int LBS = x & 0b1111;
	return result = (MBS*10)+LBS;
}

byte DS3231Timer::convertBCDtoDEC(byte x){
  int result;
  int MBS = x >> 4;
  int LBS = x & 0b1111;
  return result = (MBS*10)+LBS;
}

Once you fix these, the code should compile. But you have the same code in both methods - presumably they should be different?

Regards

Ray

Couple of further thoughts ...

It looks like you are trying to convert (both ways) between a two digit packed BCD value and its numeric equivalent (e.g. 0x42 represents the decimal value 42). The range of values you can store in one byte packed BCD is from 0 to 99. This will also fit in one byte, so there is no need to use int as the type. Also int is signed, whereas you are (I assume) handling only positive values?

You said you were using a Nano? The error message says Pro, so make sure you select the correct board type in the IDE when compiling.

In file included from C:\Program Files (x86)\Arduino\libraries\DS3231\DS3231.cpp:1:0:

C:\Program Files (x86)\Arduino\libraries\DS3231\DS3231.h:27:6: error: candidate is: int DS3231Timer::convertBCDtoDEC(byte)

  int convertBCDtoDEC(byte x) {return b;}

      ^

exit status 1
Error compiling for board Arduino Pro or Pro Mini.