[SOLVED] Multiple Parameter Issue - Overloading??

Hi ,

I am working on a functions before implementing it in my code but cant pass multiple parameters to it. Here is what I have so far. I went through the forum and found that adding a pointer solved my first problem (although I am not too sure why). So this compiles..

char H[] = {"11001011"};

void setup() {
Serial.begin(9600);
disp(H);
delay(500);
disp(2);
}

void loop() {
}

void disp(byte a) {
  Serial.println(a);
}


void disp( char * a) { // * solved my problem
  Serial.println(a);
}

But what I really want to add is pass multiple parameters (char and byte) to the function (overloading?) like so but don't know enough to do this as this doesn't compile..

char H[] = {"11001011"};

void setup() {
Serial.begin(9600);

disp(H, 1, 2, 3);
}

void loop() {
}

void disp(byte a, byte b, byte c, byte d) {
 Serial.println(a);
 Serial.println(b);
 Serial.println(c);
 Serial.println(d);
}


void disp( char * a, char * b, char * c, char * d) {
Serial.println(a);
 Serial.println(b);
 Serial.println(c);
 Serial.println(d);
}

I guess I dont understand how pointers work or overloading functions if thats what I am doing.
Thanks for any help!

Thanks for the link and I will look into that!

What I want to do it accept H at any place e.g:

disp(1, H, 2, 3); or
disp(1, 2, H, 3); etc

So I looked into function overloading which I think the first example of my code is and just tried to expand it but then ran onto problems!

I am writing myself a library for the TM1637 (4x7segment display) just to teach myself and it's working fine for all the numbers from 0-9 which are stored in a private array

disp(4,6,3,7) // displays those numbers on each digit of the TM1637 fine

I now want to be able to display user defined patterns whereby H contains the user data

disp(H,6,3,7) // H display user defined pattern followed by numbers on the TM1637 fine

Here is where the issue lies with the code / method

The truth is that 'H' which you declared as a char array actually is a unsigned byte which you represented in a char array. (this is already a bit of an issue)
The simplest solution i thought of would be to simply pass 4 bytes to the function, and to use #define to define any non-custom values

#define N1 0x0000110
#define N0 etc..

like this you simply pass 4x uint8_t, but it may look like disp(0x10101010, N4, N2, N1);
either that or have a values higher than your pre-defined characters refer to the custom characters.disp(11,4,5,2);(where 11 refers to the 2nd custom character.)

Hi Deva,

Thanks very much for your time and explanations clearing up the signed and unsigned issue!

Your #define tip was so simple and yet did exactly what I wanted. Brilliant.

Thanks you!!!

Karma your way :slight_smile:

Oh great, yeah overloading just didn't seem to be the right option, generally used to create functions that perform the same action, but with different arguments eg like a color in 3 separate bytes and as a CRGB type or a uint32_t. Getting the compiler to differentiate between certain types can be tricky. For instance a 'byte' is aka 'unsigned char'.

I am writing myself a library for the TM1637 (4x7segment display) just to teach myself and it's working fine for all the numbers from 0-9 which are stored in a private array

disp(4,6,3,7) // displays those numbers on each digit of the TM1637 fine

disp(1, 2, H, 3); etc

Ok. Now I understand better what you want to achieve.

A 3 part answer:

a) if you would handle the numbers also as char, than you don't need 4 functions.

b) if these char array represents the segments to switch on / of :
char H[] = {"11001011"};

you don't need an array of 8 bytes at all. you can put your LEDs in ONE byte (instead of 8) if you handle each bit:

byte H = 0b11001011;

saves 7 bytes for each character!

c) have you ever considered to write a "real" library like lcd, or serial?
learn how to inherit from the print class and enable your code to print just use a simple

display.print("12H4");

or what ever.
you have to implement a write method to write one single character to a digit - and you get all the print methods (print chars, integers, floats, floats with decimals, BIN, HEX, ... EVERYTHING). More or less every "I want to print something" library inherits from print (or at least the better ones) - so you really should consider to do the same.

if I was able to convince you, you can have a look at some of my libraries
I think the HT16K33 lib for the 7 segment version will do 80% of what you need - I'm just using a different (I2C) hardware
https://werner.rothschopf.net/201909_arduino_ht16k33.htm

if you compare it to the Neopixel Display Library ... you still will find lot of similarities. And even the Max7219 or the LCD lib - all of them inherit from print.

good luck!

Thanks very much for your thoughts which have been very helpful as well!The link was a good intro into pointers.

I never considered about the memory aspects of a char "you don't need an array of 8 bytes at all. you can put your LEDs in ONE byte".

The main reason for trying to write my own was I needed something small for the ATTINY85. Some of the libraries out there took up too much space and others had issues. It was a good way of getting into writing libraries and also going through data sheets other peoples libraries and using my logic analyzer for the first time. The TM16307 was good to start with as its not overly complicated.

I will definitely have a look through your HT16K33 lib.

Thanks again for your time.

Karma your way :slight_smile: