Object, instance of an object, function

Hello -

I fadged together some other peoples sketches to print the Lux value from a BH1750 light sensor to an OLED, and also to switch a relay that turns on one LED, or another one, depending on how much light there is.

And I think I understand it. Except that when I was commenting it I called something an Object, something else an instance of an Object, and something else a function. I did this based on what I remember from when I was trying to program in Javascript. As I understand it, an Arduino sketch is written in C.

Is my labeling of an Object, etc. correct ?

/*
BH1750 & OLED 1306 library usage.
 
This sketch reads the BH1750 and either sends a 1 or a 0 to pin 11, depending on
if the LUX value is above or below the threshold value. Pin 11 is connected to a
5v relay. If a 1, one LED is turned on. If a 0, the other LED is turned on.
This will be used to control the direction of a DC motor, instead of two LEDs.
 
Connection:
 VCC-3.3v  (Powers both the BH1750 & the 1306 OLED)   BH1750 2.4v to 3.6v   1306 OLED 1.65v to 3.3v
 GND-GND
 SCL-SCL(analog pin 5) Clock Line
 SDA-SDA(analog pin 4) Data Line
 ADD-NC  Not Connected
*/
 
#include <Wire.h>
#include <BH1750.h>

#include <SPI.h>  // If using the Wire.h library, SPI.h must be included
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);     //this reset is required to intitiate the OLED
 
BH1750 lightMeter;    // starts an instance of the BH1750 object
 
void setup(){
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);   // starts the display located at address 0x3C
  
  pinMode(13, OUTPUT);
  digitalWrite(13,LOW);     // turn off the UNO on-board LED
  pinMode(11, OUTPUT);     // initialize digital pin 11 as an output.
    
  lightMeter.begin();   //begins function lightMeter
  }
 
 void loop() {
  unsigned int lux = lightMeter.readLightLevel();    //Reads the light level
  delay(500);
  
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setCursor(18,20);   //starts at Row 18 & Col 20
  display.setTextSize(2);
   
  String stringOne = "";
  String stringTwo = stringOne + lux;
  String stringThree = stringTwo + " LUX";
  display.println(stringThree);
  display.println("");   // adds a linebreak
  display.display();      // this command must be used after EVERY display.println
  display.setTextSize(1);
  if (lux > 80)
  { 
  display.println(" Hello frogging World");
  digitalWrite(11,HIGH);
  }
  else
  {
    display.println(" Lights Out");
    digitalWrite(11,LOW);
  }
  display.display();
  
  delay(1500);  // wait for 1.5 second
  display.clearDisplay();
  display.display();
  
  delay(1500);          
}

As I understand it, an Arduino sketch is written in C.

C++, not C.

I suggest you start managing without the String class, which tends to fragment memory.

  lightMeter.begin();   //begins function lightMeter

Calls function lightMeter.begin() is more accurate. In fact the comment is stating the bleeding obvious, so I would omit it.

Nick -

It's not so obvious to me. I have more or less given up trying to distinguish between Java and C and Javascript and Classes and Objects. But not completely.

When I start a new Arduino project I look back thru any of my similar sketches and read my comments to remind myself what is what.

Which is the reason I asked if my comments were correct. Dumb I can live with. Obvious I can live with. But wrong will goof me up in the future.

BH1750 lightMeter;    // starts an instance of the BH1750 object

Yes you could say you instantiate (create) an instance of the object.

Excellent. thanks. I've revised my comments.

Regarding the Strings. At one point I was trying to get the OLED to display, on one line, "blah" value "blah". The only way I could figure out to do it (i googled the problem) was with Strings.
I tried + concatenation, and one or two other techniques, but with no success.

Care to point me another direction ?

Care to point me another direction ?

Use an array of chars

char printBuffer[30];  //char array large enough to hold the output plus a terminating zero

void setup()
{
  Serial.begin(115200);
  int value = 123;
  sprintf(printBuffer, "blah %d blah", value);  //build the output in the char array
  Serial.println(printBuffer);
}

void loop() {}

OK. that certainly gave me something new to noodle on. So far it's confusing. for example if I say char printBuffer[5], it gives me blah 999 blah, and starts over at 99.

But if I say lah %d blah, it goes up to 9999 before going back to 99.

So I'm not sure if the array is for characters alone or characters & integers. confusing.

I will work on it.

I think I can see that, once I get the hang of it, it will reduce the amount of memory set aside.

char printBuffer[5];  //char array large enough to hold the output plus a terminating zero
int value = 99;
void setup()
{
  Serial.begin(9600);
 }

void loop() {
  sprintf(printBuffer, "lah %d blah", value);  //build the output in the char array
  Serial.println(printBuffer);
  delay(300);
  value=(value+10);
  }

The char array needs to be large enough to hold each character that will be put in it plus 1 for the terminating zero that sprintf() will add automatically to turn the array into a C style string (lowercase s). If the array is too small then all bets are off as to what will happen because sprintf() will write over memory that is not allocated to be used by it. It is, however, safe to allocate too many chars in the array as I did.

Calls function lightMeter.begin() is more accurate.

Calls method begin() would be even more accurate.

PaulS:
Calls method begin() would be even more accurate.

Not in C/C++, everything is a function regardless of scope. The standard makes no mention of a function being called a 'method'. Just a scrap dragged in from other languages.

Not in C/C++, everything is a function regardless of scope

@pYro_65: While that's true, I find it useful to make the distinction that Paul makes, especially when you're mixing C and C++ in the same program.

@pratto: As you know, C++ allows you to declare something called a class. The class is simply a description of an object, much like a set of blueprints describes a house. Just as you cannot "live" in a set of blueprints, nor can you use a class by itself in a program. You must first create an object of the class. You have no doubt done this in programs you've tried. For example:

LiquidCrystal lcd(a,b,c,d,e);

Uses the blueprints from the class named LiquidCrystal to create an object of that class called lcd. The process of creating an object of a class is called instantiation. Inside the class declaration is code to clear the display (e..g, lcd.clear()), set the cursor to a specific location (e.g., lcd.setCursor(x, y)), and so on. pYro_65 wants to call these chunks of code "functions", while most OOP programmers call them "methods". To me, they are not the same. A function is simply a call to a piece of code, like:

delay(1000);

However, to use a "function" that is associated with a class, you MUST reference that "function" using the class object (e.g., lcd), the dot operator (e.g., '.'), and the "function" that is defined within the class definition, as in:

lcd.setCursor(1,5);

Because access to the "function" defined within a class can only be done through the class object, it is useful to call it a method so the user knows they are working with a class object.

Anything that makes a discussion among programmers more clear is a good thing, and programmers are pretty bad at it. They treat define and declare as the same when they are not, and now we want to obfuscate method and function. Personally, I vote for method for code routines buried within a class and functions when they are not. I'm sure I'm poking a number of bears with a pointy stick, but I honestly think making the distinction is useful.

econjack:
pYro_65 wants to call these chunks of code "functions", while most OOP programmers call them "methods".

Because access to the "function" defined within a class can only be done through the class object, it is useful to call it a method so the user knows they are working with a class object.

Your discussion is great, however nothing more than opinion, 'method' is a catch all term used indiscriminately for any function throughout many different languages, for sure it has it OO roots, but C does not make a comparison between 'method' and function.

When talking about C or C++ its irrelevant what you prefer, a function inside a class is either a class function or a member function, not a method.

C does not make a comparison between 'method' and function.

I wouldn't expect it to since it's not an OOP language. Function works perfectly well when talking about C.

...a function inside a class is either a class function or a member function, not a method.

Like mine, that's your opinion. If you wish to refer to it as a class function or a member function, you are free to do so. However, why use two words when you can make the distinction with one? In the context of Arduino programming, which seems to be the emphasis in this Forum, using the term function could be viewed as only applying to the C variant and method to the C++ variant. If this group could come to a consensus on the two, I think discussions would be more clear (and, perhaps, shorter). Using function by itself on this Forum does nothing to distinguish its heritage. However, if you use method, I, for one, would look for the object used to access the method. If you say function, there's no need for an object search.

All I'm saying is making the distinction imparts information that is absent when you use function to describe two similar, albeit different, concepts.

econjack:
I wouldn't expect it to since it's not an OOP language. Function works perfectly well when talking about C.

Like mine, that's your opinion. If you wish to refer to it as a class function or a member function, you are free to do so. However, why use two words when you can make the distinction with one?

Nope, not my opinion at all... And why use two words instead of one? ... Because it is correct.

The difference between 'member function' and 'function' is no way ambiguous. But like they say "you can put lipstick on a pig"...

Mind if I chime in? :slight_smile:

While I generally prefer to call functions inside a class - methods

I think it may be confusing for beginners to add 'yet another technicality' to their learning experience.

I say let them be functions!

YemSalat:
Mind if I chime in? :slight_smile:

While I generally prefer to call functions inside a class - methods

I think it may be confusing for beginners to add 'yet another technicality' to their learning experience.

I say let them be functions!

Exactly, stick with the terminology used in the language the beginners are using. Creating a soup of synonyms is not really helping, even if they are less letters or only a single word.

Exactly, stick with the terminology used in the language the beginners are using.

So, when someone comes along and wants to know why the begin() function is not documented on the reference page, we'll just refer them to you.

PaulS:
So, when someone comes along and wants to know why the begin() function is not documented on the reference page, we'll just refer them to you.

To be honest, most of the people I saw coming to the forum in the past week (I mean new members) don't even know what a function is.

And I think anyone who asks that kind of quetion would be comfortable enough with their programming skills to be introduced to the concept of 'methods'

PS Which begin() are you refering to? Serial.begin() is documented.

PS Which begin() are you refering to? Serial.begin() is documented.

Exactly. If the poster uses correct (accepted, rather) terminology, and distinguishes between function and method, we know where to direct them.

There are many different begin() methods. There is no begin() function. Knowing what the poster is referring to goes a long way towards being able to help them.