Beginning C for Arduino |
Author: Jack Purdum Working with the Arduino is usually seen as a hardware problem, but what about the software? This book aims to educate you about the C language as used by the Arduino.
The programming language used by the Arduino is essentially C with a bit of extra structure provided by the IDE. However, not many Arduino programmers actually sit down and learn the language before starting on sometimes major projects. They expect to pick it up as they go along. This often results in really bad programming in otherwise excellent hardware projects. What to do about it? Obvious - learn C. This particular book starts with a look at how to install the Arduino IDE and then quickly moves on to consider the C language. If you want a book that provides cookbook style solutions to writing software that works with hardware in various ways then this isn't the book for you. For a book on the Arduino it really does ignore the hardware to a surprising extent. You need to be very clear that the topic of this book really is the C language and only obliquely anything about the Arduino.
This is a very traditional and almost academic book. It isn't quite as extreme in this respect as Kernighan and Ritchie's (K&R) classic, which is an all time favorite of mine. It is almost as if the author is taking K&R as his role model - and this isn't a suitable structure for the beginner. The approach taken by this particular book is sometime odd. For example, at the start of chapter 2 we have the assertion that all programming languages are built from four basic elements:
This isn't exactly wrong, but it is a statement about the granularity or modularity of code which is an entirely optional extra. Not all languages have statement blocks and some don't have functions. The key components of any programming language is some way to write conditionals and loops, which is not an idea that is emphasized. But starting with expressions is one way to do it. However, jumping in almost immediately with operator precedence doesn't really make it easier for the complete beginner. The final part of the chapter attempts to explain the five program steps which are - initialization, input, process, output and termination. This again isn't wrong, but it might well confuse a beginner working with the Arduino or any embedded hardware because such programs are usually in the form of a loop, often an infinite loop. Indeed one of the slight complications of the Arduino form of C is that there is a loop function that you define to determine what goes on in the processing loop. Notice that there is no explicit loop construct, just a function that is called repeatedly. Chapter 3 is about data types and it just provides a list of the data types supported and a few short explanations. If you are working with embedded processors then what usually matters is data type conversion and for this you need a good understanding of binary, which the book makes little attempt to provide. It then goes on to explain null terminated strings, then arrays and lvalues and rvalues. Arrays are just mentioned in passing - they are covered after pointers so that the link between array indexing and pointer arithmetic can be made clear. Personally I'd prefer to see a good grasp of arrays, without pointers, to be established as early as possible.
Chapters 4 and 5 bring us to the flow of control where we learn to write conditionals and loops. The conditional is motivated and explained quite well, but a lot of additional ideas are thrown in along the way - as if conditionals aren't hard enough for a beginner to grasp. When it comes to loops it is much harder to see the principles involved because of the details and in particular because the general form of the for loop is explained from the word go. For beginners it is much better to introduce the for loop as an enumeration loop and only expand its behavior much later. Interestingly there is a big warning about using the goto, but without any explanation of why using it is such a bad idea - it is simply banned. It is so much better to use it to explain the motivation of writing good clear non-spaghetti code. Also, soon afterwards. break and continue statements are introduced but in this case no such warning about the need to use them sparingly is given even though they do violence to the flow of control in a similar way to a goto. Chapter 6 introduces functions and it does a good job, but again the level changes make it more difficult than it need be. Why, for example, cover the behavior of the stack in a section on pass by value. Yes, it's nice to know about the stack, but it can wait until the reader has had time to settle down with functions and all the other difficult ideas. Chapter 7 introduces the idea of scope and storage classes. Chapters 8 and 9 moves on to pointers and arrays. Chapter 10 is on structures, including unions and finishes with a look at how to use EEPROM memory to store such data. Finally Chapter 11 explains the C preprocessor and bitwise operations and the book comes to a close with an examination of some libraries. I'm not sure why bit operations are included with the C preprocessor but they are. Overall I didn't like this book. It was too much a mix of the formal and informal. It didn't stay with a formal explanation of the C language in the style of K&R and yet you could feel that it was basically organized in the same way. Equally the informal language too often gave way to an explanation of some academic sounding topic such as lvalue or rvalue. The book dabbles in explaining what good coding should be, but doesn't really do it justice. It also angles the approach towards the Arduino, but again doesn't really emphasize the low-level ideas such as bit manipulation and bit-oriented data structures but neither does it take a completely high-level abstract approach. Of course, the fact that I didn't like it doesn't mean that you won't, but my final warning is that none of the examples rise above the obvious. Occasionally you also have to wonder why the code is as it is. For example, why is there an infinite loop within the loop function on page 86? Why are custom functions being used when they haven't yet been introduced and so on.. I would urge you to learn to program in C, and if this book manages it for you then great, but for me it isn't the best.
|
|||
Last Updated ( Saturday, 18 May 2013 ) |