I have ordered my first arduino uno. I will be watching your videos when i'll be experimenting. your content is better than most on YT
@IndrekL4 жыл бұрын
Thanks!
@weekendprogrammer75456 жыл бұрын
Awesome. I am beginner for arduiNo and you just showed me how flexibly we can use this IDE , though I knew c++ I wasn't knowing that we can use the function, class and add our own class like c++ or Java Loved it man.
@IndrekL6 жыл бұрын
Thanks!
@Zubairkhan-rb1fx3 жыл бұрын
i have read all your comments... man you have some knowledge.. impressed
@ericksonengineering70116 жыл бұрын
Nicely done, thanks. A simple example of how to create a class, written from scratch and with a commonly understood model. Just the basics, no extra complexity.
@gabriel_gelli4 жыл бұрын
Very nice video! Passed thru lots of cool concepts, such as state machines and making the timer class for not using delay
@IndrekL4 жыл бұрын
Thank you for the feedback!
@lloydgwasira80874 жыл бұрын
@@IndrekL hie guys I would like to learn how to learn how to develop traffic lights abd how to use the softwares can you help me
@sumanthyadav49193 жыл бұрын
Loved the way you have gone deeper the topic
@eduardoarevalo32083 жыл бұрын
Awesome example! Thanks for sharing and keep up the great work!!
@IndrekL3 жыл бұрын
Thanks for watching!
@MrNezlee3 жыл бұрын
Very good example for C++ beginner like me!
@rfdave39802 жыл бұрын
Excellent Video. So glad you didn't call them Libraries.
@frankcorr65662 жыл бұрын
A really good simple example. Very useful.
@atvdk33773 жыл бұрын
Hi, i am trying to do this step by step, but i have not yet figured out how to determine how to add more buttons/inputs and differentiate which button is pressed.. i'm so confused..
@gabriel_gelli4 жыл бұрын
Is there any downside on having the methods in the header files as you did? It is quite easier and cleaner in a small code, it remembers how classes are used in Python. I was wondering if it could bring any problems, since in libraries the methods usually go into separate c++ files. Or what would be the benefits of using the .cpp. Another curiosity is about not having to #include "Arduino.h" in the header files.
@IndrekL4 жыл бұрын
Hey! 1. The main benefit of the .cpp file is compilation time. If you make a change in the header file, all the files that include it will be recompiled. Athough this isn't much of a problem with Arduino projects since those are very small. 2. You can't do circular dependencies between header-only libraries. If you don't need to do that, then it is not an issue. 3. You may want to declare static a global variable that all instances of your class can see. You can't define global variables inside the header file if you include it more than once. You will get a "conflicting declaration" error. Maybe there is something more that I can't remember right now. But, for most cases, you actually can just write the method code into the header file.
@gabriel_gelli4 жыл бұрын
Indrek Thank you a lot for the detailed response!
@swapnilbandgar87304 жыл бұрын
Please help me ::::: I want to start my loop only when my Ultrasound sensor detects the object and if the object is not present then it should directly stop all the operations until the ultrasound sensor detect another (different or same) object
@felipecavalcante62034 жыл бұрын
I really like this idea. But, if you see, you use more memory space to work with OOP. But, I confess that is very cool!
@IndrekL4 жыл бұрын
Yes, there is some overhead since every class has to hold it's pin numbers in memory (in this case, it would be easy to slash this overhead by half by replacing "int" with "uint8_t" since pin numbers will never exceed 255). In the majority of cases, maintainability is more important than efficiency. You can do the necessary optimizations in those rare situations when you need the extra few bytes or some additional clock cycles. For example in my camera project kzbin.info/www/bejne/eqGWg4CYZcpkm6M I needed the break the encapsulation of the screen library since otherwise, it would be too slow.
@felipecavalcante62034 жыл бұрын
@@IndrekL Incredible! More one time, thanks for sharing your skills.I will start to work with classes too! :)
@dewisuharty14382 жыл бұрын
hello, can you help me to convert traffic light flow program from PLC into C++ programming ?
@hhhhhhbbvcddtg10074 жыл бұрын
Dear Sir, I need Arduino Uno Code " Arduino Uno for variable pulse width controlled with a potentiometer
@IndrekL4 жыл бұрын
Hey! Do you mean the code from this video? kzbin.info/www/bejne/o2WooYePlqd1gJY It's this: volatile int period = 25; void setup() { noInterrupts(); TCCR1A = 0; TCCR1B = (1
@iceman1st12 жыл бұрын
hi, ive never done circuit making and want to learn it so i can make something. this video gave me some sort of idea to how the coding was working, but still got a long way to go. i got a arduino and accelerometer and want to make a auto brake light that will come on when it detects the vehicle slowing down. do you know how i could make it work
@mastermoarman4 жыл бұрын
another question. why do you use a second set of variables (ie _greenled =greenled) instead of just using greenled?
@IndrekL4 жыл бұрын
The variable with the underscore "_greenLED" is the object level variable. I need to store the pin numbers for later use inside "green," "yellow," "red," and "off" methods. Those method can't see the "greenLED" variable since it is only visible inside the class' constructor "TrafficLight(int greenLED, int yellowLED, int redLED)" Do I understand the question correctly that you mean why here is "pinMode(_greenLED, OUTPUT);" instead of "pinMode(greenLED, OUTPUT);"? TrafficLight(int greenLED, int yellowLED, int redLED) { _greenLED = greenLED; _yellowLED = yellowLED; _redLED = redLED; pinMode(_greenLED, OUTPUT); pinMode(_yellowLED, OUTPUT); pinMode(_redLED, OUTPUT); green(); } Here it doesn't matter at all. It could have been "pinMode(greenLED, OUTPUT);" as well. I used "_greenLED" since I already stored it. I thought it would be a little more consistent if every call to the pins will use the object level variable.
@hz.turqay4263 жыл бұрын
İt was really great explonation, thanx for this awesome video
@aryanmishra55915 жыл бұрын
Any more Arduino examples using classes and objects.
@vardfriki72743 жыл бұрын
Hi again Indrek, Can please explain how to use arrays and/or enum to populate variables for multiple instances of a class? Such as pin numbers and serial outputs or joystick buttons? Is there a trick to display required/available variables for a class for assignment? Kindest Regards and many thanks, Vard
@IndrekL3 жыл бұрын
Do you mean how to initialize array of classes? For example this is array of TrafficLights with two elements: TrafficLight trafficLights[2] = {TrafficLight(2,3,4), TrafficLight(5,6,7)}; or you can simplify it by writing like this: TrafficLight trafficLights[2] = {{2,3,4},{5,6,7}};
@wlricardo2 жыл бұрын
Wonderfull project !! Congrats !!
@atheer14155 жыл бұрын
Plz..i have question...i have two loop ..void loop and loop void blue05 How can I do call fun in void loop to let the system check if the Bluetooth connected or not it is connected I can control the system manually if not will work auto ....plz help me
@IndrekL5 жыл бұрын
Can you show your code that you already have?
@atheer14155 жыл бұрын
// RemoteXY select connection mode and include library #define REMOTEXY_MODE__SOFTSERIAL #include #include #include Servo myservo; // RemoteXY connection settings #define REMOTEXY_SERIAL_RX A0 #define REMOTEXY_SERIAL_TX A1 #define REMOTEXY_SERIAL_SPEED 9600 // RemoteXY configurate #pragma pack(push, 1) uint8_t RemoteXY_CONF[] = { 255,3,0,0,0,29,0,8,97,0, 2,0,65,21,22,11,250,26,31,31, 79,78,0,79,70,70,0,5,0,14, 10,30,30,250,26,31 }; #define PIN_SWITCH_1 6 // MOTORS PINs const int BackForward = 2; const int BackBackward = 3; const int FrontForward = 4; const int FrontBackward = 5; // Other variable int i =0; int pos = 0; boolean fire = false; #define Forward_S 8 //forward sensor #define left_S 9 //left sensor #define right_S 10 //right sensor #define leftbackward_S 11 //left sensor #define rightbackward_S 12 //right sensor #define LM1 2 // left motor #define LM2 3 // left motor #define RM1 4 // right motor #define RM2 5 // right motor #define pump 6 SoftwareSerial mySerial (A1, A0); //SoftwareSerial mySerial = SoftwareSerial( A1, A0 ); // this structure defines all the variables of your control interface struct { // input variable uint8_t switch_1; // =1 if switch ON and =0 if OFF int8_t joystick_1_x; // =-100..100 x-coordinate joystick position int8_t joystick_1_y; // =-100..100 y-coordinate joystick position // other variable uint8_t connect_flag; // =1 if wire connected, else =0 } RemoteXY; #pragma pack(pop) ///////////////////////////////////////////// // END RemoteXY include // ///////////////////////////////////////////// void setup() { pinMode(Forward_S, INPUT); pinMode(left_S , INPUT); pinMode(Forward_S, INPUT); pinMode(right_S, INPUT); pinMode(leftbackward_S, INPUT); pinMode(rightbackward_S, INPUT); pinMode(LM1, OUTPUT); pinMode(LM2, OUTPUT); pinMode(RM1, OUTPUT); pinMode(RM2, OUTPUT); pinMode(pump, OUTPUT); RemoteXY_Init (); // define pin modes for tx, rx: pinMode( A1, INPUT); pinMode( A0, OUTPUT); // set the data rate for the SoftwareSerial port Serial.begin(9600); pinMode (PIN_SWITCH_1, OUTPUT); /* pinMode (BackForward, OUTPUT); pinMode (BackBackward, OUTPUT); pinMode (FrontForward, OUTPUT); pinMode (FrontBackward, OUTPUT);*/ // TODO you setup code } void put_off_fire() { delay (500); digitalWrite(LM1, HIGH); digitalWrite(LM2, HIGH); digitalWrite(RM1, HIGH); digitalWrite(RM2, HIGH); digitalWrite(pump, HIGH); delay(500); digitalWrite(pump,LOW); myservo.write(90); fire=false; } void loop(){ if (digitalRead(Forward_S) ==0) //If Fire is straight ahead { //Move the robot forward digitalWrite(LM1, LOW); digitalWrite(LM2, HIGH); digitalWrite(RM1, LOW); digitalWrite(RM2, HIGH); fire = true; } else if (digitalRead(left_S) ==0) //If Fire is to the left { //Move the robot left digitalWrite(LM1, HIGH); digitalWrite(LM2, LOW); digitalWrite(RM1, HIGH); digitalWrite(RM2, HIGH); fire = true; } else if (digitalRead(rightbackward_S) ==0) //If Fire is to the left { //Move the robot left digitalWrite(LM1, HIGH); digitalWrite(LM2, LOW); digitalWrite(RM1, HIGH); digitalWrite(RM2, HIGH); fire = true; } else if (digitalRead(right_S) ==0) //If Fire is to the right { //Move the robot right digitalWrite(LM1, HIGH); digitalWrite(LM2, HIGH); digitalWrite(RM1, HIGH); digitalWrite(RM2, LOW); fire = true; } else if (digitalRead(leftbackward_S) ==0) //If Fire is to the right { //Move the robot right digitalWrite(LM1, HIGH); digitalWrite(LM2, HIGH); digitalWrite(RM1, HIGH); digitalWrite(RM2, LOW); fire = true; } delay(500); //Slow down the speed of robot while (fire == true) { put_off_fire(); } } void blue05 (){ RemoteXY_Handler (); while (i2){ digitalWrite(PIN_SWITCH_1, (RemoteXY.switch_1==0)?LOW:HIGH); //Turn right if (RemoteXY.joystick_1_y > 50){ digitalWrite(FrontForward , HIGH); digitalWrite(FrontBackward , LOW); } //Turn left if (RemoteXY.joystick_1_y < -50){ digitalWrite(FrontForward , LOW); digitalWrite(FrontBackward , HIGH); } //Go ahead if (RemoteXY.joystick_1_y > -50 && RemoteXY.joystick_1_y < 50 ){ digitalWrite(FrontForward , HIGH); digitalWrite(FrontBackward , HIGH); } if (RemoteXY.joystick_1_x > 50){ //go Forward digitalWrite(BackForward , HIGH); digitalWrite(BackBackward , LOW); } if (RemoteXY.joystick_1_x < -50){ // go Backward digitalWrite(BackForward , LOW); digitalWrite(BackBackward , HIGH); } if (RemoteXY.joystick_1_x > -50 && RemoteXY.joystick_1_x < 50 ){ // stop digitalWrite(BackForward , LOW); digitalWrite(BackBackward , LOW); } } }
@IndrekL5 жыл бұрын
@@atheer1415 You probably should call "RemoteXY_Handler (); " in the beginning of "void loop()" Then you should be able ta check if it is connected: if (RemoteXY.connect_flag) { blue05 (); } else { // do the automatic control }
@naemamir6783 жыл бұрын
Indrek, can you please make similar project but with pedestrian LEDs and with their codes
@mastermoarman4 жыл бұрын
also would you put the seccond set of lights in a separate class or ?
@IndrekL4 жыл бұрын
I would create a second object based on the same class. Like this: int PIN_GREEN = 4; int PIN_YELLOW = 3; int PIN_RED = 2; int PIN_GREEN_2 = 7; int PIN_YELLOW_2 = 6; int PIN_RED_2 = 5; TrafficLight trafficLight(PIN_GREEN, PIN_YELLOW, PIN_RED); TrafficLight trafficLight2(PIN_GREEN_2, PIN_YELLOW_2, PIN_RED_2); (C++ class is the blueprint and object is an instance based on thet blueprint. Each object has it's own set of internal variables) In the loop method, you have to call "loop" for both of them trafficLight.loop(); trafficLight2.loop();
@mastermoarman4 жыл бұрын
@@IndrekL would you use the same loop or would you have to make a separate loop inside that class? thank you for all your help.
@IndrekL4 жыл бұрын
The same loop. The code that is executed is exactly the same for both traffic lights. They are just different objects in memory with their own internal variables, but with exactly the same behavior. When you call trafficLight.loop(); then it handles the LED colors for pins 2, 3, 4 When you call trafficLight2.loop(); then it handles the LED colors for pins 5, 6, 7
@mastermoarman4 жыл бұрын
@@IndrekL hmmmm how will it work when the set void color functions are all based on _colorLED variable? and how would I get it to be so the first set of lights is opposite of the second set? (main and side street)
@IndrekL4 жыл бұрын
They have the same name internally inside the class but they don't have the same variables in memory. When you create a new class object (trafficLight and trafficLight2) then it creates a new set of variables in memory. One is trafficLight. _colorLED and the other is trafficLight2. _colorLED If you call trafficLight.loop() then it operates on the trafficLight. _colorLED And if you call trafficLight2.loop() then it operates on the trafficLight2. _colorLED
@SymphonyKol_androidLover3 жыл бұрын
Thanks a lot dude, was just searching for this ,thanks a lot man.:):):)
@mastermoarman4 жыл бұрын
is there a reason you used int for the pins instead of #define?
@IndrekL4 жыл бұрын
Hey! C++, in general, is trying to move away from macros. The main problem with them is that compiler can't see them and can't optimize them directly. "#define" macros are basically just "search and replace" before the actual compilation of the code. It doesn't really matter with simple constants like pin numbers. C++ compiler can see that I didn't change the "PIN_GREEN" anywhere in the code and will optimize it. So the result will be identical to #define PIN_GREEN 4 Although it would have been more correct for me to write it as "const": const int PIN_GREEN = 4; In the end, it comes down to preference.
@IndrekL4 жыл бұрын
One thing is that you get more cryptic error messages if you make mistakes when using #define. Let's say we have #define PIN_GREEN 4 Now, if you accidentally write in your code PIN_GREEN = 3; You will get an error: lvalue required as left operand of assignment If you are a beginner, you can't even understand what that means. But if you have const int PIN_GREEN = 4; then PIN_GREEN = 3; will give a more understandable error: assignment of read-only variable 'PIN_GREEN'
@IndrekL4 жыл бұрын
It starts to matter more if you are defining functions with macros like this: #define SQUARE(x) ((x) * (x)) 1. if you don't add all the brackets correctly, you will get unintended behaviors. 2. These will always be inlined. It is good for speed, but if you use it a lot, it will grow your executable size. 3. Macros are good if your parameters are constants. Then the calculation will be done in compile time. With normal functions, the calculation will happen during runtime. But C++14 added keyword "constexpr" to address this issue. The equivalent to the "#define SQUARE(x) ((x) * (x))" macro in modern C++ is: constexpr int square(int x) { return x*x; } Now, depending on your compiler optimization parameters compiler can use it as function or inline it like a macro. Or do the calculation at compilation time if the input parameter is a constant.
@mastermoarman4 жыл бұрын
@@IndrekL oh my thank you. I am new and been trying to figure out why some people use define and others use into. Good to know
@laekemariamgetu42596 жыл бұрын
thanks ...how about with 12 led ?
@IndrekL6 жыл бұрын
What do you mean exactly? Four traffic lights three LEDs each?
@M40-y4x6 жыл бұрын
Indrek yes please
@IndrekL6 жыл бұрын
You can add traffic lights by adding new TrafficLight objects: TrafficLight trafficLight1(PIN_GREEN_1, PIN_YELLOW_1, PIN_RED_1); TrafficLight trafficLight2(PIN_GREEN_2, PIN_YELLOW_2, PIN_RED_2); TrafficLight trafficLight3(PIN_GREEN_3, PIN_YELLOW_3, PIN_RED_3); You also need to add controlling code of the traffic lights in the loop()
@TimeoutMegagameplays5 жыл бұрын
@@IndrekL That's the beauty of OOP, if you had done it with classic procedural programming methods creating extra traffic lights would be a torture, it would be cool to have more programming tutorials like this from you.
@IndrekL5 жыл бұрын
@@TimeoutMegagameplays I have to come up with some project that is simple enought to fit into one video. This trafic light project was perfect for that. There are lots of tutorials about how to do OOP but not enough practical example how to use it in actual projects. I think the most importart part is to show the thought process of when and why I am creating a new class.
@shaistakhalid77616 жыл бұрын
I'm getting an error while running the code. Can you please create a zip file for the whole coding so that all the errors are removed up. It would be great if you help me out soon. As, I'm having my project day this week.
@IndrekL6 жыл бұрын
Hey! Link to code is in the description box: github.com/indrekluuk/ArduinoClassesTutorial If you click on the "Cloner or Download" button then you get a zip file of the project files. Unzip the content into "ArduinoClassesTutorial" folder and it should compile.
@ericthered96553 жыл бұрын
Is a class the same thing as a library?
@IndrekL3 жыл бұрын
Basically yes. All the Arduino libraries are just C++ classes.
@ericthered96553 жыл бұрын
@@IndrekL OK, and public means it's a "global variable" in Arduino jargon?
@IndrekL3 жыл бұрын
@@ericthered9655 "public" means that the class or method in the class is accessible outside of the class. If you define a class' method "private" then it is usable only inside the class.
@ericthered96553 жыл бұрын
@@IndrekL Right. In most of the Arduino videos they call public "global" and private "local." I don't know why they don't use the same terms. Thank you!
@IndrekL3 жыл бұрын
@@ericthered9655 No public/private does not mean the same as global/local. A global variable is a variable that is defined outside of any function. Any part of the code can use that variable. A local variable is a variable that is defined inside a function scope and is only visible to that function. Those variables will "die" after the function returns. Then there are class member variables and functions (also known as methods) that can be public/private/protected. public/private/protected define access restrictions on class member variables and functions. But the classes themselves can be initialized as a global or local variable. So the term global and local describes a different concept than public/private. --------- Let's define a class: class MyCalculator { private: int a = 1; public: int b = 2; int getSum() { return calculateSum(); } private: int calculateSum() { return a + b; } }; Now we can initialize MyCalculator as a global variable at the top of our Arduino sketch: MyCalculator myGlobalCalculator; Since variable "a" and method "calculateSum()" are defined as "private" then calling these will give you a compilation error: myGlobalCalculator.a = 7; // error myGlobalCalculator.calculateSum(); // error But you can do this: myGlobalCalculator.b = 7; myGlobalCalculator.getSum(); You can also define the MyCalculator object inside a function as a local variable: void doSomething() { MyCalculator myLocalCalculator; myLocalCalculator.b = 11; Serial.println(myLocalCalculator.getSum()); // We can also call the global version of MyCalculator Serial.println(myGlobalCalculator.getSum()); }
@aryanmishra55915 жыл бұрын
Anyone help the button code is not working.
@IndrekL5 жыл бұрын
Did you connect the button the same way as in the video? Button must pull input down to ground since I am using internal pull-up while the button is not pressed.
@aryanmishra55915 жыл бұрын
@@IndrekL ok thank you so much for the reply. It's working now.
@aryanmishra55915 жыл бұрын
@@IndrekL one more thing why did u write mills() % 1000 to wait for 500 milliseconds ? How does that work. Why not millis() - previoustime >500. Then previoustime =millis(); Thanks in advance.
@IndrekL5 жыл бұрын
@@aryanmishra5591 In C++ "%" gives you reminder of a division (it has nothing to do with percentages). "millis() % 1000" will always give a time value between 0 to 999. The value of millis() will count milliseconds from 0 up to 4294967296 but if I divide that value with 1000 and take reminder I will get a value that will run 0,1,2,3...997,998,999,0,1,2,3,4...997,998,999,0,1,2.. etc. Now if I turn the LED off while the value is below 500 and on when it is above 500 then I get one blink per second. Your version would also work. But since the exact ON/OFF time of the blinking LED is not important then I just saved myself from having to use "previoustime" variable.
@tr1deca6584 жыл бұрын
Can you give me the script of the traffic light program
@IndrekL4 жыл бұрын
Hey! Link to the Github page is in the description box: github.com/indrekluuk/ArduinoClassesTutorial
@maxim25o23 жыл бұрын
Thats super using OOP for arduino and make tutorial. make more of them, :)
@admasmamo93952 жыл бұрын
you are very fast to coding
@drnda20073 жыл бұрын
Good example
@hinyu03233 жыл бұрын
Great, Thanks! I don't know Arduino IDE can write OOP. Thank you. I can forget microsoft Visual Studio Now.
@vardfriki72743 жыл бұрын
Thank you so much!
@nischal_mahadeshwar4 жыл бұрын
Amazing tutorial!
@IndrekL4 жыл бұрын
Thank you!
@musicalhamsa58924 жыл бұрын
Can you make one on how to make a button work? Also you just got a sub
@IndrekL4 жыл бұрын
Hey! Do you mean wiring or the code? Basically all you need to do is connect a button between an input pin and GND. Then enable INPUT_PULLUP and then you can read the button status with the digitalRead command. I made a video about it: kzbin.info/www/bejne/jquXmommmbKXY9k
@lastchance0454 ай бұрын
Very good information however --- . Please do not use music in your videos!!! The music is annoying and makes it difficult to fully understand your message
@iamsparkicus3 жыл бұрын
Thanks for the video. Please lose the music, though. It drove me insane!
@MrPeeterlaas5 жыл бұрын
Suured tänud!
@hund29553 жыл бұрын
Why doas the Titel say organising c++ Code? The arduino IDE uses C. The language C is very different from c++
@IndrekL3 жыл бұрын
No, Arduino most definitely uses C++. The first obvious clue is that you can use classes. C does not have classes. Basically, all the Arduino libraries are c++ classes. Secondly, if you turn on verbose compiler output from the preferences, you can see "avr-g++" in the compile log. g++ is gnu c++ compiler. A couple of years ago, Arduino even turned on C++11 by default, so we can use new c++ features like rvalue references and constexpr.
@everythingfree46436 жыл бұрын
Brother it to good but send circuit diagram
@IndrekL6 жыл бұрын
Hey! You can see connection diagram at 2:55 in the video.
@programing_time_01743 жыл бұрын
MUCHAS GRACIAS !!!!
@hashanmadusanka61282 жыл бұрын
you are amazing
@johnsun241610 ай бұрын
I'm a beginner, your code is getting bigger in size, but yet accomplish the same task, do more with less code is the better, delay should not be use, but instead use milli function.
@bob-ny6kn2 жыл бұрын
RED YEL GRN
@gustavogarcia7442 Жыл бұрын
Too fast for me
@yupingliao40785 жыл бұрын
ok
@shaygoldin2 жыл бұрын
Way too fast for me. I saw many new features in your video. I had enough half way. NOT for beginners. Thank you.