Notifications
Clear all

[Closed] Object Oriented techniques - Example 1 - "Hello Class"

302 Posts
10 Users
229 Likes
20.5 K Views
 Biny
(@binaryrhyme)
Member
Joined: 2 years ago
Posts: 269
Topic starter  

Folks:

As promised, I am providing a few examples of how to use OO techniques in C++ for microcontrollers. The first example is a rewrite of the classic Arduino "Hello World" sketch. It's very basic, but at the same time will seem over complex for what it accomplishes. The benefits will become clear in subsequent examples, I hope, but thought we should start with how classes are constructed using a familiar example.

/* ********** Constant Declarations ********** */
const int PIN_GeenLed = 8;


/* ********** LED Class ********** */
class CLed
{
   public:
      CLed(int LedPin, int blinkDelay);
      void LoopTick();
   private:
      int _ledPin;
      int _blinkDelay;
};

/* CLed Constructor */
CLed::CLed(int LedPin, int blinkDelay) {
   _ledPin = LedPin;
   _blinkDelay = blinkDelay;

  // Initialize LED Pin
  pinMode(_ledPin, OUTPUT); // Green LED Pin
}

void CLed::LoopTick() {
   digitalWrite(_ledPin, HIGH); // turn on Green LED
   delay(_blinkDelay);

   digitalWrite(_ledPin, LOW); // turn off Green LED
   delay(_blinkDelay); 
}

/* CLed - End Class */


/* ************************************************** */
/* ********** Global Variable Declarations ********** */
int BlinkWait = 100;

/* ********** Global Class Instances ********** */
CLed GreenLed(PIN_GeenLed, BlinkWait);



/* ************************************ */
/* ********** Setup Function ********** */
void setup() {

  // Initialize Serial Ports
  Serial.begin(9600); // Serial Monitor

}

/* ********** Main Loop ********** */
void loop() {

   Serial.print("Top of Loop\n");

   GreenLed.LoopTick();
}

I edit my posts to fix typos, correct grammar, or improve clarity. On-screen keyboards are evil.


   
Lee G, ron bentley, YurkshireLad and 1 people reacted
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2043
 

   
Lee G, ron bentley, DualFuel and 1 people reacted
 Biny
(@binaryrhyme)
Member
Joined: 2 years ago
Posts: 269
Topic starter  

@robotbuilder Yeah, this example is intended to be stupid simple (delays). I'm putting the finishing touches on a demo of multiple LEDs with different blink characteristics using a display refresh manager and measured elapsed time with no blocking code to show something a bit beefier. 😉

Not familiar with that language - my mainstay is C#, but have a bit of interesting history with C++, lol.

I edit my posts to fix typos, correct grammar, or improve clarity. On-screen keyboards are evil.


   
Inst-Tech reacted
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2043
 

@binaryrhyme 

Processing is an easy to use language that I am using to interface with an Arduino.
https://processing.org/
It becomes a Java program I think but is much easier to use with graphics and the IDE is complementary to the Arduino IDE.

To enlarge image, right click image and choose open link in new window.

ProcessingImage

 


   
Biny reacted
 Biny
(@binaryrhyme)
Member
Joined: 2 years ago
Posts: 269
Topic starter  

@robotbuilder I'll poke through your sketch tomorrow - deep frying atm. 😉

I edit my posts to fix typos, correct grammar, or improve clarity. On-screen keyboards are evil.


   
Inst-Tech reacted
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2043
 

@binaryrhyme 

Tried c# but it was too hard for me to use.  It seemed like a cryptic version of vb net. They added the squiggly braces to make it look professional.  Also I wanted code that would run on Linux as well.  I used to use TurboC++ on the old MSDOS PC.

 

 


   
Biny reacted
 Biny
(@binaryrhyme)
Member
Joined: 2 years ago
Posts: 269
Topic starter  

@robotbuilder Yeh. C# is basically Java with all the problems fixed. As elegant a language as I've encountered in 40 years. 😉

I edit my posts to fix typos, correct grammar, or improve clarity. On-screen keyboards are evil.


   
Inst-Tech reacted
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1122
 
Posted by: @binaryrhyme

I am providing a few examples of how to use OO techniques in C++ for microcontrollers.

I'm sure your examples will prove of interest to those now getting passed the basics of arduino coding.  As a complement to your detailed code examples I give a link to a video of what I think is a very good overview of the why this would be worth perusing when writing ones sketches and the sort of complexities and payoff's that are involved.  Its also probably easier to understand the 'why' when presented as a video and thus I hope this will complement and enhance your educational effort. 👍 

 


   
Ron, ron bentley, Inst-Tech and 1 people reacted
 Biny
(@binaryrhyme)
Member
Joined: 2 years ago
Posts: 269
Topic starter  

@byron Yeh, I think I'll do a vid explaining the next example, with a walk through of the code. Nice vid. Thanks.

I edit my posts to fix typos, correct grammar, or improve clarity. On-screen keyboards are evil.


   
Inst-Tech and DualFuel reacted
 Biny
(@binaryrhyme)
Member
Joined: 2 years ago
Posts: 269
Topic starter  

@robotbuilder Hmm. Weird. I copied your sketch in verbatim, changed the pin numbers to suit my board and ran it and I get serialized behaviour - LED1 goes on and off, then LED2 goes on and off. 

I edit my posts to fix typos, correct grammar, or improve clarity. On-screen keyboards are evil.


   
Inst-Tech and DualFuel reacted
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2043
 

@binaryrhyme 

Silly me!!  Because of the angle I was viewing the row of leds when LED_2 was on it shone through LED_1 making it appear to be on as well.  When viewing face on to the row it works as expected.

To enlarge image right mouse click and Open link in new window.

ledRow

 

 


   
 Biny
(@binaryrhyme)
Member
Joined: 2 years ago
Posts: 269
Topic starter  

@robotbuilder Woot! Mystery solved. 🙂

I edit my posts to fix typos, correct grammar, or improve clarity. On-screen keyboards are evil.


   
ron bentley
(@ronbentley1)
Member
Joined: 2 years ago
Posts: 385
 

Hi,

Just read your OO  code example post and thought it was very good and instructive for those starting to understand OO in c++.

Keep if coming!

 

Ps

I read the extension to 2 leds as posted and I could not see from the code why it was behaving in t.he way suggested.

Then I read the answer! I've also fallen fowl of that same issue!! Ha  ha!

Ron Bentley

Ron Bentley
Creativity is an input to innovation and change is the output from innovation. Braden Kelley
A computer is a machine for constructing mappings from input to output. Michael Kirby
Through great input you get great output. RZA
Gauss is great but Euler rocks!!


   
Biny and Inst-Tech reacted
 Biny
(@binaryrhyme)
Member
Joined: 2 years ago
Posts: 269
Topic starter  

@ronbentley1 Woot! Yeah, I've been afk, more or less, for the past few days - but the DisplayManager demo was working, and then I thought... can I make this a library? ... hit some issues ... and then I wondered if I should switch to VSCode... and THAT was an adventure, lol (short answer - I'm using VSCode for offline editing on my main machine, and sticking with version 2 of the Arduino IDE for the bench machine) ... with the sketches sync'd on one drive, which was itself a minor rabbit hole of its own. To make a short story long, it's coming, and is considerably more substantial, lol.

Glad you like.

I edit my posts to fix typos, correct grammar, or improve clarity. On-screen keyboards are evil.


   
Inst-Tech reacted
(@ronsmitsnl)
Member
Joined: 2 years ago
Posts: 20
 
Posted by: @binaryrhyme

@robotbuilder Yeh. C# is basically Java with all the problems fixed. As elegant a language as I've encountered in 40 years. 😉

As a professional java and kotlin architect, I am crying a little now 🙂 we usually say that C# is a poor copy of Java 🙂

 

This is all meant in jest, please do not start a flamewar, I love you all even if you use C#


   
Biny and Ron reacted
Page 1 / 21