Arduino Traffic Light Project (a Practical Example of Organizing Your C++ Code)

Sdílet
Vložit
  • čas přidán 8. 09. 2024

Komentáře • 105

  • @i.katsantonis1378
    @i.katsantonis1378 Před 4 lety +8

    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

  • @weekendprogrammer7545
    @weekendprogrammer7545 Před 6 lety +6

    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.

  • @ericksonengineering7011
    @ericksonengineering7011 Před 6 lety +7

    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_gelli
    @gabriel_gelli Před 4 lety +2

    Very nice video! Passed thru lots of cool concepts, such as state machines and making the timer class for not using delay

    • @IndrekL
      @IndrekL  Před 4 lety

      Thank you for the feedback!

    • @lloydgwasira8087
      @lloydgwasira8087 Před 4 lety

      @@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

  • @eduardoarevalo3208
    @eduardoarevalo3208 Před 3 lety +3

    Awesome example! Thanks for sharing and keep up the great work!!

    • @IndrekL
      @IndrekL  Před 3 lety +1

      Thanks for watching!

  • @Zubairkhan-rb1fx
    @Zubairkhan-rb1fx Před 2 lety

    i have read all your comments... man you have some knowledge.. impressed

  • @MrNezlee
    @MrNezlee Před 3 lety +1

    Very good example for C++ beginner like me!

  • @sumanthyadav4919
    @sumanthyadav4919 Před 3 lety

    Loved the way you have gone deeper the topic

  • @rfdave3980
    @rfdave3980 Před 2 lety

    Excellent Video. So glad you didn't call them Libraries.

  • @felipecavalcante6203
    @felipecavalcante6203 Před 4 lety +1

    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!

    • @IndrekL
      @IndrekL  Před 4 lety +2

      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
      czcams.com/video/Dp3RMb0e1eA/video.html
      I needed the break the encapsulation of the screen library since otherwise, it would be too slow.

    • @felipecavalcante6203
      @felipecavalcante6203 Před 4 lety

      @@IndrekL Incredible! More one time, thanks for sharing your skills.I will start to work with classes too! :)

  • @frankcorr6566
    @frankcorr6566 Před 2 lety

    A really good simple example. Very useful.

  • @hz.turqay426
    @hz.turqay426 Před 2 lety

    İt was really great explonation, thanx for this awesome video

  • @wlricardo
    @wlricardo Před rokem

    Wonderfull project !! Congrats !!

  • @SymphonyKol_androidLover

    Thanks a lot dude, was just searching for this ,thanks a lot man.:):):)

  • @drnda2007
    @drnda2007 Před 2 lety

    Good example

  • @gabriel_gelli
    @gabriel_gelli Před 4 lety +1

    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.

    • @IndrekL
      @IndrekL  Před 4 lety +2

      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_gelli
      @gabriel_gelli Před 4 lety

      Indrek Thank you a lot for the detailed response!

  • @vardfriki7274
    @vardfriki7274 Před 2 lety +1

    Thank you so much!

  • @atvdk3377
    @atvdk3377 Před 3 lety

    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..

  • @swapnilbandgar8730
    @swapnilbandgar8730 Před 3 lety

    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

  • @maxim25o2
    @maxim25o2 Před 3 lety

    Thats super using OOP for arduino and make tutorial. make more of them, :)

  • @iceman1st1
    @iceman1st1 Před 2 lety

    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

  • @naemamir678
    @naemamir678 Před 3 lety

    Indrek, can you please make similar project but with pedestrian LEDs and with their codes

  • @nischal_mahadeshwar
    @nischal_mahadeshwar Před 4 lety +1

    Amazing tutorial!

  • @hinyu0323
    @hinyu0323 Před 3 lety

    Great, Thanks! I don't know Arduino IDE can write OOP. Thank you. I can forget microsoft Visual Studio Now.

  • @aryanmishra5591
    @aryanmishra5591 Před 5 lety +1

    Any more Arduino examples using classes and objects.

  • @dewisuharty1438
    @dewisuharty1438 Před 2 lety

    hello, can you help me to convert traffic light flow program from PLC into C++ programming ?

  • @lastchance045
    @lastchance045 Před 24 dny

    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

  • @mastermoarman
    @mastermoarman Před 4 lety

    another question. why do you use a second set of variables (ie _greenled =greenled) instead of just using greenled?

    • @IndrekL
      @IndrekL  Před 4 lety +1

      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.

  • @hashanmadusanka6128
    @hashanmadusanka6128 Před 2 lety

    you are amazing

  • @vardfriki7274
    @vardfriki7274 Před 2 lety

    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

    • @IndrekL
      @IndrekL  Před 2 lety

      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}};

  • @hhhhhhbbvcddtg1007
    @hhhhhhbbvcddtg1007 Před 4 lety

    Dear Sir, I need Arduino Uno Code " Arduino Uno for variable pulse width controlled with a potentiometer

    • @IndrekL
      @IndrekL  Před 4 lety

      Hey! Do you mean the code from this video?
      czcams.com/video/m4EpTYaBBJ4/video.html
      It's this:
      volatile int period = 25;
      void setup() {
      noInterrupts();
      TCCR1A = 0;
      TCCR1B = (1

  • @johnsun2416
    @johnsun2416 Před 6 měsíci

    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.

  • @MrPeeterlaas
    @MrPeeterlaas Před 5 lety +1

    Suured tänud!

  • @programing_time_0174
    @programing_time_0174 Před 3 lety

    MUCHAS GRACIAS !!!!

  • @admasmamo9395
    @admasmamo9395 Před 2 lety +1

    you are very fast to coding

  • @iamsparkicus
    @iamsparkicus Před 3 lety

    Thanks for the video. Please lose the music, though. It drove me insane!

  • @atheer1415
    @atheer1415 Před 5 lety

    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

    • @IndrekL
      @IndrekL  Před 5 lety

      Can you show your code that you already have?

    • @atheer1415
      @atheer1415 Před 5 lety

      // 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);
      }
      }
      }

    • @IndrekL
      @IndrekL  Před 5 lety

      @@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
      }

  • @shaistakhalid7761
    @shaistakhalid7761 Před 6 lety

    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.

    • @IndrekL
      @IndrekL  Před 6 lety +2

      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.

  • @musicalhamsa5892
    @musicalhamsa5892 Před 4 lety

    Can you make one on how to make a button work?
    Also you just got a sub

    • @IndrekL
      @IndrekL  Před 4 lety

      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:
      czcams.com/video/Xz4iVpdMd-w/video.html

  • @bob-ny6kn
    @bob-ny6kn Před 2 lety

    RED
    YEL
    GRN

  • @mastermoarman
    @mastermoarman Před 4 lety

    is there a reason you used int for the pins instead of #define?

    • @IndrekL
      @IndrekL  Před 4 lety

      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.

    • @IndrekL
      @IndrekL  Před 4 lety +1

      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'

    • @IndrekL
      @IndrekL  Před 4 lety +1

      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.

    • @mastermoarman
      @mastermoarman Před 4 lety

      @@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

  • @mastermoarman
    @mastermoarman Před 4 lety

    also would you put the seccond set of lights in a separate class or ?

    • @IndrekL
      @IndrekL  Před 4 lety

      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();

    • @mastermoarman
      @mastermoarman Před 4 lety

      @@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.

    • @IndrekL
      @IndrekL  Před 4 lety

      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

    • @mastermoarman
      @mastermoarman Před 4 lety

      @@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)

    • @IndrekL
      @IndrekL  Před 4 lety

      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

  • @tr1deca658
    @tr1deca658 Před 4 lety

    Can you give me the script of the traffic light program

    • @IndrekL
      @IndrekL  Před 4 lety +2

      Hey! Link to the Github page is in the description box:
      github.com/indrekluuk/ArduinoClassesTutorial

  • @laekemariamgetu4259
    @laekemariamgetu4259 Před 6 lety

    thanks ...how about with 12 led ?

    • @IndrekL
      @IndrekL  Před 6 lety

      What do you mean exactly? Four traffic lights three LEDs each?

    • @user-vd1rb7by3q
      @user-vd1rb7by3q Před 6 lety

      Indrek yes please

    • @IndrekL
      @IndrekL  Před 6 lety +2

      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()

    • @TimeoutMegagameplays
      @TimeoutMegagameplays Před 5 lety +1

      @@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.

    • @IndrekL
      @IndrekL  Před 5 lety +2

      @@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.

  • @gustavogarcia7442
    @gustavogarcia7442 Před rokem

    Too fast for me

  • @aryanmishra5591
    @aryanmishra5591 Před 5 lety

    Anyone help the button code is not working.

    • @IndrekL
      @IndrekL  Před 5 lety

      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.

    • @aryanmishra5591
      @aryanmishra5591 Před 5 lety

      @@IndrekL ok thank you so much for the reply.
      It's working now.

    • @aryanmishra5591
      @aryanmishra5591 Před 5 lety

      @@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.

    • @IndrekL
      @IndrekL  Před 5 lety +1

      @@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.

  • @ericthered9655
    @ericthered9655 Před 3 lety

    Is a class the same thing as a library?

    • @IndrekL
      @IndrekL  Před 3 lety +1

      Basically yes. All the Arduino libraries are just C++ classes.

    • @ericthered9655
      @ericthered9655 Před 3 lety

      @@IndrekL OK, and public means it's a "global variable" in Arduino jargon?

    • @IndrekL
      @IndrekL  Před 3 lety +1

      @@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.

    • @ericthered9655
      @ericthered9655 Před 3 lety

      @@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!

    • @IndrekL
      @IndrekL  Před 3 lety +1

      @@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());
      }

  • @everythingfree4643
    @everythingfree4643 Před 5 lety

    Brother it to good but send circuit diagram

    • @IndrekL
      @IndrekL  Před 5 lety

      Hey! You can see connection diagram at 2:55 in the video.

  • @hund2955
    @hund2955 Před 3 lety

    Why doas the Titel say organising c++ Code? The arduino IDE uses C. The language C is very different from c++

    • @IndrekL
      @IndrekL  Před 3 lety +1

      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.

  • @yupingliao4078
    @yupingliao4078 Před 5 lety

    ok

  • @shaygoldin
    @shaygoldin Před 2 lety

    Way too fast for me. I saw many new features in your video. I had enough half way. NOT for beginners. Thank you.

  • @albert4866
    @albert4866 Před 4 lety

    This guy sounds like electroBOOM

  • @trishnadeka9209
    @trishnadeka9209 Před 5 lety

    BRO IS THE PROGRAMMING CORRECT

  • @col8981
    @col8981 Před 3 lety

    Way too complicated