Using an external function from within a class...

Hi all,

My google-fu is failing me today. I'm trying to execute a function that is declared outside of a class from within a class. Here is a simple example of my code:

#include <LiquidCrystal.h>


class Flasher
{
  // Class Member Variables
  // These are initialized at startup
  int ledPin;      // the number of the LED pin
  long OnTime;     // milliseconds of on-time
  long OffTime;    // milliseconds of off-time

  // These maintain the current state
  int ledState;                 // ledState used to set the LED
  unsigned long previousMillis;   // will store last time LED was updated

  // Constructor - creates a Flasher 
  // and initializes the member variables and state
  public:
  Flasher(int pin, long on, long off)
  {
  ledPin = pin;
  pinMode(ledPin, OUTPUT);     
    
  OnTime = on;
  OffTime = off;
  
  ledState = LOW; 
  previousMillis = 0;
  }

  void Update()
  {
    // check to see if it's time to change the state of the LED
    unsigned long currentMillis = millis();
     
    if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
    {
      ledState = LOW;  // Turn it off
      previousMillis = currentMillis;  // Remember the time
      digitalWrite(ledPin, ledState);  // Update the actual LED
    }
    else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
    {
      ledState = HIGH;  // turn it on
      previousMillis = currentMillis;   // Remember the time
      digitalWrite(ledPin, ledState);   // Update the actual LED
      lcd_out(ledState);    // errors right here
    }
  }
};

// LCD display setup
//                BS E  D4 D5  D6  D7
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);   // digital pins

Flasher led1(12, 100, 400);
Flasher led2(13, 350, 350);

void setup()
{
  lcd.begin(16, 2);
  lcd.clear();
}

void loop()
{
  led1.Update();
  led2.Update();

  
}

void lcd_out(int state)
{
  lcd.setCursor(0,0);
  lcd.print(state);
}

So I've got a 16x2 lcd character display and a function that outputs data to the display. How do I access that lcd_out function from within the class?

Line 47 causes an error - 'lcd_out' was not declared in this scope

How do I gain access to the lcd_out function from within the class Flasher?

Thanks for any help,
Randy

Is all the code in a single file?

Add a function prototype:

#include <LiquidCrystal.h>
void lcd_out(int state);

Thanks for the replies!

PaulS:
Is all the code in a single file?

Yes.

gfvalvo:
Add a function prototype:

#include <LiquidCrystal.h>

void lcd_out(int state);

Thank you. That gets me past the compilation error, but I've not tested it yet. I still have to make other changes to the code before testing.

Delta_G:
Just for the record, a class that needs access to outside functions smells bad. You should instead let the class have a function that returns the ledState or whatever you want to display and have loop get that value from the class and let the main loop code do the stuff with the LCD.

That way the class is portable even to projects that don't have the LCD.

Thank you and noted! I will look into this further. In my case, it's alright if the code 'smells odd' because it will only be executed on my device. This now has me thinking maybe I should move all the lcd display code to this class, but that will take a lot of work. So consider this a starting point in moving in that direction.

This is actually my first attempt at creating a class and OOP outside of tutorials on the subject I've read, so any help in pointing me in the right direction is welcomed!

Thanks,
Randy