Is it possible to call a subrutine using a class?

Hello! I’m new to arduino and to c programming in general. I have tried googling my problem, but “c” and “class” yields a lot of irrelevant links.

Heres the problem:

I have two LCD displays which are defined like this

LCD3Wire lcd1 = LCD3Wire(LCD_LINES, DOUT_PIN, STR_PIN, CLK_PIN); 
LCD3Wire lcd2 = LCD3Wire(LCD2_LINES, DOUT_PIN, STR2_PIN, CLK_PIN);

I would really like to call a subroutine using lcd1 and lcd2 as variables, thereby avoiding having to reference them both.

For reference, here is the subroutine:

void digitalClockDisplay(){
  lcd1.cursorTo(2, 10);
  lcd2.cursorTo(2, 26);
  lcd1.printIn(dag[weekday()-1]);
  lcd2.printIn(dag[weekday()-1]);
  lcd1.print(' ');
  lcd2.print(' ');
  //if(day() <10)
  //lcd1.print('0');
  lcdPrintDec (day() , 2);
  lcd2PrintDec (day() , 2);
 // if(hour() <10)
 // lcd1.cursorTo(2,3);
  lcd1.cursorTo(2,4);
  lcd2.cursorTo(2,20);
// clock display of current time
lcdPrintDec (hour(),2); 
lcd2PrintDec (hour(),2); 
printDigits(minute());
//printDigits(second());
}

This is a snippet from the main loop calling the subroutine:

 if( now() != prevDisplay) //update the display only if the time has changed
      {
      prevDisplay = now();
      digitalClockDisplay();  
      sendPackets();
      }

Regards,
Lasse

The thing you're looking for is a pointer. Look for class pointers, that should yield better results.

Korman

Or references. You have instances of the class. You can pass those instance to a function, by reference (using the & operator).

Thank you! I should really buy a book....

With references it should like the code snippet below

void loop()
{
...
digitalClockDisplay(lcd1);   
digitalClockDisplay(lcd2);
...
}

void digitalClockDisplay(LCD3Wire & LCD ){
  LCD.cursorTo(2, 10);
  LCD.printIn(dag[weekday()-1]);
  LCD.print(' ');
  LCD.PrintDec (day() , 2);

  LCD.cursorTo(2,4);
  LCD.PrintDec (hour(),2);
  // etc
}

I noted that in your code you do not use the same XY coordinates for both displays when displaying a string. So besides a pointer to the display object you should one way or the other also pass the right coordinates. One way to do this is to pass them explicitly (additional parameters) the other way is to pass them implicit (they are part of the LCD object) both have pro and cons.

Thank you. I have already done it. The finished code is much cleaner and I have eliminated two additional functions (lcdPrintDec, andPrintDigits):

 if( now() != prevDisplay) //update the display only if the time has changed
      {
      prevDisplay = now();
      digitalClockDisplay(lcd1, 10);  
      digitalClockDisplay(lcd2, 26);  
      sendPackets();
      }
void digitalClockDisplay( LCD3Wire &lcd, int pos ){
  lcd.cursorTo(2, pos);
  lcd.printIn(dag[weekday()-1]);
  lcd.print(' ');
  lcd.printIn(itoa(day(),decBuffer, 10));
  lcd.cursorTo(2,(pos-6));
  // clock display of current time
   if (hour() < 10)
  lcd.print('0');
  lcd.printIn(itoa(hour(),decBuffer, 10));
  lcd.print(':');
  if (minute() < 10)
  lcd.print('0');
  lcd.printIn(itoa(minute(),decBuffer, 10));
 // printDigits(lcd, minute());

}