from c to c++ arduino with oop (object oriented...

43
from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel Kosolapov

Upload: others

Post on 19-Jan-2020

30 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

from C to C++Arduino with OOP

(Object Oriented Programming)

Lecturer: Dr. Samuel Kosolapov

Page 2: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

Disclaimer

2

OOP and C++ are not part of the Syllabus of Electronic Instrumentation.

Students will not be REQUESTED to create OOP code.

BUT, because all modern software uses OOP, students will be asked to understandwhat OOP code do

in the Arduino sketchesand in Windows Form programs

presented in the lectures and examples

Additionally, lecturer (naively) expects that students will try to understandwhy OOP is so widely used in modern projects

and some day will use it in their’ real projects

Page 3: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

Items to be explained

• What is “wrong” with “plain C” • What is wrong with our simple “Blinking LED” program (sketch)• Why all modern software is OOP• Some (but not all !!!) ideas of OOP• OOP terminology:

Class, Object, Constructor, Destructor, Field, Member, Method• What is inside “h” and “cpp” files• Extremely simple ArduinoLed class. Example of usage• Creation of ArduinoLed library: step by step• Serial library

3

Page 4: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

What is “wrong” with “plain C”

4

From “Why C is a good language - or why you're wrong thinking C++ is better”http://blog.grumpycoder.net/index.php/post/2012/03/02/Why-C-is-a-good-language-or-why-you-re-wrong-thinking-C-is-better

The reason why some programmers think C++ is superior to Cis because they're bad programmers.

Yes !!! 100% correct. “Good” programmer does not need C++ .Actually, good programmer do not need C and even assembler.“Good” programmer can program directly in machine code. Good programmer never write code with errors. Hence machine code is all he/she needs. Bold dot.

The only problem is that programmers are (still) (mostly) humans. But humans DO errors while programming all the time:

make typos, forgot to do important things, call functions with wrong arguments, etc…

“Human Programmer” == “Bad programmer”

Page 5: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

What is wrong with our simple “Blinking LED” program (sketch)

5

ledPin in the following lines must be the same.pinMode(ledPin, OUTPUT);digitalWrite(ledPin, HIGH);digitalWrite(ledPin, LOW);

int ledPin = 13;

void setup(){

pinMode(ledPin, OUTPUT); }

void loop() {

digitalWrite(ledPin, HIGH); delay(100); digitalWrite(ledPin, LOW); delay(1000);

}

Now, suppose, some “good”(but unexperienced with Arduino board) programmer writes:

digitalWrite(ledPin, HIGH); delay(100); ledPin = 2;digitalWrite(ledPin, LOW); delay(1000);

It is legal to change variable at any place in C. But sketch will not work…..

The problem is not with “C”, but with lack of knowledgeof the human programmer

Page 6: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

What is wrong with our simple “Blinking LED” program (sketch)

6

Suppose additional LED is added (led1)int ledPin = 13; int ledPin1 = 12;

void setup(){

pinMode(ledPin, OUTPUT); }

void loop() {

digitalWrite(ledPin, HIGH); digitalWrite(ledPin1, HIGH);delay(100); digitalWrite(ledPin, LOW); delay(1000);

}

With “C” it is very easy to forgot to add pinMode(ledPin1, OUTPUT); in the “setup()”

and digitalWrite(ledPin1, LOW); in “loop()”

Additional problem is that correct function can be called with wrong data (say, wrong pin number) and opposite. It is legal in C to do this.

Again, “good” programmer never forgot anything,But human programmer do forgot.

What if the programmer will write 113 instead of 13 ?With the hardware the result may be catastrophic.Are you sure that ANY programmer can be allowed to write such code ?

Page 7: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

Why all modern software is OOP

• OOP provides much better modular structure for programs.• “Experienced” programmer writes sophisticated part of OOP code

so that implementation details are hiddenfrom “unexperienced”/”normal”/”bad” programmer

• “Normal” human programmer uses classes by using “human friendly” syntax making many typical errors impossible

Resulted code is much more reliableHuge projects can be implementedModern compilers “optimize” the code, so that speed of “C++” code is

~ the speed of “C”

7

Page 8: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

Some (but not all !!!) ideas of OOP

• Data and Code together in one “block”

Function call can contains less arguments

less chances to call function with wrong arguments

• In many cases functions have no arguments at all (void)

• Words “private”, “protected”, “public” used to divide responsibility:

experienced programmer can do anything by writing

sophisticated code of the “class”,

whereas user of class can use only what is “published” for him

in the “public” section

Less dull errors More reliable code

8

Page 9: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

OOP terminology: Class, Object, Constructor, Destructor, Member, Method

9

Unfortunately, from the beginning, OOP creators started to use their own language.

First time reader of OOP book is immediately frightened and run away.

OOP terminology will be explained by examples: step by step.

Page 10: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

What is inside “h” and “cpp” files

10

In plain “C””h” files contains declarations, variables and function prototypes

Generally, customer can get “h” file to know what declarations, variables and functions are available to him.

In plain “C””cpp” files contains additional declarations, additional variables

functions implementation (code)

In most cases “cpp” file is send to the user as “lib” or as “dll”.In this case: Customer can use what is written in “h” file

(for example: to call functions mentioned in the “h” file)but code implementation (“secrets of the experienced programmer”

cannot be seen.

Page 11: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

Extremely simple ArduinoLed class. Example of usage

11

Exemplary ArduinoLed class with minimum features.

In this example we have 3 files:ArduinoLed.hArduinoLed.cpp

andExample1.ino

Q. Do you agree that everyone

can understand what Arduinowill do from reading loop code ?

A. Yes, syntax with “.” is very clearit is nearly plain English

#include <ArduinoLed.h>// Demostrates how to BlinkLed by using ArduinoLed library

ArduinoLed myLed = ArduinoLed();

void setup(){}

void loop(){

myLed.ledOn();delay(50);

myLed.ledOff();delay(1000);

}

This “cryptic line will be explained later

“Magic” number “13” did not appears here.

Is this good or bad for the customer?

Page 12: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

File ArduinoLed.h

12

#ifndef ArduinoLed _h#define ArduinoLed _h

#include "Arduino.h"

#define ARDUINO_UNO_R3_BUILDIN_LED (13)

// library interface descriptionclass ArduinoLed{

// user-accessible "public" interfacepublic: ArduinoLed(void);

void ledOn(void);void ledOff(void);

};

#endif

#ifndef ArduinoLed _h#define ArduinoLed _h

…..

#endifTo ensure that

ArduinoLeddeclarations will be

“included” only once

#define ARDUINO_UNO_R3_BUILDIN_LED (13)

Standard “C” declaration.This is still not an OOP way.Customer still can redefine

and misuse this…

Page 13: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

File ArduinoLed.h

13

class ArduinoLed{

// user-accessible "public" interfacepublic:

ArduinoLed(void);void ledOn(void);void ledOff(void);

};

Class declaration(Looks like “C” structure.

Actually, today structures in VisualStudio are classes)

End of class declaration“;” is important. Do not forget it

Page 14: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

File ArduinoLed.h

14

class ArduinoLed{

// user-accessible "public" interfacepublic:

ArduinoLed(void);void ledOn(void);void ledOff(void);

};

Public: Customer can use the following staff

Page 15: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

File ArduinoLed.h

15

class ArduinoLed{

// user-accessible "public" interfacepublic:

ArduinoLed(void);void ledOn(void);void ledOff(void);

};

This “function” is called “constructor”

It has a special syntax and special role (to be explained later)

Page 16: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

File ArduinoLed.h

16

class ArduinoLed{

// user-accessible "public" interfacepublic:

ArduinoLed(void);void ledOn(void);void ledOff(void);

};

This “function” are self-explanatory.No arguments Nothing is returned.

Page 17: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

File ArduinoLed.cpp

17

// for basic declarations and functions#include "Arduino.h"

// include this library's description file#include "ArduinoLed.h“

….

// Constructor // Function that handles the creation and setup of instances

ArduinoLed::ArduinoLed(void){

// do whatever is required to initialize the librarypinMode(ARDUINO_UNO_R3_BUILDIN_LED, OUTPUT);

}

Strange “OOP” syntax: “::”

Normal Arduino function is called

Page 18: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

File ArduinoLed.cpp

18

Writer of the class can do anything.Even to write “bad values”, etc…

// Public Methods //////////////////////////////////////////////////////////////// Functions available in Wiring sketches, this library, and other libraries

void ArduinoLed::ledOn(void){

digitalWrite(ARDUINO_UNO_R3_BUILDIN_LED, HIGH);}

void ArduinoLed::ledOff(void){

digitalWrite(ARDUINO_UNO_R3_BUILDIN_LED, LOW);}

Strange “OOP” syntax: “::”

Normal Arduino functions are called

Page 19: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

Example of usage again. Script Example1.ino

19

Important to understand: “int” is abstraction, does not take room in the memory. So, “ArduinoLed” is an abstraction,does not take room in the memory.

#include <ArduinoLed.h>// Demostrates how to BlinkLed by using ArduinoLed library

ArduinoLed myLed = ArduinoLed();

void setup(){}

void loop(){

myLed.ledOn();delay(50);

myLed.ledOff();delay(1000);

}

Compare this with “C” line:int ququ = 13;

ArduinoLed is “like” int(integer)

that is type

Page 20: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

Example of usage again. Script Example1.ino

20

myLed is called“instance of the class ArduinoLed”

(like ququ is called“instance of the integer” in the plain “C”

“ququ” really takes a room in the memory (4 bits)”myLed” also takes some room in the memory

#include <ArduinoLed.h>// Demostrates how to BlinkLed by using ArduinoLed library

ArduinoLed myLed = ArduinoLed();

void setup(){}

void loop(){

myLed.ledOn();delay(50);

myLed.ledOff();delay(1000);

}

Compare this with “C” line:int ququ = 13;

“myLed” is a name of the “object

Page 21: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

Example of usage again. Script Example1.ino

21

Ququ was set to 13 in in this line(when “ququ was born in memory”)

Modern compilers accept the line:int ququ = int(13);

int(13) is a constructor. It is creates in the memory instance of the integer (region in the memory having name “ququ”, and the relevant bytes are filled with value equivalent to 13 )

#include <ArduinoLed.h>// Demostrates how to BlinkLed by using ArduinoLed library

ArduinoLed myLed = ArduinoLed();

void setup(){}

void loop(){

myLed.ledOn();delay(50);

myLed.ledOff();delay(1000);

}

Compare this with “C” line:int ququ = 13;

Instance of ArduinoLed classhaving type myLed is born in memory.Constructor is automatically called at this moment(well, small litters for Arduino: when compiler decides to do this)

Page 22: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

Example of usage again. Script Example1.ino

22

User of the class can do what he needed:Switch on LED positioned on the Arduino UNO board

and switch it off.User of the class is not obliged to know

what pin is usedand how exactly it is manipulated.

#include <ArduinoLed.h>// Demostrates how to BlinkLed by using ArduinoLed library

ArduinoLed myLed = ArduinoLed();

void setup(){}

void loop(){

myLed.ledOn();delay(50);

myLed.ledOff();delay(1000);

}

OOP functionsledOn and ledOff

are called “members”or “member functions”

Sometimes these functions are called “methods”

Page 23: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

Class MyLed.

23

In the previous example only Arduino Led on the Pin 13 can be blinked

Now, private ledPin variable be added,So that more Leds can be operated

Why private ?1. Because if can be set only in the constructor (Why ?)2. Because not any pin value can be set even in the

constructor

Page 24: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

Class MyLed. MyLed.h

24

#ifndef MyLed _h#define MyLed _h#include "Arduino.h"#define ARDUINO_UNO_R3_BUILDIN_LED (13)

// library interface description

class MyLed{// user-accessible "public" interfacepublic:

MyLed(int _ledPin = ARDUINO_UNO_R3_BUILDIN_LED);void ledOn(void);void ledOff(void);

// library-accessible "private" interfaceprivate:int ledPin;void doSomethingSecret(void);

};#endif

Explain “default arguments

This staff cannot be used by user of the class

Data and Code together

Page 25: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

Class MyLed. MyLed.cpp

25

// for basic declarations and functions#include "Arduino.h"// include this library's description file#include "MyLed.h"// Constructor /////////////////////////////////////////////////////////////////// Function that handles the creation and setup of instances

MyLed::MyLed(int _ledPin){if (_ledPin > ARDUINO_UNO_R3_BUILDIN_LED){// Arduino has no error protection mechanism,// so, think what can be done in Arduino// but surely do not continue with pinMode settingsreturn;}// initialize this instance's variablesledPin = _ledPin;

// do whatever is required to initialize the librarypinMode(ledPin, OUTPUT);

}

User can try to set another value (not 13)

But this is a good place to checkif the proposed value is legal

better reliability

Not all OOP possibilities are available for Arduino.In case of Windows, well known error message may be presented

Page 26: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

Class MyLed. MyLed.cpp

26

// Public Methods // Functions available in Wiring sketches, this library, and other librariesvoid MyLed::ledOn(void){

digitalWrite(ledPin, HIGH);doSomethingSecret();

}

void MyLed::ledOff(void){

digitalWrite(ledPin, LOW);}

// Private Methods// Functions only available to other functions in this library

void MyLed::doSomethingSecret(void){

// No secret operations for now// Some idea:// if ledPin == 12 )// send secret message by using RS232}

This function can be called here.(but not in “main” sketch)

ledPin value was tested in the constructor, so it is safe to use it here

Page 27: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

Class MyLed. Example2.ino

27

#include <MyLed.h>

MyLed myLed1 = MyLed();MyLed ququLed = MyLed(12);

void setup(){}

void loop(){

myLed1.ledOn();ququLed.ledOn();delay(200);

ququLed.ledOff();delay(2000);

myLed1.ledOff();delay(1000);

}

This instance will use default: pin 13

Try to write this without OOP to see the difference in “loop” readability

This instance will use pin 12(Active buzzer can be used in Demo)

Page 28: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

How to write, distribute and install YOUR class as a library

28

Create somewhere in Your computer directory ArduinoLed

Write files “ArduinoLed.h” and Arduino.cpp (inside ArduinoLed directoryUse Notepad, VisualStudio, etc. There is an option to do this with Arduino IDE , but this is not simple

All the process is described in details at: https://www.arduino.cc/en/Hacking/LibraryTutorialhttp://playground.arduino.cc/Code/Library

Exemplary Test directory can be used, but it contains some outdated features…

Page 29: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

How to write, distribute and install YOUR class

29

Write/Edit file “keywords.txt” by using Template and Notepad,

######################################## Syntax Coloring Map For ArduinoLed#######################################

######################################## Datatypes (KEYWORD1)#######################################

ArduinoLed KEYWORD1

######################################## Methods and Functions (KEYWORD2)#######################################

ledOn KEYWORD2

ledOff KEYWORD3

######################################## Instances (KEYWORD4)#######################################

######################################## Constants (LITERAL1)#######################################

ArduinoLed KEYWORD1

ledOn KEYWORD2

ledOff KEYWORD3

Page 30: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

How to write, distribute and install YOUR class

30

Write/Edit file “readme.txt” By using “template” and Notepad,

This is an examplary ArduinoLed C++ library for Arduino 0004+, based on the Test created by Nicholas Zambetti for Wiring 0006+with relevant modifications made by Samuel Kosolapov

Installation--------------------------------------------------------------------------------

To install this library, just place this entire folder as a subfolder in yourArduino/lib/targets/libraries folder.

When installed, this library should look like:

Arduino/lib/targets/libraries/ArduinoLed(this library's folder)

Arduino/lib/targets/libraries/ArduinoLed/ArduinoLed.cpp (the library implementation file)

Arduino/lib/targets/libraries/ArduinoLed/ArduinoLed.h(the library description file)

Arduino/lib/targets/libraries/ArduinoLed/keywords.txt (the syntax coloring file)

Arduino/lib/targets/libraries/ArduinoLed/examples (the examples in the "open" menu)

Arduino/lib/targets/libraries/ArduinoLed/readme.txt(this file)

Building--------------------------------------------------------------------------------

After this library is installed, you just have to start the Arduino application.You may see a few warning messages as it's built.

To use this library in a sketch, go to the Sketch | Import Library menu and select ArduinoLed. This will add a corresponding line to the top of your sketch:#include <ArduinoLed.h>

To stop using this library, delete that line from your sketch.

Page 31: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

How to write, distribute and install YOUR class

31

Create subdirectory “examples”

#include <ArduinoLed.h>//OOP BlinkingLed// Created by Samuel Kosolapov by using: // Doing Something// by John Doe <http://www.yourwebsite.com>// Demostrates how to BlinkLed by using ArduinoLed library// Modified 2015-10-26 by Samuel Kosolapov

ArduinoLed myLed = ArduinoLed();void setup(){}void loop(){myLed.ledOn();delay(500);myLed.ledOff();delay(1000);

}.

Then create subdirectory named, say,“BlinkingLedwithOOP”

Write/edit sketch “BlinkingLedwithOOP.pde”

Page 32: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

How to write, distribute and install YOUR class

32

Copy all the staff to C:\Users\John\Documents\Arduino\libraries\ArduinoLed

User Name

User Name

Page 33: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

How to write, distribute and install YOUR class

33

Open Arduino IDE

Write code of Example1.ino

Add YOUR Library:Sketch | Include Library

select ArduinoLed

Arduino IDE automatically “see” your new library in the

list of available libraries

Sketch | Verify/Compile

In case of errors red line will appear.

Sketch | UploadWhen happy, ZIP all the ArduinoLed directoryAnd distribute ArduinoLed.zip by any available way

Page 34: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

Disclaimer (again, to be clear)

34

OOP and C++ are not part of the Syllabus of Electronic Instrumentation.

Students will not be REQUESTED to create OOP code on exam.

BUT, because all modern software uses OOP, students will be asked to understandwhat OOP code do

in the Arduino sketchesand in Windows Form programs

presented in the lectures and examples

Additionally, lecturer (naively) expects that students will try to understandwhy OOP is so widely used in modern projects

and some day will use OOP in their’ real projects

Actually, non OOP option does not exist many years except special cases …

Page 35: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

Important class (library) : Serial

35

When Arduino board is connected to PC, it is possible to send messages from Arduino Board and back by using USB cable.• Actually not USB, but RS232 protocol is used.

Computer “sees” Arduino board by using Virtual COM port

Special Tools can be used to manage text messages: to see messages arrived from Arduino boardand to send messages back to Arduino board

Tools | Serial Monitor

Considering that serial communication is governed by standards, a lot of other options can be used to manage communication. For example: Create Windows Form Application by using .NET (OOP) library, Specifically COM port class

Page 36: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

Important class (library) : Serial

36

Serial communication (UARTY, RS232) is very complex with a lot of small detailscritical for real life applications.

OOP implementation enables to make default communication simple, while enabling more sophisticated options for advanced users.

Simplest example:

void setup(){Serial.begin(9600);

}

void loop(){Serial.write(45); // send a byte with the value 45

int bytesSent = Serial.write(“hello”); //send the string “hello” and return the length of the string.}

Explain (again) “.” syntax

Explain: functions with the same names but with different set of arguments

Most Protocol details are

hidden

Page 37: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

Important class (library) : Serial

37

Explain: functions with the same names but with different set of arguments

Serial.write(val)Serial.write(str)Serial.write(buf, len)

Parametersval: a value to send as a single bytestr: a string to send as a series of bytes (last byte of the string is 0 - not ‘0’ !!!)buf: an array to send as a series of byteslen: the length of the buffer

Returnsbytewrite() will return the number of bytes written,

though reading that number is optionalint bytesSent = Serial.write(“hello”); or Serial.write(“hello”);

Page 38: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

Important class (library) : Serial

38

Class Serial has a big numberof additional functions (Methods):

Again,with OOP it is easy to make

“common” tasks,But practically no limitations For sophisticated real-time tasks

Page 39: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

Important class (library) : Serial

39

Method “begin” has a number of functions with different set of arguments:

Serial.begin(speed)Serial.begin(speed, config)

In most cases Serial.begin( 9600) is used, however other rates can be used:300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200 baud (bits per second

An optional second argument configures the data, parity, and stop bits. The default is 8 data bits, no parity, one stop bit: SERIAL_8N1

Important: “second device” (for example PC) must use the same configuration

Page 40: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

Important class (library) : Serial

40

Hence, in order to save valuable time, we will use Serial library to see voltage on the sensors connected to the Analog pins

on the PC screen(instead of entering complex project dealing with LED or TFT small-size screens)

+ state of digital pins on the PC screen

AND

We will send commands to Arduino by using PC keyboard and mouse(instead of entering complex project dealing with user non-friendly buttons)

Goal of this course is to get experience with sensors and actuators

Page 41: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

Important class (library) : Serial. Usage with Bluetooth

41

JY-MCU class-2 Bluetooth modulethat acts like a wireless serial port

The messages from/to Arduino can be send wirelesslyTo/From ANY device supporting Bluetooth communication(for example, Android smartphone)

No changes in Ardiono code are neededNo software configuration needed

on the Arduino, Windows10, or other devicesEXCEPT standard pairing procedure

Now Arduino can be controlled from (say) android Phone.Controlled:

Accept information from Android SmartphoneSend Information to Android Phone

Detailed Example: Later

Page 42: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

Short List of Standard Libraries currently available from Arduino IDE

42

MENU | Sketch | Include Library

All libraries are written as CLASSes by using OOP. Think why

EEPROM - reading and writing to "permanent" storageEthernet - for connecting to the internet using the Arduino Ethernet ShieldGSM - for connecting to a GSM/GRPS network with the GSM shield.LiquidCrystal - for controlling liquid crystal displays (LCDs)SD - for reading and writing SD cardsServo - for controlling servo motorsSoftwareSerial - for serial communication on any digital pins. Stepper - for controlling stepper motorsTFT - for drawing text , images, and shapes on the Arduino TFT screenWiFi - for connecting to the internet using the Arduino WiFi shieldWire - Two Wire Interface (TWI/I2C) for sending and receiving data over a net of devices or sensors.

Page 43: from C to C++ Arduino with OOP (Object Oriented …brd4.ort.org.il/~ksamuel/ElIn.31361/Exercises/E013...from C to C++ Arduino with OOP (Object Oriented Programming) Lecturer: Dr. Samuel

Short List of Third Party (Contributed) Libraries that can be used with Arduino

43

All libraries are written as CLASSes by using OOP. Think why

OneWire - control devices (from Dallas Semiconductor) that use the One Wire protocol.PS2Keyboard - read characters from a PS2 keyboard.

Capacitive Sensing - turn two or more pins into capacitive sensorsDebounce - for reading noisy digital inputs (e.g. from buttons)

FFT - frequency analysis of audio or other analog signalsTone - generate audio frequency square waves in the background on any microcontroller pin

EE Engineer may not be requested to write “class” code,but it must UNDERSTAND OOP syntax and usage