Teaching myself C++

Uncle peanut butter May 20, 2020

  1. Uncle peanut butter

    Uncle peanut butter TrainBoard Member

    179
    238
    9
    Need an opinion from the folks on here that have programming skills. I have my DCC++ set up and running and am ready to experiment with the capabilities of the world or arduino as it applies to our hobby.
    I have zero programming experience but I feel if I can learn to speak the language necessary it will make my projects more rewarding.
    I’m looking at this right now and trying to learn what I can: https://www.learncpp.com/cpp-tutorial/introduction-to-these-tutorials/
    Is my goal of learning this a realistic one?
    Thanks
     
  2. Sumner

    Sumner TrainBoard Member

    2,798
    5,841
    63
    I think I'd just work with C as it pertains to the Arduino. A number of YouTubes and other sources begin with very simple concepts and build from there using the Arduino as you go. Get one of the Arduino starter kits...

    https://www.google.com/search?q=ard...hrome.0.0l8.1469j0j7&sourceid=chrome&ie=UTF-8

    If you are actually doing something from the beginning vs. just trying to learn how to code I think you will find it more motivating. With one of these kits you would also have a separate Arduino to work with leaving your DCC++ system up and running,

    Sumner
     
    Uncle peanut butter likes this.
  3. FlightRisk

    FlightRisk TrainBoard Member

    548
    237
    14
    I agree that a primer is great because you have to understand a few concepts, but then look at the code a piece at a time and lookup questions like, "what is the C++ += operator do", then go back and check it out. Play by sticking code in an online compiler like this one https://www.learn-cpp.org/ or this one http://cpp.sh/. Not sure how well you like the link you posted, but there is also https://www.learn-cpp.org/. And if you are visual, videos of course.

    Then if you need help, post code from our GitHub repository here and ask for a breakdown of what it does. The hardest things (I think) for a someone new to learn are the binary math we need to use and pointers (the "address" that tells you where that object is stored vs. object itself). Example of maybe worst case for you:

    binary math:

    b[1]=((((DecoderAddress/64)%8)<<4) + (DecoderSubaddress%4<<1) + Activate%2) ^ 0xF8;

    Total gibberish! LOL. But if you draw 1's and 0's on a page to see how it is just manipulating those "bits" in a "byte" of 8 of them, you can see what it is doing. I need to do this myself all the time. So you have to learn that the % does a modulo calculation (give you the remainder of a division 5%3=2). And the << operator is shift left. So we shift those bits over to the left by 4 positions (00000011 becomes 00110000). And the last operation is a logical XOR or "exclusive or" function which is used to put the 1s and 0s in a particular pattern. And 0xF8 is a shorthand in hex to represent "11111000" (our pattern to mask against). It is actually easy to work in 2s once you get the hang of it. But maybe you are a mathematician and already know a lot of this!

    The normal processes of "if this, do that" and store this here and add these things together are relatively easy to do. So the general learning of C++ can be pretty quick and painless, but understanding code someone else has written is not as easy without comments in the code or you posting here the snippets and we can dissect it for you.
     
  4. Uncle peanut butter

    Uncle peanut butter TrainBoard Member

    179
    238
    9
    Not really committed to the lessons I linked to but once I get deeper in I’d like to stay with one teaching method.
    My main interest is to be able to run servo motors for turnout control.
    Therefore I need to be able to tell the command station which servo to address, tell the servo how many degrees to rotate, how much time will that rotation take etc.
    I know there are sketches readily available and in the end I will most likely use those as the parameters mentioned above have already been worked out. I want to try to understand what it is that’s going on, where the parameters are in the sketch and how do I change them.
    The Linux district I use has a text compiler called Geany. Will this work for me. It looks to me like it would but I’m not sure. As for math, it’s not one of my strongest attributes but I do have a fairly decent logical mind.
     
  5. Doug Gosha

    Doug Gosha TrainBoard Member

    3,595
    7,664
    80
    I learned C++ in 1998 because certain parties at work thought it would make the hearing aid test program run faster than it did programmed with Pascal which is what my cohort used to write it. We tried to explain that the hearing aid reaction time is what controlled the test time but, to no avail.

    So, I learned it and never used it. Oh well, the company paid for it.

    Doug
     
  6. lyncher

    lyncher TrainBoard Member

    16
    11
    14
    You say you want to control servos with an Arduino. That's a great goal. I'd recommend you look for tutorials that focus on Arduino programming and the Arduino IDE rather than generic C++ coding. Programming microcontrollers, and in particular working with the Arduino IDE, entails a unique set of skills and tools that I don't see emphasized in the tutorial you referenced. Not that these skills and tools are more difficult to master, in fact, I'd say it's just the opposite: you'll find it's an easier path if you focus on Arduino-specific learning aids. You need something that combines coding with the Arduino hardware.

    The standard Arduino microcontrollers - those based on the AVR platform (which is the majority of Arduino boards) - all have relatively small addressable space. Well, small compared to, say, what your desktop or laptop has, or even small compared to the ARM platform that powers a Raspberry Pi. Which is to say, you typically don't (or even want) an Object-Oriented Programming approach when coding for the Arduino as it's just not necessary to manage the programs you'll be writing.

    But you can do plenty with a standard run-of-the-mill Arduino - I'm working on a control system myself and plan on powering 4 servos per Nano - the Nano will also manage 4 relays through some L293Ds and speak RS485 to a bunch of other Nanos doing similar tasks. None of what I'm doing requires C++ - it's all done in old-school C style code using the Arduino IDE.

    You can spend an entire career learning C++. I know that first-hand.

    Jeremy Blum's book Exploring Arduino can be found for less than $20 online - an excellent intro to the hardware and to coding. He covers servos. You definitely need something that covers the universe of Arduino hardware which his book does.

    One last thing: the online tutorial you referenced is too old if you're seriously interested in the latest iterations of C++ - you want at a minimum C11.
     
    Last edited: May 27, 2020
    Uncle peanut butter likes this.
  7. FlightRisk

    FlightRisk TrainBoard Member

    548
    237
    14
    Hi Lyncher. Does that mean you just volunteered to review our code on GitHub as we make changes? ;)
     
  8. lyncher

    lyncher TrainBoard Member

    16
    11
    14
    Ha!

    You'll probably want to talk to some of my old team members before suggesting I review someone else's code.

    Hey, just saying ...
     
    FlightRisk likes this.
  9. bremner

    bremner Staff Member

    6,268
    6,248
    106
    Don't look at me, I am a server guy. I can figure out what is broken....
     
  10. Uncle peanut butter

    Uncle peanut butter TrainBoard Member

    179
    238
    9
    FlightRisk likes this.
  11. Uncle peanut butter

    Uncle peanut butter TrainBoard Member

    179
    238
    9
    Uncle Peanut Butter is headin' to school!
    8BA14FE9-44DA-43ED-A99D-39FBF6CF5CC0.jpeg
     
    FlightRisk and Sumner like this.
  12. Todd Hackett

    Todd Hackett TrainBoard Member

    93
    201
    8
    Guys - there are IDE's out there that can deal directly with microprocessors. Studio 7 ( Microchip now, was AtMel ) is one. I have not connected my studio 7 to an Arduino as I don't use them. Studio allows me to write code ( ASM in my case, does C also ) and step through the program so I can see how bad I have screwed it up. www.avrfreaks.net has a bunch of people that are doing just that and is a great place to ask questions and test out new ideas.
     
    Uncle peanut butter likes this.
  13. S t e f a n

    S t e f a n TrainBoard Member

    167
    93
    6
    Todd, would you recommend Studio 7 over the arduino IDE?
     
  14. Todd Hackett

    Todd Hackett TrainBoard Member

    93
    201
    8
    Didn't realize that there was an IDE as I don't use arduino. I have found in some cases, another IDE ( or view of things ) sometimes get me out of a hole which helps. Guess that ima gonna have to find one and play with it. Does the arduino IDE allow ASM?
     
  15. S t e f a n

    S t e f a n TrainBoard Member

    167
    93
    6
    I assume you could link in routines written in assembler, but I think (definitely not an expert) the arduino IDE just allows to edit C.
     
  16. Todd Hackett

    Todd Hackett TrainBoard Member

    93
    201
    8
    Stefan - did some looking and you are right, C only for the arduino IDE. Studio 7 will work with arduino and that allows ASM, which works for me. Studio 7 also allows you to NOT use some of the arduino included libs. Consensus is some of them are bloated with a lot of overhead; digitalWrite is an example. Google "atmel studio 7 arduino". Hackster.io has a good article on this. I have not used or installed the arduino IDE so I do not know its capabilities. I started with Studio 4 way back when so I have pretty much figured out all the nuances that Studio throws at ya. Both 4 and 7 have large footprints and with that brings more 'stuff' that you can throw at your code while testing. I do not know yet if Studio 4 supports arduino; I suspect not. Still gonna have to get an arduino and play.
     
  17. FlightRisk

    FlightRisk TrainBoard Member

    548
    237
    14
    Not to throw another wrench into the works, but the DCC-EX team uses PlatformIO. So under the hood it is Visual Studio Code from Microsoft, the Atom Editor, and plugins that allow it have all the specifics for microcontrollers. I think it is easier to use PlatformIO, but there are some things in the Atmel Studio that is easier for low-level chip specific things.
     
  18. Todd Hackett

    Todd Hackett TrainBoard Member

    93
    201
    8
    Thanks FlightRisk. Had a look at PlatformIO and from what I have seen am gonna give it a go. What I really like about it is that it will run on my Linux boxes, whereas Studio does not. I seem to remember that AtMel started at one time or another to port Studio to Linux, don't know what happened with that. Ok, so it could be my OLD memory and I only think I remember that. Studio also runs on top of Visual Studio so I will have to look further into that for the Linux part.
     
  19. FlightRisk

    FlightRisk TrainBoard Member

    548
    237
    14
    I was a reluctant convert. Atani can tell you ;) But it was much easier to get up and running than I thought, and a really good editor. Why would anyone try to code without syntax highlighting, autocomplete, intellisense, "find all references", etc. The color coding in the file list on the left with letter and counters to know what has been changed, what has syntax errors, etc. is invaluable. Plus I can add libraries to the project, upload to the microcontroller, and use the serial monitor for debugging. My Halloween theme is a "killer" (pun intended). I got used to it and am keeping it. I thought about using a christmas color theme, but I don't want to get confused since I instantly know what item is what color now ;)
     

Share This Page