Keyboard Shortcuts
ctrl + shift + ? :
Show all keyboard shortcuts
ctrl + g :
Navigate to a group
ctrl + shift + f :
Find
ctrl + / :
Quick actions
esc to dismiss
Likes
Search
Re: Basic Arduino Programming Knowledge
Jack Purdum
There are a lot of C books out there and many that are aimed specifically at the Arduino population. Since you are working from a non-beginner programming background, you may want to skip an intro text. That said, neither Pascal, SQL, or even Basic and Java, directly make pointers available in the language. That's one of the reasons my intro book has two chapters on pointers, as they are a really fundamental element in C and a common stumbling block for many students.? Now, frame that within the context of the Arduino. There are "tricks" that you can use to make the code smaller (very important on an Arduino), faster, or both. For example: ? ?for (int i = 0; i < 100; i++) { ? ? ? ? ? ? ? ? ? ? ? ?// This is ok...it works ? ? ? myArray[i] = 0; ? ?} is commonly used to zero out all elements of a 100 element array. A better way to write it is: ? ?for (int i = 0; i < sizeof(myArray) / sizeof(myArray[0]); i++) { ? ? ? ?// This is better...more scaleable and robust ? ? ? myArray[i] = 0; ? ?} #define ARRAYELEMENTS(x) (sizeof(x) / sizeof(x[0]) // ...more code... ? ?for (int i = 0; i <?ARRAYELEMENTS(myArray); i++) { ? ? ? ?// Better yet...more easily read ? ? ? myArray[i] = 0; ? ?} This is better because it's a little easier to understand. Still, I would replace the for loop with: ? ?memset(0, myArray,?ARRAYELEMENTS(myArray)); ? ? ? ?// Bingo! One line of code... and be done with it. The for loop is no longer needed.?memset() is a System V standard library routine that is an inherent part of the Arduino's underlying Gnu C++ compiler. It's usually written in hand-tweaked assembler and is going to be as fast and small as it can be. A lot of new programmers don't even know about it.? There are many places in the book where I go through this kind of iterative refinement process. You start with code that works, but doesn't take advantage of the language. I call this RDC...Really Dumb Code. Then I show a refinement...SDC...Sorta Dumb Code. Then the final version, which I don't have a name for 'cuz that's what you should be writing in the first place. I also warn about Flat Forehead mistakes...mistakes where you slam the heel of your hand into your forehead once you find the bug and ask yourself "How could I be so dumb?" There is a pool of common FFM's that you should expect to make, but it helps to have them pointed out so you can find them more easily. Most good C programmers have foreheads that look like a pool table. Finally, because almost all of the libraries are written in C++, the 2nd edition of my Beginning C book has a chapter titled A Gentle Introduction to Object Oriented Programming and C++. The purpose is not to teach OOP or C++, but rather give you enough to understand what you're reading if you look inside a C++ library file. Now, are these iterations "tricks" or just refinements that an experienced C programmer would make? I don't know the answer. On the one hand, I fear telling you to buy Beginning C for Arduino might disappoint because it is aimed at the beginner. On the other hand, my writing is based on 40 years of using C and teaching it for almost as long. Unless you've stood in front of 150 sets of eyeballs, with 130 of them looking like a deer in the headlights, you probably haven't had to grapple with developing teaching methods and examples that work. Truthfully, there are a bazillion programmers who can code circles around me, but with little modesty I feel that I can say you'd be hard-pressed to find a handful who would be a better teacher, mainly because of my classroom experience. Also, I had a software company for 17 years that sold programming tools, including a C compiler we developed, so not all of that experience is just in teaching. Go to Amazon, look through the Table of Contents (i.e., click on the Look Inside banner on the cover) to get a feel for what's in the book. Then read the reviews. (The 2nd edition has fewer reviews, but is a better book.) Then do the same for the other books mentioned here and then make a decision.? I just wasted a ton of bandwidth to say: Look around, read some reviews, and then select a book you think fits your background. Jack, W8TEE From: Helmut OE4HDS <hs@...> To: [email protected] Sent: Sunday, February 26, 2017 1:51 PM Subject: Re: [BITX20] Basic Arduino Programming Knowledge Jack, what would you recommend as a resource for learning programming an Arduino for someone who knows programming principal, but does not know the specific things for Arduino? I have 3 decades of programming experience but 95% is Pascal and SQL (I do this for a living). Very basic C knowledge though. When I look at the Raduino sketch, I understand what's going on and I am able to make some changes, but I miss some knowledge of the basic concept to make a sketch from scratch that goes beyond "hello world".
|
to navigate to use esc to dismiss