Simplifying (function) code [SOLVED]

As i’m relatively new to the arduino and being my first post - guidance is sought after. This is my working (function) code for a led cube with RTC.

char fontdata [11][8] ={

{0x00,  0x3C,  0x66,  0x6E,  0x76,  0x66,  0x66,  0x3C },  //  0
... // (snippet)
{ 0x00,  0x3C,  0x66,  0x66,  0x3E,  0x06,  0x66,  0x3C }  // 9

};

void effect_digital(){

DateTime now = RTC.now(); // get time from ds1307 hh:mm

int ab;   
ab = (now.hour()); // digits of hour 
int a = ab/10; //  1st digit of hh
int b =ab%10; //   2nd digit of hh

int cd; 
cd = (now.minute()); // digits of minutes
int c = cd/10; // 1st digit of mm
int d =cd%10; // 2nd digit of mm

const int charNum = 1; 
char string[charNum] = {a}; // which digit to display

{
for(int dist = 0; dist < 8; dist++) { // bring digit forward
     
int rev = 0;
      
for (int rw = 7; rw >= 0; rw--) { // copy rows
cube[rev][dist] =(fontdata[string[0]][rw]); // digit stored in fontdata 
rev++;
}

basically i’ve copied the function four times, renaming the effect_ and inserting desired digit required (hh:mm as ab:cd) as the charNum. Then from the main loop - selecting which function effect_ i require . what i would like to achieve is, rather than replicating - use it once and be able to insert the charNum (a,b,c,d) from the main loop.

Any guidance appreciated.

That code cannot possibly compile. Please post your code. Try using the IDE's auto format tool to sort out the indentation before posting.

Code posted below with non relevant parts omitted eg delay etc- (auto formatted - still learning correct layout) What i need guidance with, rather than reiterating the effect_ function by renaming it. how do go about calling either a,b,c,d from the main loop with just one effect_ function.

#include <TimerOne.h>
#include <string.h>
#include <Wire.h>
#include "RTClib.h"
#define DS1307_I2C_ADDRESS 0x68

RTC_DS1307 RTC;

void setup() {

  Wire.begin();
  RTC.begin();
}


void loop(){
  int i,x,y,z;
  byte cube[8][8];
  while (true)
  {

    ///eg effect_digit(a);

    effect_digitalh1();
    effect_digitalh2();
    effect_digitalm3();
    effect_digitalm4();
  }
}

char fontdata [11][8] ={

  {
    0x00,  0x3C,  0x66,  0x6E,  0x76,  0x66,  0x66,  0x3C                       }
  ,    //  0

  { 
    0x00,  0x18,  0x18,  0x38,  0x18,  0x18,  0x18,  0x7E                       }
  ,    // 1

  { 
    0x00,  0x3C,  0x66,  0x06,  0x0C,  0x30,  0x60,  0x7E                       }
  ,    // 2		

  { 
    0x00,  0x3C,  0x66,  0x06,  0x1C,  0x06,  0x66,  0x3C                       }
  ,    // 3

  { 
    0x00,  0x0C,  0x1C,  0x2C,  0x4C,  0x7E,  0x0C,  0x0C                       }
  ,    // 4

  { 
    0x00,  0x7E,  0x60,  0x7C,  0x06,  0x06,  0x66,  0x3C                       }
  ,    //  5

  { 
    0x00,  0x3C,  0x66,  0x60,  0x7C,  0x66,  0x66,  0x3C                       }
  ,    // 6

  { 
    0x00,  0x7E,  0x66,  0x0C,  0x0C,  0x18,  0x18,  0x18                       }
  ,    //  7

  { 
    0x00,  0x3C,  0x66,  0x66,  0x3C,  0x66,  0x66,  0x3C                       }
  ,    // 8

  { 
    0x00,  0x3C,  0x66,  0x66,  0x3E,  0x06,  0x66,  0x3C                       }   
  // 9
};

void effect_digitalh1(){
  byte cube[8][8];

  DateTime now = RTC.now();

  int ab;
  ab = (now.hour()); 
  int a = ab/10;
  int b =ab%10;

  const int charNum = 1;
  char string[charNum] = {
    a                      };
  {
    for(int dist = 0; dist < 8; dist++) { 

      int rev = 0;

      for (int rw = 7; rw >= 0; rw--) {
        cube[rev][dist] =(fontdata[string[0]][rw]);
        rev++;
      }
    }
  }
}

void effect_digitalh2(){
  byte cube[8][8];

  DateTime now = RTC.now();


  int ab;
  ab = (now.hour()); 
  int a = ab/10;
  int b =ab%10;


  const int charNum = 1;
  char string[charNum] = {
    b                      };

  {
    for(int dist = 0; dist < 8; dist++) { 

      int rev = 0;
      for (int rw = 7; rw >= 0; rw--) {
        cube[rev][dist] =(fontdata[string[0]][rw]);
        rev++;
      }
    }
  }
}
void effect_digitalm3(){
  byte cube[8][8];

  DateTime now = RTC.now();

  int cd;
  cd = (now.minute()); 
  int c = cd/10;
  int d =cd%10;

  const int charNum = 1;
  char string[charNum] = {
    c                      };
  {
    for(int dist = 0; dist < 8; dist++) { 

      int rev = 0;
      for (int rw = 7; rw >= 0; rw--) {
        cube[rev][dist] =(fontdata[string[0]][rw]);
        rev++;
      }
    }
  }
}

void effect_digitalm4(){
  byte cube[8][8];

  DateTime now = RTC.now();

  int cd;
  cd = (now.minute()); 
  int c = cd/10;
  int d =cd%10;

  const int charNum = 1;
  char string[charNum] = {
    d                      };
  {
    for(int dist = 0; dist < 8; dist++) { 

      int rev = 0;
      for (int rw = 7; rw >= 0; rw--) {
        cube[rev][dist] =(fontdata[string[0]][rw]);
        rev++;

      }
    }
  }
}

Well being that this is the only part that seems to change,

char string[charNum] = {
a };

You can replace it with a char variable and when you call the function, what you put in there will become that variable.

pseudo code of coarse.

void effect_digital(char holder){
.
.
.
char string[charNum] = { holder };
}

Something like this:
void loop() {

effect_digital( ‘a’ );
}

Have just tried solution of using char holder. No problem compiling but the outputs of a,b,c,d as displayed are 0,0,0,0 respectively . Any thoughts? RTC is working fine.

const int charNum = 1; char string[charNum] = {

fontdata[string[0]][rw]);

What happens if you change this "string[0]" to 1 ?

If all else fails, debug.

const int charNum = 1; char string[charNum] = {

refers to the number of digits to be displayed

void loop(){
  int i,x,y,z;
  while (true)
  {
   
   effect_digital(a);
   effect_digital('b');

...

above snippet of code - if a is without single quotes, (compile error 'a' not declared in scope) before when i mentioned outputs of a,b,c,d where 0,0,0,0

void loop(){
  int i,x,y,z;
  int a,b,c,d;
  while (true)
  {
   
   effect_digital(a);
   effect_digital(b);

...

should i use char instead of int?? because they only become values once the RTC is read..

What are you tring to do? Show the letter/number on the cube? If so, you may need to you case statements or some IF/ELSE statements to convert the letters to numbers. My guess is that you want to enter an int or char, and the cube will display it.

You may be right. Just to clarifly my objective - have connected Real Time Clock to cube it outputs hh:mm. As i can only display one digit at a time - my code splits the four digits into a,b,c,d respectively. then the correspending number from fontdata displayed. Rather than having four effect_ functions for each (a,b,c,d) i was wanting just one - where a,b,c,d can be called from main loop. You mention if/else statements - just reading up on now aswel as char holder (new to arduino) could please explain in relation. thank you

Oh! My appologies. OK easy.

see how your storing the digits into a,b,c and d, replace those with string[0] - string [3], then using another for loop, you increment the string to change the number.

// NOTE: string[] may/will need to be global 

int ab;   
ab = (now.hour()); // digits of hour 
string[0] = ab/10; //  1st digit of hh
string[1] =ab%10; //   2nd digit of hh

int cd; 
cd = (now.minute()); // digits of minutes
string[2] = cd/10; // 1st digit of mm
string[3] =cd%10; // 2nd digit of mm

And this

for(int count = 0; count <= 3; count++) 
{
   for(int dist = 0; dist < 8; dist++) { 

      int rev = 0;

      for (int rw = 7; rw >= 0; rw--) {
        cube[rev][dist] =(fontdata[string[count]][rw]);
        rev++;
      }
    }
// some delay may be needed
}// close "count" for loop

Works a treat - thank-you HazardsMind… just put it down for five to say so! who clicks solved?

Everything works now? Great.