# Classes with Arduino

**URL:** <https://forum.arduino.cc/t/classes-with-arduino/1074105>\
**Category:** Programming\
**Created:** [January 6, 2023, 5:35pm UTC](https://forum.arduino.cc/t/classes-with-arduino/1074105 "2023-01-06T17:35:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![sloopjohnt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sloopjohnt/32/1099305_2.png) [@sloopjohnt](https://forum.arduino.cc/u/sloopjohnt)\
**Post date:** [January 6, 2023, 5:35pm UTC](https://forum.arduino.cc/t/classes-with-arduino/1074105/1 "2023-01-06T17:35:13Z")

</div>

I'm really surprised Object Oriented Programming is not given more prominence in the Arduino environment.

The Language Reference doesn't even mention "Class" when describing the Structure elements of Arduino C++ language, which led me (as an Arduino newby) to initially believe Arduino really supported a sort of C rather than real C++.

But this code works fine:

```cpp
//
// Flashes the built in LED once per second without using the 'delay' function.
// Written in object orientated style.
//
// Public Domain: Please feel free to use but citation appreciated
// JIT 06/01/23
//

class LED {
  private: bool state; // whether the LED is on or off
  private: unsigned long flashtime;
  public: void Flash(unsigned long current_time) {
    if (( flashtime + 1000) < current_time) {

      if ( state ) {
        digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
      } else
      {
        digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)}
    }
    state = !state;
    flashtime = millis();
  }};
};

LED builtinled;

void setup() {
    pinMode(LED_BUILTIN, OUTPUT); // initialize digital pin LED_BUILTIN as an output.
}

void loop() {
  builtinled.Flash(millis());
}

```

Why is "class" not mentioned in the documents?

Am I missing something?

---

<div class="post-metadata">

**Author:** ![Coding\_Badly](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@Coding\_Badly](https://forum.arduino.cc/u/Coding_Badly)\
**Post date:** [January 6, 2023, 5:51pm UTC](https://forum.arduino.cc/t/classes-with-arduino/1074105/2 "2023-01-06T17:51:46Z")

</div>

> [@sloopjohnt](#):
>
> Why is "class" not mentioned in the documents?

The intended audience are folks who have never programmed. Object oriented programming is not appropriate for such folks.

---

<div class="post-metadata">

**Author:** ![dlloyd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dlloyd/32/751911_2.png) [@dlloyd](https://forum.arduino.cc/u/dlloyd)\
**Post date:** [January 6, 2023, 6:09pm UTC](https://forum.arduino.cc/t/classes-with-arduino/1074105/3 "2023-01-06T18:09:29Z")

</div>

> [@sloopjohnt](#):
>
> Why is "class" not mentioned in the documents?  
> Am I missing something?

- [5,375 Libraries](https://www.arduinolibraries.info/)
- [Arduino Libraries Reference](https://www.arduino.cc/reference/en/libraries/)
- [Writing a Library for Arduino](https://docs.arduino.cc/learn/contributions/arduino-creating-library-guide) (this guide explains using classes)

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [January 6, 2023, 6:51pm UTC](https://forum.arduino.cc/t/classes-with-arduino/1074105/4 "2023-01-06T18:51:08Z")

</div>

> [@sloopjohnt](#):
>
> Why is "class" not mentioned in the documents

frightening word to blink a led 🙂

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [January 6, 2023, 7:12pm UTC](https://forum.arduino.cc/t/classes-with-arduino/1074105/5 "2023-01-06T19:12:27Z")

</div>

> [@sloopjohnt](#):
>
> The Language Reference doesn't even mention "Class" when describing the Structure elements of Arduino C++ language, which led me (as an Arduino newby) to initially believe Arduino really supported a sort of C rather than real C++.

That initial belief was jumping to the wrong conclusion.

C++ is C++.

BTW  
A **Structure** is nearly identical to a **Class**

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [January 6, 2023, 9:15pm UTC](https://forum.arduino.cc/t/classes-with-arduino/1074105/6 "2023-01-06T21:15:09Z")

</div>

> [@LarryD](#):
>
> A **Structure** is nearly identical to a **Class**

A function call is missing in a structure, but can be insert as a member of the structure.

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [January 6, 2023, 9:25pm UTC](https://forum.arduino.cc/t/classes-with-arduino/1074105/7 "2023-01-06T21:25:09Z")

</div>

Yes, as here:

```cpp

// ********************************************^************************************************
//a structure that creates TIMER objects
struct makeTimer
{
  //previousTime = the time this TIMER was (re)started
  //waitInterval = delay time (ms/us)we are looking for
  //restart = do we start this TIMER again and again
  //enableFlag = is this TIMER enabled/allowed to be accessed
  //timeType = true = millis(), false = micros()
  //
  // **********************
  //For each TIMER object we need to define it:
  //Example:
  // makeTimer myTimer = //give the TIMER a name "myTimer"
  // {
  // 0, 200UL, true, true, true //previousTime, waitInterval, restart, enableFlag, timeType
  // };
  // You have access to:
  // Variables: myTimer.previousTime, myTimer.waitInterval, myTimer.restart, myTimer.enableFlag, myTimer.timeType,
  // Functions: myTimer.checkTime(), myTimer.enableTimer(), myTimer.expireTimer()
  // **********************

  unsigned long previousTime;
  unsigned long waitInterval;
  bool restart;
  bool enableFlag;
  bool timeType;

  unsigned long currentTime;

  // ******************************************
  //Function to check if this TIMER has expired ex: myTimer.checkTime();
  bool checkTime()
  {
    if (timeType == true)
    {
      currentTime = millis();
    }
    else
    {
      currentTime = micros();
    }

    //has this TIMER expired ?
    if (enableFlag == true && currentTime - previousTime >= waitInterval)
      //Note: if delays of < 2 millis are needed, use micros() and adjust waitInterval as needed
    {
      //should this TIMER start again?
      if (restart)
      {
        //get ready for the next iteration
        previousTime = currentTime;
      }
      //TIMER has expired
      return true;
    }

    //TIMER has not expired or is disabled
    return false;

  } //END of checkTime()

  // ******************************************
  //Function to enable and initialize this TIMER, ex: myTimer.enableTimer();
  void enableTimer()
  {
    enableFlag = true;

    //initialize previousTime to current millis() or micros()
    if (timeType == true)
    {
      previousTime = millis();
    }
    else
    {
      previousTime = micros();
    }

  } //END of enableTimer()

  // ******************************************
  //Function to force this TIMER to expire ex: myTimer.expireTimer();
  void expireTimer()
  {
    //force this TIMER to expire now
    previousTime = currentTime - waitInterval;

  } //END of expireTimer()

}; //END of structure “makeTimer”

// ********************************************^************************************************

```

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [January 6, 2023, 9:28pm UTC](https://forum.arduino.cc/t/classes-with-arduino/1074105/8 "2023-01-06T21:28:15Z")

</div>

C O P I E D 🤓

---

<div class="post-metadata">

**Author:** ![christop](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/christop/32/1013044_2.png) [@christop](https://forum.arduino.cc/u/christop)\
**Post date:** [January 6, 2023, 9:31pm UTC](https://forum.arduino.cc/t/classes-with-arduino/1074105/9 "2023-01-06T21:31:24Z")

</div>

The only difference between a `struct` and a `class` in C++ is that a `struct` has `public` access by default.

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [January 6, 2023, 9:32pm UTC](https://forum.arduino.cc/t/classes-with-arduino/1074105/10 "2023-01-06T21:32:42Z")

</div>

nice to know, thanks

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [January 6, 2023, 9:44pm UTC](https://forum.arduino.cc/t/classes-with-arduino/1074105/11 "2023-01-06T21:44:46Z")

</div>

```cpp

}; //END of structure “makeTimer”

// ********************************************^************************************************

//Let's create and and initialize 2 "makeTimer" objects

// ***************************
makeTimer redLEDtimer = //create redLEDtimer
{
  0, redLEDonOffInterval, true, true, true //previousTime, waitInterval, restart, enableFlag, true=millis/false=micros
};

// ***************************
makeTimer greenLEDtimer = //create greenLEDtimer
{
  0, greenLEDoffInterval, true, true, true //previousTime, waitInterval, restart, enableFlag, true=millis/false=micros
};

```

---

<div class="post-metadata">

**Author:** ![westfw](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/westfw/32/213443_2.png) [@westfw](https://forum.arduino.cc/u/westfw)\
**Post date:** [January 7, 2023, 12:31am UTC](https://forum.arduino.cc/t/classes-with-arduino/1074105/12 "2023-01-07T00:31:37Z")

</div>

> [@sloopjohnt](#):
>
> Why is "class" not mentioned in the documents?

In general, Arduino documentation does not mention "advanced" features of the underlying language. I don't think you'll find "struct" mentioned in the Arduino docs, either. Certainly not "typedef"...  
This is an attempt to cater to "beginners" - the idea is that if you need info about t the language beyond what's in the Arduino docs, you need to consult the language references instead (or "in addition")

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [January 7, 2023, 1:11am UTC](https://forum.arduino.cc/t/classes-with-arduino/1074105/13 "2023-01-07T01:11:23Z")

</div>

> [@Coding\_Badly](#):
>
> The intended audience are folks who have never programmed. Object oriented programming is not appropriate for such folks.

I wonder about that premise. If you're starting from zero, is learning OOP really more difficult than procedural coding? Since I learned the latter first, I have no way of knowing.

---

<div class="post-metadata">

**Author:** ![Coding\_Badly](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@Coding\_Badly](https://forum.arduino.cc/u/Coding_Badly)\
**Post date:** [January 7, 2023, 1:21am UTC](https://forum.arduino.cc/t/classes-with-arduino/1074105/14 "2023-01-07T01:21:27Z")

</div>

It's impossible to understand polymorphism without an understand of functions. Learning object oriented program requires some foundation.

---

<div class="post-metadata">

**Author:** ![westfw](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/westfw/32/213443_2.png) [@westfw](https://forum.arduino.cc/u/westfw)\
**Post date:** [January 7, 2023, 3:23am UTC](https://forum.arduino.cc/t/classes-with-arduino/1074105/15 "2023-01-07T03:23:14Z")

</div>

> [@gfvalvo](#):
>
> is learning OOP really more difficult than procedural coding?

The Arduino documentation is weak on encouraging "procedures" as well.  
All the time you see posted code with all of the user program logic stuffed into loop(), and people unclear on the idea that loop() and setup() are just procedures.  
"Procedures" == "functions", in C.

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [January 7, 2023, 4:05am UTC](https://forum.arduino.cc/t/classes-with-arduino/1074105/16 "2023-01-07T04:05:31Z")

</div>

> [@westfw](#):
>
> "Procedures" == "functions", in C.

No, they're "voids" 😅 🤣 😂 😆

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [January 7, 2023, 4:12am UTC](https://forum.arduino.cc/t/classes-with-arduino/1074105/17 "2023-01-07T04:12:11Z")

</div>

Aren't we talking about sub-routines?

```cpp
bsr.l askStupidQuestions 

```

🤓

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [January 7, 2023, 5:07am UTC](https://forum.arduino.cc/t/classes-with-arduino/1074105/18 "2023-01-07T05:07:09Z")

</div>

> [@christop](#):
>
> The only difference between a `struct` and a `class` in C++ is that a `struct` has `public` access by default.

Does _struct_ provide data protection as is done by _class_?

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [January 7, 2023, 6:06am UTC](https://forum.arduino.cc/t/classes-with-arduino/1074105/19 "2023-01-07T06:06:04Z")

</div>

What do you mean by data protection?

Abstraction, encapsulation, inheritance, and polymorphism are four of the key principles of object-oriented programming you need to understand and master. Data "protection" can be a side effect of abstraction or encapsulation.depending how you define protection (private?)

---

<div class="post-metadata">

**Author:** ![noiasca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/noiasca/32/1126910_2.png) [@noiasca](https://forum.arduino.cc/u/noiasca)\
**Post date:** [January 7, 2023, 6:55am UTC](https://forum.arduino.cc/t/classes-with-arduino/1074105/20 "2023-01-07T06:55:40Z")

</div>

> [@GolamMostafa](#):
>
> Does _struct_ provide data protection as is done by _class_?

a struct has the same access modifier public, protected, private like a class. Only the default behaviour is different like already mentioned (struct-public, class-private).

[https://cplusplus.com/doc/tutorial/inheritance/](https://cplusplus.com/doc/tutorial/inheritance/)

[Next page](https://forum.arduino.cc/t/classes-with-arduino/1074105.md?page=2)
