SlideShare a Scribd company logo
1 of 19
ARDUINO
OBJECT
ORIENTED
PROGRAMMING
(OOP)
introduction to
Arduino
Object
Oriented
Programming
programming using C++ and OOP.
example how to re-write Arduino using
the OOP way.
you’ll get the complete code so you can
directly test on your own Arduino board.
Alright, let’s get started!
Table of
Contents
• Arduino Object Oriented Programming limitations
• Hardware setup
• A class for a LED
• Class for a Button
• Complete Arduino Object Oriented code
• Reorganize your Arduino OOP code
• Led.h
• Led.cpp
• Button.h
• Button.cpp
• Main
• Arduino Object Oriented: it’s already everywhere
Arduino Object Oriented
Programming limitations
• Even if Oriented Object Programming is
possible with Arduino, you have to know the
limitations.
• Basically, the Arduino language is a subset of
C/C++. You can create classes, use
inheritance, composition, and many other nice
OOP functionalities, but:
• The STL library is not available (not
natively, you can still use an external
library to get most of the functionalities).
• You can’t use exceptions.
Hardware
simulator setup
• Note: it’s OK if you don’t have any Arduino or
hardware component available. You can still follow this
tutorial and get all the knowledge you need.
• We’ll be using the following hardware setup:
• Component list:
• 1* Arduino Uno board (Any Arduino board will do)
• 1* breadboard
• 4* LEDs (any color you want)
• 4* 220 Ohm resistors (one for each LED)
• 1* push button
• 1* 10k Ohm resistor (for the push button)
• A few male-to-male wires
• The goal of the tutorial will be to turn on LED 1 & 3,
and turn off LED 2 & 4 when the button is pressed.
When the button is not pressed we’ll do the opposite –
turn off LED 1 & 3, and turn on LED 2 & 4.
• Now let’s go step by step to reach this goal.
CLASSES AND
OBJECTS
• A class is a collection of functions and
variables that can be used to perform specific
programming tasks. In Arduino programming, a
class is basically the same thing as a library.
• A class is like a template that lets you reuse
code without having to type it over and over.
For example, say you needed to make 20
documents that all had the same font, header,
and margins. It would be too time consuming to
change the formatting of all 20 documents
individually. Instead you could make a template
document with the right font, header, and
margins, then add the text and save it as a
separate file.
• Classes are like that template document.
Classes are templates of code for what are
called “objects”. Think of a class as the
template document, and the object as the
individual document. Objects let you access
the functions and variables inside of a class.
An object is sometimes called an “instance” of
a class.
A class for a
LED
• An LED is a very basic component. You
setup a digital pin to INPUT mode, and
then you just need to set its state to
HIGH or LOW.
• Let’s create a simple class to wrap the
LED functionalities.
You can now use
this class to create
a Led object.
• With the Led class we can hide all the
Arduino stuff about digital pins. Note
that the object must be created in the
global scope if you want to be able to
use it in the setup() and loop()
functions.
Class for a
Button
• Let’s now write some OOP code for a
push button! The button is a little bit
more complex, because we need to
add a debounce functionality if we want
it to remove the mechanical noise.
• Here again, all the complexity is
hidden. Once you’ve implemented the
debounce functionality for the button
inside the class, you don’t need to think
about it anymore.
create a
Button object
• In your program, you just need to
create a Button object and check
whether it’s pressed or not.
• When the button is pressed, we turn on
LED 1. As you can see the code in the
loop() is quite small and clean.
Complete
Arduino
Object
Oriented
code
• OOP is great for reusability. Do
you remember we added 4 LEDs
at the beginning of the tutorial?
Well, now that we have a class
for a LED, we just need to create
additional objects, all the
implementation is already done.
• Here is the complete code,
including the Led class, the
Button class, and the main code
of the program:
• https://wokwi.com/projects/34921
7893036065362
Reorganize
your
Arduino
OOP code
• The previous code works well, but
everything is in the same file. We
want to use OOP for reusability,
modularity, readability, etc, but it’s
impossible if we write all the code in
one file. As your program grows in
complexity, so your code length,
until you reach a point where the
code is so long that you spend more
time finding stuff and fixing bugs
instead of adding new
functionalities.
• We’ll separate the code into 3 parts:
the Led class, the Button class, and
the “main”.
Each class
will be on its
own
independent
file.
• in fact, for one class we’ll have 2 files: one Constructor
Cpp file (.cpp) and one header file (.h).
• Your code will become much more readable. The classes
you create will be also more reusable as you can include
them in every file where you need them.
• Note that creating other files for an Arduino program is
quite tricky. You must create all your files inside your Arduino
program folder. Example: if your program is named Test.ino,
then it will be automatically saved on a Test/ folder (the
Arduino IDE does that). You’ll have to put all your files in the
Test/folder as well, so the Arduino IDE can find them.
• Go into the folder of your current Arduino program. Create
4 files:
• Led.h
• Led.cpp
• Button.h
• Button.cpp
• The files won’t appear in the Arduino IDE right away. You’ll
need to close the project, and open it again so the files will
show up in tabs on the top.
Led.h
• In this file we just write the class declaration, which is
the interface that any client using this class will use.
This makes the class much easier to understand and
use.
• The header guards (first 2 lines, and last line) will
make sure that the Led class will not be included more
than once, if for example you have multiple #include
"Led.h" in other parts of your program.
• Also note that I’ve added #include <Arduino.h> at the
beginning. Why? This include is necessary to use the
specific Arduino functions and types (think of
pinMode(), digitalWrite(), byte). In the “main” file we
don’t need to write it because it’s automatically added
when you compile your code. But in any other file, you
need to add it by yourself.
Led.cpp
Button.h
• Same warning as for the Led.h file.
Don’t forget to include the Arduino
library at the beginning of the file. Also
put all your class declaration inside
header guards so you won’t include it
twice in other parts of your code.
Button.cpp
Main
• Well, as you can see, the code is now much
clearer and readable. Plus, you could import
the Led and Button class in any other part of
your application.
• For example you could create a class named
LedPanel. This class would contain an array
of Led objects and handle them. In your
“main”, you could just import the LedPanel
header file without having to know about the
Led class.
• You can always create more modules on top
of other modules. That’s the power of Object
Oriented Programming with Arduino (and not
only Arduino by the way).
Arduino
Object
Oriented: it’s
already
everywhere
• If it’s the first time you use Object Oriented
Programming with Arduino, well… Don’t think
you’re doing something new! In fact, many of the
Arduino already use OOP.
• A few OOP library examples:
• Servo: control a servo motor
• Serial: communicate between your Arduino board and
other devices
• Wire: communicate though the I2C protocol
• SevSeg: control a digit 7-segment display
• EEPROM: store values permanently in your Arduino board
• If you’re already familiar with OOP and want to
use it in your Arduino programs, don’t hesitate
and do it! OOP is certainly not the answer to
everything, but if you know how to use it right,
there is no reason you couldn’t get its benefits
with Arduino.

More Related Content

What's hot

Makalah Arduino
Makalah ArduinoMakalah Arduino
Makalah ArduinoWahyus31
 
RL - Daya Rangkaian Tiga Fasa
RL - Daya Rangkaian Tiga FasaRL - Daya Rangkaian Tiga Fasa
RL - Daya Rangkaian Tiga FasaMuhammad Dany
 
Thyristor
ThyristorThyristor
Thyristorrisal07
 
Transmisi Daya Listrik
Transmisi Daya ListrikTransmisi Daya Listrik
Transmisi Daya ListrikMulia Damanik
 
Modul pelatihan praktikum mikrokontroler dengan software proteus
Modul pelatihan praktikum mikrokontroler dengan software proteusModul pelatihan praktikum mikrokontroler dengan software proteus
Modul pelatihan praktikum mikrokontroler dengan software proteusKukuh Adhi Rumekso
 
Modul teknik pemrograman mikrokontroler dan mikroprosesor
Modul teknik pemrograman mikrokontroler dan mikroprosesorModul teknik pemrograman mikrokontroler dan mikroprosesor
Modul teknik pemrograman mikrokontroler dan mikroprosesorBeny Abd
 
Pertemuan 1 Sistem Pengendali Elektronik
Pertemuan 1   Sistem Pengendali ElektronikPertemuan 1   Sistem Pengendali Elektronik
Pertemuan 1 Sistem Pengendali ElektronikAhmad Nawawi, S.Kom
 
Arduino coding.ppt
Arduino coding.pptArduino coding.ppt
Arduino coding.pptdidikmaarif
 
Laporan 5 gelombang filter c
Laporan 5 gelombang filter cLaporan 5 gelombang filter c
Laporan 5 gelombang filter cRidwan Satria
 
SCR, UJT, TRIAC, DIAC
SCR, UJT, TRIAC, DIACSCR, UJT, TRIAC, DIAC
SCR, UJT, TRIAC, DIACGhins GO
 
Laporan pengukuran tahanan tanah
Laporan pengukuran tahanan tanahLaporan pengukuran tahanan tanah
Laporan pengukuran tahanan tanahmahfudi55
 
Perbedaan mikroprosesor & mikrokontroler
Perbedaan mikroprosesor & mikrokontrolerPerbedaan mikroprosesor & mikrokontroler
Perbedaan mikroprosesor & mikrokontrolerM Cahyo Ardi Prabowo
 
Laporan Praktikum Gerbang logika
Laporan Praktikum Gerbang logikaLaporan Praktikum Gerbang logika
Laporan Praktikum Gerbang logikaFebriTiaAldila
 
Sistem Kontrol (Distributed Control System dan Programable Logic Controller)
Sistem Kontrol (Distributed Control System dan Programable Logic Controller)Sistem Kontrol (Distributed Control System dan Programable Logic Controller)
Sistem Kontrol (Distributed Control System dan Programable Logic Controller)University Of Polytechnic Malang
 
Pengetahuan Dasar Motor Listrik ( Motor AC 1 Fasa , Motor AC 3 Fasa , Motor D...
Pengetahuan Dasar Motor Listrik ( Motor AC 1 Fasa , Motor AC 3 Fasa , Motor D...Pengetahuan Dasar Motor Listrik ( Motor AC 1 Fasa , Motor AC 3 Fasa , Motor D...
Pengetahuan Dasar Motor Listrik ( Motor AC 1 Fasa , Motor AC 3 Fasa , Motor D...Andrean Yogatama
 
Laporan 2 penyearah gelombang penuh dengan beban tahanan murni
Laporan 2 penyearah gelombang penuh dengan beban tahanan murniLaporan 2 penyearah gelombang penuh dengan beban tahanan murni
Laporan 2 penyearah gelombang penuh dengan beban tahanan murniridwan35
 

What's hot (20)

Makalah Arduino
Makalah ArduinoMakalah Arduino
Makalah Arduino
 
RL - Daya Rangkaian Tiga Fasa
RL - Daya Rangkaian Tiga FasaRL - Daya Rangkaian Tiga Fasa
RL - Daya Rangkaian Tiga Fasa
 
Sistem Kendali.ppt
Sistem Kendali.pptSistem Kendali.ppt
Sistem Kendali.ppt
 
Dasar sistem kontrol
Dasar sistem kontrolDasar sistem kontrol
Dasar sistem kontrol
 
Thyristor
ThyristorThyristor
Thyristor
 
Transmisi Daya Listrik
Transmisi Daya ListrikTransmisi Daya Listrik
Transmisi Daya Listrik
 
Modul pelatihan praktikum mikrokontroler dengan software proteus
Modul pelatihan praktikum mikrokontroler dengan software proteusModul pelatihan praktikum mikrokontroler dengan software proteus
Modul pelatihan praktikum mikrokontroler dengan software proteus
 
Modul teknik pemrograman mikrokontroler dan mikroprosesor
Modul teknik pemrograman mikrokontroler dan mikroprosesorModul teknik pemrograman mikrokontroler dan mikroprosesor
Modul teknik pemrograman mikrokontroler dan mikroprosesor
 
Pertemuan 1 Sistem Pengendali Elektronik
Pertemuan 1   Sistem Pengendali ElektronikPertemuan 1   Sistem Pengendali Elektronik
Pertemuan 1 Sistem Pengendali Elektronik
 
Arduino coding.ppt
Arduino coding.pptArduino coding.ppt
Arduino coding.ppt
 
Laporan 5 gelombang filter c
Laporan 5 gelombang filter cLaporan 5 gelombang filter c
Laporan 5 gelombang filter c
 
SCR, UJT, TRIAC, DIAC
SCR, UJT, TRIAC, DIACSCR, UJT, TRIAC, DIAC
SCR, UJT, TRIAC, DIAC
 
1 karakteristik sensor
1 karakteristik sensor1 karakteristik sensor
1 karakteristik sensor
 
Laporan pengukuran tahanan tanah
Laporan pengukuran tahanan tanahLaporan pengukuran tahanan tanah
Laporan pengukuran tahanan tanah
 
Perbedaan mikroprosesor & mikrokontroler
Perbedaan mikroprosesor & mikrokontrolerPerbedaan mikroprosesor & mikrokontroler
Perbedaan mikroprosesor & mikrokontroler
 
Laporan Praktikum Gerbang logika
Laporan Praktikum Gerbang logikaLaporan Praktikum Gerbang logika
Laporan Praktikum Gerbang logika
 
Sistem Kontrol (Distributed Control System dan Programable Logic Controller)
Sistem Kontrol (Distributed Control System dan Programable Logic Controller)Sistem Kontrol (Distributed Control System dan Programable Logic Controller)
Sistem Kontrol (Distributed Control System dan Programable Logic Controller)
 
Pengetahuan Dasar Motor Listrik ( Motor AC 1 Fasa , Motor AC 3 Fasa , Motor D...
Pengetahuan Dasar Motor Listrik ( Motor AC 1 Fasa , Motor AC 3 Fasa , Motor D...Pengetahuan Dasar Motor Listrik ( Motor AC 1 Fasa , Motor AC 3 Fasa , Motor D...
Pengetahuan Dasar Motor Listrik ( Motor AC 1 Fasa , Motor AC 3 Fasa , Motor D...
 
GAS INSULATED SUBSTATION (GIS)
GAS INSULATED SUBSTATION (GIS)GAS INSULATED SUBSTATION (GIS)
GAS INSULATED SUBSTATION (GIS)
 
Laporan 2 penyearah gelombang penuh dengan beban tahanan murni
Laporan 2 penyearah gelombang penuh dengan beban tahanan murniLaporan 2 penyearah gelombang penuh dengan beban tahanan murni
Laporan 2 penyearah gelombang penuh dengan beban tahanan murni
 

Similar to Arduino Object Oriented Programming (OOP).pptx

Does The Delphi IDE Narrow You? Extend It! - ITDevConX European Delphi Confer...
Does The Delphi IDE Narrow You? Extend It! - ITDevConX European Delphi Confer...Does The Delphi IDE Narrow You? Extend It! - ITDevConX European Delphi Confer...
Does The Delphi IDE Narrow You? Extend It! - ITDevConX European Delphi Confer...Marco Breveglieri
 
Ios-training-institute-in-mumbai
Ios-training-institute-in-mumbaiIos-training-institute-in-mumbai
Ios-training-institute-in-mumbaivibrantuser
 
Ios-training-institute-in-mumbai
Ios-training-institute-in-mumbaiIos-training-institute-in-mumbai
Ios-training-institute-in-mumbaivibrantuser
 
C++ Introduction brown bag
C++ Introduction brown bagC++ Introduction brown bag
C++ Introduction brown bagJacob Green
 
PyCourse - Self driving python course
PyCourse - Self driving python coursePyCourse - Self driving python course
PyCourse - Self driving python courseEran Shlomo
 
Python_Introduction&DataType.pptx
Python_Introduction&DataType.pptxPython_Introduction&DataType.pptx
Python_Introduction&DataType.pptxHaythamBarakeh1
 
Introduction to the intermediate Python - v1.1
Introduction to the intermediate Python - v1.1Introduction to the intermediate Python - v1.1
Introduction to the intermediate Python - v1.1Andrei KUCHARAVY
 
Python Programming for ArcGIS: Part I
Python Programming for ArcGIS: Part IPython Programming for ArcGIS: Part I
Python Programming for ArcGIS: Part IDUSPviz
 
Adagio: Agile and Distributed Authoring of Generic Learning Objects
Adagio: Agile and Distributed Authoring of Generic Learning ObjectsAdagio: Agile and Distributed Authoring of Generic Learning Objects
Adagio: Agile and Distributed Authoring of Generic Learning ObjectsAbelardo Pardo
 
Xcode, Basics and Beyond
Xcode, Basics and BeyondXcode, Basics and Beyond
Xcode, Basics and Beyondrsebbe
 
NDK Programming in Android
NDK Programming in AndroidNDK Programming in Android
NDK Programming in AndroidArvind Devaraj
 
Python Programming1.ppt
Python Programming1.pptPython Programming1.ppt
Python Programming1.pptRehnawilson1
 
Ios training-cum-course-in-mumbai-
Ios training-cum-course-in-mumbai-Ios training-cum-course-in-mumbai-
Ios training-cum-course-in-mumbai-vibrantuser
 
EMPEX LA 2018 - Inclusion Starts with Docs
EMPEX LA 2018 - Inclusion Starts with DocsEMPEX LA 2018 - Inclusion Starts with Docs
EMPEX LA 2018 - Inclusion Starts with DocsPete Gamache
 
ppt notes for python language variable data types
ppt notes for python language variable data typesppt notes for python language variable data types
ppt notes for python language variable data typesSukhpreetSingh519414
 
Csharp introduction
Csharp introductionCsharp introduction
Csharp introductionSireesh K
 
Half-automatic Compilable Source Code Recovery
Half-automatic Compilable Source Code RecoveryHalf-automatic Compilable Source Code Recovery
Half-automatic Compilable Source Code RecoveryJoxean Koret
 

Similar to Arduino Object Oriented Programming (OOP).pptx (20)

Does The Delphi IDE Narrow You? Extend It! - ITDevConX European Delphi Confer...
Does The Delphi IDE Narrow You? Extend It! - ITDevConX European Delphi Confer...Does The Delphi IDE Narrow You? Extend It! - ITDevConX European Delphi Confer...
Does The Delphi IDE Narrow You? Extend It! - ITDevConX European Delphi Confer...
 
Ios-training-institute-in-mumbai
Ios-training-institute-in-mumbaiIos-training-institute-in-mumbai
Ios-training-institute-in-mumbai
 
Ios-training-institute-in-mumbai
Ios-training-institute-in-mumbaiIos-training-institute-in-mumbai
Ios-training-institute-in-mumbai
 
C++ Introduction brown bag
C++ Introduction brown bagC++ Introduction brown bag
C++ Introduction brown bag
 
PyCourse - Self driving python course
PyCourse - Self driving python coursePyCourse - Self driving python course
PyCourse - Self driving python course
 
Python_Introduction&DataType.pptx
Python_Introduction&DataType.pptxPython_Introduction&DataType.pptx
Python_Introduction&DataType.pptx
 
Ios - Intorduction to view controller
Ios - Intorduction to view controllerIos - Intorduction to view controller
Ios - Intorduction to view controller
 
Introduction to the intermediate Python - v1.1
Introduction to the intermediate Python - v1.1Introduction to the intermediate Python - v1.1
Introduction to the intermediate Python - v1.1
 
Python Programming for ArcGIS: Part I
Python Programming for ArcGIS: Part IPython Programming for ArcGIS: Part I
Python Programming for ArcGIS: Part I
 
Python Tutorial Part 2
Python Tutorial Part 2Python Tutorial Part 2
Python Tutorial Part 2
 
Adagio: Agile and Distributed Authoring of Generic Learning Objects
Adagio: Agile and Distributed Authoring of Generic Learning ObjectsAdagio: Agile and Distributed Authoring of Generic Learning Objects
Adagio: Agile and Distributed Authoring of Generic Learning Objects
 
Xcode, Basics and Beyond
Xcode, Basics and BeyondXcode, Basics and Beyond
Xcode, Basics and Beyond
 
External Editors for Arduino
External Editors for ArduinoExternal Editors for Arduino
External Editors for Arduino
 
NDK Programming in Android
NDK Programming in AndroidNDK Programming in Android
NDK Programming in Android
 
Python Programming1.ppt
Python Programming1.pptPython Programming1.ppt
Python Programming1.ppt
 
Ios training-cum-course-in-mumbai-
Ios training-cum-course-in-mumbai-Ios training-cum-course-in-mumbai-
Ios training-cum-course-in-mumbai-
 
EMPEX LA 2018 - Inclusion Starts with Docs
EMPEX LA 2018 - Inclusion Starts with DocsEMPEX LA 2018 - Inclusion Starts with Docs
EMPEX LA 2018 - Inclusion Starts with Docs
 
ppt notes for python language variable data types
ppt notes for python language variable data typesppt notes for python language variable data types
ppt notes for python language variable data types
 
Csharp introduction
Csharp introductionCsharp introduction
Csharp introduction
 
Half-automatic Compilable Source Code Recovery
Half-automatic Compilable Source Code RecoveryHalf-automatic Compilable Source Code Recovery
Half-automatic Compilable Source Code Recovery
 

Recently uploaded

怎样办理昆士兰大学毕业证(UQ毕业证书)成绩单留信认证
怎样办理昆士兰大学毕业证(UQ毕业证书)成绩单留信认证怎样办理昆士兰大学毕业证(UQ毕业证书)成绩单留信认证
怎样办理昆士兰大学毕业证(UQ毕业证书)成绩单留信认证ehyxf
 
CRISIS COMMUNICATION presentation=-Rishabh(11195)-group ppt (4).pptx
CRISIS COMMUNICATION presentation=-Rishabh(11195)-group ppt (4).pptxCRISIS COMMUNICATION presentation=-Rishabh(11195)-group ppt (4).pptx
CRISIS COMMUNICATION presentation=-Rishabh(11195)-group ppt (4).pptxRishabh332761
 
一比一原版(USYD毕业证书)澳洲悉尼大学毕业证如何办理
一比一原版(USYD毕业证书)澳洲悉尼大学毕业证如何办理一比一原版(USYD毕业证书)澳洲悉尼大学毕业证如何办理
一比一原版(USYD毕业证书)澳洲悉尼大学毕业证如何办理uodye
 
Top profile Call Girls In Ratlam [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Ratlam [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Ratlam [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Ratlam [ 7014168258 ] Call Me For Genuine Models We...nirzagarg
 
一比一维多利亚大学毕业证(victoria毕业证)成绩单学位证如何办理
一比一维多利亚大学毕业证(victoria毕业证)成绩单学位证如何办理一比一维多利亚大学毕业证(victoria毕业证)成绩单学位证如何办理
一比一维多利亚大学毕业证(victoria毕业证)成绩单学位证如何办理uodye
 
怎样办理斯威本科技大学毕业证(SUT毕业证书)成绩单留信认证
怎样办理斯威本科技大学毕业证(SUT毕业证书)成绩单留信认证怎样办理斯威本科技大学毕业证(SUT毕业证书)成绩单留信认证
怎样办理斯威本科技大学毕业证(SUT毕业证书)成绩单留信认证tufbav
 
Guwahati Escorts Service Girl ^ 9332606886, WhatsApp Anytime Guwahati
Guwahati Escorts Service Girl ^ 9332606886, WhatsApp Anytime GuwahatiGuwahati Escorts Service Girl ^ 9332606886, WhatsApp Anytime Guwahati
Guwahati Escorts Service Girl ^ 9332606886, WhatsApp Anytime Guwahatimeghakumariji156
 
Mass storage systems presentation operating systems
Mass storage systems presentation operating systemsMass storage systems presentation operating systems
Mass storage systems presentation operating systemsnight1ng4ale
 
🌹Patia⬅️ Vip Call Girls Bhubaneswar 📱9777949614 Book Well Trand Call Girls In...
🌹Patia⬅️ Vip Call Girls Bhubaneswar 📱9777949614 Book Well Trand Call Girls In...🌹Patia⬅️ Vip Call Girls Bhubaneswar 📱9777949614 Book Well Trand Call Girls In...
🌹Patia⬅️ Vip Call Girls Bhubaneswar 📱9777949614 Book Well Trand Call Girls In...Call Girls Mumbai
 
Abortion pills in Jeddah +966572737505 <> buy cytotec <> unwanted kit Saudi A...
Abortion pills in Jeddah +966572737505 <> buy cytotec <> unwanted kit Saudi A...Abortion pills in Jeddah +966572737505 <> buy cytotec <> unwanted kit Saudi A...
Abortion pills in Jeddah +966572737505 <> buy cytotec <> unwanted kit Saudi A...samsungultra782445
 
Top profile Call Girls In Udgir [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Udgir [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Udgir [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Udgir [ 7014168258 ] Call Me For Genuine Models We ...gajnagarg
 
Abort pregnancy in research centre+966_505195917 abortion pills in Kuwait cyt...
Abort pregnancy in research centre+966_505195917 abortion pills in Kuwait cyt...Abort pregnancy in research centre+966_505195917 abortion pills in Kuwait cyt...
Abort pregnancy in research centre+966_505195917 abortion pills in Kuwait cyt...drmarathore
 
怎样办理维多利亚大学毕业证(UVic毕业证书)成绩单留信认证
怎样办理维多利亚大学毕业证(UVic毕业证书)成绩单留信认证怎样办理维多利亚大学毕业证(UVic毕业证书)成绩单留信认证
怎样办理维多利亚大学毕业证(UVic毕业证书)成绩单留信认证tufbav
 
Call Girls Amethi 9332606886 HOT & SEXY Models beautiful and charming call g...
Call Girls Amethi  9332606886 HOT & SEXY Models beautiful and charming call g...Call Girls Amethi  9332606886 HOT & SEXY Models beautiful and charming call g...
Call Girls Amethi 9332606886 HOT & SEXY Models beautiful and charming call g...Sareena Khatun
 
Top profile Call Girls In Palghar [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In Palghar [ 7014168258 ] Call Me For Genuine Models W...Top profile Call Girls In Palghar [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In Palghar [ 7014168258 ] Call Me For Genuine Models W...gajnagarg
 
一比一原版(Otago毕业证书)奥塔哥理工学院毕业证成绩单学位证靠谱定制
一比一原版(Otago毕业证书)奥塔哥理工学院毕业证成绩单学位证靠谱定制一比一原版(Otago毕业证书)奥塔哥理工学院毕业证成绩单学位证靠谱定制
一比一原版(Otago毕业证书)奥塔哥理工学院毕业证成绩单学位证靠谱定制uodye
 
一比一定(购)坎特伯雷大学毕业证(UC毕业证)成绩单学位证
一比一定(购)坎特伯雷大学毕业证(UC毕业证)成绩单学位证一比一定(购)坎特伯雷大学毕业证(UC毕业证)成绩单学位证
一比一定(购)坎特伯雷大学毕业证(UC毕业证)成绩单学位证wpkuukw
 
一比一原版(CSUEB毕业证书)东湾分校毕业证原件一模一样
一比一原版(CSUEB毕业证书)东湾分校毕业证原件一模一样一比一原版(CSUEB毕业证书)东湾分校毕业证原件一模一样
一比一原版(CSUEB毕业证书)东湾分校毕业证原件一模一样ayoqf
 

Recently uploaded (20)

怎样办理昆士兰大学毕业证(UQ毕业证书)成绩单留信认证
怎样办理昆士兰大学毕业证(UQ毕业证书)成绩单留信认证怎样办理昆士兰大学毕业证(UQ毕业证书)成绩单留信认证
怎样办理昆士兰大学毕业证(UQ毕业证书)成绩单留信认证
 
CRISIS COMMUNICATION presentation=-Rishabh(11195)-group ppt (4).pptx
CRISIS COMMUNICATION presentation=-Rishabh(11195)-group ppt (4).pptxCRISIS COMMUNICATION presentation=-Rishabh(11195)-group ppt (4).pptx
CRISIS COMMUNICATION presentation=-Rishabh(11195)-group ppt (4).pptx
 
一比一原版(USYD毕业证书)澳洲悉尼大学毕业证如何办理
一比一原版(USYD毕业证书)澳洲悉尼大学毕业证如何办理一比一原版(USYD毕业证书)澳洲悉尼大学毕业证如何办理
一比一原版(USYD毕业证书)澳洲悉尼大学毕业证如何办理
 
Top profile Call Girls In Ratlam [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Ratlam [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Ratlam [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Ratlam [ 7014168258 ] Call Me For Genuine Models We...
 
一比一维多利亚大学毕业证(victoria毕业证)成绩单学位证如何办理
一比一维多利亚大学毕业证(victoria毕业证)成绩单学位证如何办理一比一维多利亚大学毕业证(victoria毕业证)成绩单学位证如何办理
一比一维多利亚大学毕业证(victoria毕业证)成绩单学位证如何办理
 
Abortion pills in Dammam +966572737505 Buy Cytotec
Abortion pills in Dammam +966572737505 Buy CytotecAbortion pills in Dammam +966572737505 Buy Cytotec
Abortion pills in Dammam +966572737505 Buy Cytotec
 
怎样办理斯威本科技大学毕业证(SUT毕业证书)成绩单留信认证
怎样办理斯威本科技大学毕业证(SUT毕业证书)成绩单留信认证怎样办理斯威本科技大学毕业证(SUT毕业证书)成绩单留信认证
怎样办理斯威本科技大学毕业证(SUT毕业证书)成绩单留信认证
 
Guwahati Escorts Service Girl ^ 9332606886, WhatsApp Anytime Guwahati
Guwahati Escorts Service Girl ^ 9332606886, WhatsApp Anytime GuwahatiGuwahati Escorts Service Girl ^ 9332606886, WhatsApp Anytime Guwahati
Guwahati Escorts Service Girl ^ 9332606886, WhatsApp Anytime Guwahati
 
Mass storage systems presentation operating systems
Mass storage systems presentation operating systemsMass storage systems presentation operating systems
Mass storage systems presentation operating systems
 
🌹Patia⬅️ Vip Call Girls Bhubaneswar 📱9777949614 Book Well Trand Call Girls In...
🌹Patia⬅️ Vip Call Girls Bhubaneswar 📱9777949614 Book Well Trand Call Girls In...🌹Patia⬅️ Vip Call Girls Bhubaneswar 📱9777949614 Book Well Trand Call Girls In...
🌹Patia⬅️ Vip Call Girls Bhubaneswar 📱9777949614 Book Well Trand Call Girls In...
 
Abortion pills in Jeddah +966572737505 <> buy cytotec <> unwanted kit Saudi A...
Abortion pills in Jeddah +966572737505 <> buy cytotec <> unwanted kit Saudi A...Abortion pills in Jeddah +966572737505 <> buy cytotec <> unwanted kit Saudi A...
Abortion pills in Jeddah +966572737505 <> buy cytotec <> unwanted kit Saudi A...
 
Top profile Call Girls In Udgir [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Udgir [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Udgir [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Udgir [ 7014168258 ] Call Me For Genuine Models We ...
 
Abort pregnancy in research centre+966_505195917 abortion pills in Kuwait cyt...
Abort pregnancy in research centre+966_505195917 abortion pills in Kuwait cyt...Abort pregnancy in research centre+966_505195917 abortion pills in Kuwait cyt...
Abort pregnancy in research centre+966_505195917 abortion pills in Kuwait cyt...
 
怎样办理维多利亚大学毕业证(UVic毕业证书)成绩单留信认证
怎样办理维多利亚大学毕业证(UVic毕业证书)成绩单留信认证怎样办理维多利亚大学毕业证(UVic毕业证书)成绩单留信认证
怎样办理维多利亚大学毕业证(UVic毕业证书)成绩单留信认证
 
Call Girls Amethi 9332606886 HOT & SEXY Models beautiful and charming call g...
Call Girls Amethi  9332606886 HOT & SEXY Models beautiful and charming call g...Call Girls Amethi  9332606886 HOT & SEXY Models beautiful and charming call g...
Call Girls Amethi 9332606886 HOT & SEXY Models beautiful and charming call g...
 
Top profile Call Girls In Palghar [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In Palghar [ 7014168258 ] Call Me For Genuine Models W...Top profile Call Girls In Palghar [ 7014168258 ] Call Me For Genuine Models W...
Top profile Call Girls In Palghar [ 7014168258 ] Call Me For Genuine Models W...
 
In Riyadh Saudi Arabia |+966572737505 | Buy Cytotec| Get Abortion pills
In Riyadh Saudi Arabia |+966572737505 | Buy Cytotec| Get Abortion pillsIn Riyadh Saudi Arabia |+966572737505 | Buy Cytotec| Get Abortion pills
In Riyadh Saudi Arabia |+966572737505 | Buy Cytotec| Get Abortion pills
 
一比一原版(Otago毕业证书)奥塔哥理工学院毕业证成绩单学位证靠谱定制
一比一原版(Otago毕业证书)奥塔哥理工学院毕业证成绩单学位证靠谱定制一比一原版(Otago毕业证书)奥塔哥理工学院毕业证成绩单学位证靠谱定制
一比一原版(Otago毕业证书)奥塔哥理工学院毕业证成绩单学位证靠谱定制
 
一比一定(购)坎特伯雷大学毕业证(UC毕业证)成绩单学位证
一比一定(购)坎特伯雷大学毕业证(UC毕业证)成绩单学位证一比一定(购)坎特伯雷大学毕业证(UC毕业证)成绩单学位证
一比一定(购)坎特伯雷大学毕业证(UC毕业证)成绩单学位证
 
一比一原版(CSUEB毕业证书)东湾分校毕业证原件一模一样
一比一原版(CSUEB毕业证书)东湾分校毕业证原件一模一样一比一原版(CSUEB毕业证书)东湾分校毕业证原件一模一样
一比一原版(CSUEB毕业证书)东湾分校毕业证原件一模一样
 

Arduino Object Oriented Programming (OOP).pptx

  • 2. introduction to Arduino Object Oriented Programming programming using C++ and OOP. example how to re-write Arduino using the OOP way. you’ll get the complete code so you can directly test on your own Arduino board. Alright, let’s get started!
  • 3. Table of Contents • Arduino Object Oriented Programming limitations • Hardware setup • A class for a LED • Class for a Button • Complete Arduino Object Oriented code • Reorganize your Arduino OOP code • Led.h • Led.cpp • Button.h • Button.cpp • Main • Arduino Object Oriented: it’s already everywhere
  • 4. Arduino Object Oriented Programming limitations • Even if Oriented Object Programming is possible with Arduino, you have to know the limitations. • Basically, the Arduino language is a subset of C/C++. You can create classes, use inheritance, composition, and many other nice OOP functionalities, but: • The STL library is not available (not natively, you can still use an external library to get most of the functionalities). • You can’t use exceptions.
  • 5. Hardware simulator setup • Note: it’s OK if you don’t have any Arduino or hardware component available. You can still follow this tutorial and get all the knowledge you need. • We’ll be using the following hardware setup: • Component list: • 1* Arduino Uno board (Any Arduino board will do) • 1* breadboard • 4* LEDs (any color you want) • 4* 220 Ohm resistors (one for each LED) • 1* push button • 1* 10k Ohm resistor (for the push button) • A few male-to-male wires • The goal of the tutorial will be to turn on LED 1 & 3, and turn off LED 2 & 4 when the button is pressed. When the button is not pressed we’ll do the opposite – turn off LED 1 & 3, and turn on LED 2 & 4. • Now let’s go step by step to reach this goal.
  • 6. CLASSES AND OBJECTS • A class is a collection of functions and variables that can be used to perform specific programming tasks. In Arduino programming, a class is basically the same thing as a library. • A class is like a template that lets you reuse code without having to type it over and over. For example, say you needed to make 20 documents that all had the same font, header, and margins. It would be too time consuming to change the formatting of all 20 documents individually. Instead you could make a template document with the right font, header, and margins, then add the text and save it as a separate file. • Classes are like that template document. Classes are templates of code for what are called “objects”. Think of a class as the template document, and the object as the individual document. Objects let you access the functions and variables inside of a class. An object is sometimes called an “instance” of a class.
  • 7. A class for a LED • An LED is a very basic component. You setup a digital pin to INPUT mode, and then you just need to set its state to HIGH or LOW. • Let’s create a simple class to wrap the LED functionalities.
  • 8. You can now use this class to create a Led object. • With the Led class we can hide all the Arduino stuff about digital pins. Note that the object must be created in the global scope if you want to be able to use it in the setup() and loop() functions.
  • 9. Class for a Button • Let’s now write some OOP code for a push button! The button is a little bit more complex, because we need to add a debounce functionality if we want it to remove the mechanical noise. • Here again, all the complexity is hidden. Once you’ve implemented the debounce functionality for the button inside the class, you don’t need to think about it anymore.
  • 10. create a Button object • In your program, you just need to create a Button object and check whether it’s pressed or not. • When the button is pressed, we turn on LED 1. As you can see the code in the loop() is quite small and clean.
  • 11. Complete Arduino Object Oriented code • OOP is great for reusability. Do you remember we added 4 LEDs at the beginning of the tutorial? Well, now that we have a class for a LED, we just need to create additional objects, all the implementation is already done. • Here is the complete code, including the Led class, the Button class, and the main code of the program: • https://wokwi.com/projects/34921 7893036065362
  • 12. Reorganize your Arduino OOP code • The previous code works well, but everything is in the same file. We want to use OOP for reusability, modularity, readability, etc, but it’s impossible if we write all the code in one file. As your program grows in complexity, so your code length, until you reach a point where the code is so long that you spend more time finding stuff and fixing bugs instead of adding new functionalities. • We’ll separate the code into 3 parts: the Led class, the Button class, and the “main”.
  • 13. Each class will be on its own independent file. • in fact, for one class we’ll have 2 files: one Constructor Cpp file (.cpp) and one header file (.h). • Your code will become much more readable. The classes you create will be also more reusable as you can include them in every file where you need them. • Note that creating other files for an Arduino program is quite tricky. You must create all your files inside your Arduino program folder. Example: if your program is named Test.ino, then it will be automatically saved on a Test/ folder (the Arduino IDE does that). You’ll have to put all your files in the Test/folder as well, so the Arduino IDE can find them. • Go into the folder of your current Arduino program. Create 4 files: • Led.h • Led.cpp • Button.h • Button.cpp • The files won’t appear in the Arduino IDE right away. You’ll need to close the project, and open it again so the files will show up in tabs on the top.
  • 14. Led.h • In this file we just write the class declaration, which is the interface that any client using this class will use. This makes the class much easier to understand and use. • The header guards (first 2 lines, and last line) will make sure that the Led class will not be included more than once, if for example you have multiple #include "Led.h" in other parts of your program. • Also note that I’ve added #include <Arduino.h> at the beginning. Why? This include is necessary to use the specific Arduino functions and types (think of pinMode(), digitalWrite(), byte). In the “main” file we don’t need to write it because it’s automatically added when you compile your code. But in any other file, you need to add it by yourself.
  • 16. Button.h • Same warning as for the Led.h file. Don’t forget to include the Arduino library at the beginning of the file. Also put all your class declaration inside header guards so you won’t include it twice in other parts of your code.
  • 18. Main • Well, as you can see, the code is now much clearer and readable. Plus, you could import the Led and Button class in any other part of your application. • For example you could create a class named LedPanel. This class would contain an array of Led objects and handle them. In your “main”, you could just import the LedPanel header file without having to know about the Led class. • You can always create more modules on top of other modules. That’s the power of Object Oriented Programming with Arduino (and not only Arduino by the way).
  • 19. Arduino Object Oriented: it’s already everywhere • If it’s the first time you use Object Oriented Programming with Arduino, well… Don’t think you’re doing something new! In fact, many of the Arduino already use OOP. • A few OOP library examples: • Servo: control a servo motor • Serial: communicate between your Arduino board and other devices • Wire: communicate though the I2C protocol • SevSeg: control a digit 7-segment display • EEPROM: store values permanently in your Arduino board • If you’re already familiar with OOP and want to use it in your Arduino programs, don’t hesitate and do it! OOP is certainly not the answer to everything, but if you know how to use it right, there is no reason you couldn’t get its benefits with Arduino.