What is Programming?

programming

There are tons of cool Arduino projects out there. It's simply amazing what you can do with a small microcontroller and a bit of programming. But wait, lets stop here and talk about what programming actually is and why you should start with it.

Computers are just everywhere. They are standing on your desk, you have one in your pocket and you find them in your car as well as in things like timers and thermostats. The thing about computers is, they usually don't think for themselves. Someone has to tell them what to do. If you set a timer, it's already an act of programming. Obviously someone else has done the hard work for you beforehand, that's why it's so easy.

In general when we speak about programming and programmers, we think about somebody who is writing a computer program. You know, a nerd who sits inside a dark chamber somewhere in the basement continuously typing magic commands on his keyboard. That's the cliché we have about programmers. The reality couldn't be more different. It's not that dark and it's sadly not that magic. But hey, while I'm telling you all this you might think: Why should I bother about all this?

Why should I bother?

Programming is not that hard. You can do it. Don't look at what other people are making, start making your own stuff. Arduino is a great platform to start. Or to put it in the words of the Linux author Linus Torvalds:

Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program.

Linus Torvalds

In addition to that you will start to understand the world around you better. There is no magic involved and computers don't have a personality either. So please, stop yelling at them – nothing will change when you're doing that. There is just one more thing to add. When you're good at programming and love what you do, you might want to make your hobby a profession and become a software engineer. There are a lot of great jobs out there.

Programming Languages

Okay now with that settled, let's return into the basement and talk a bit more about the magic words. When programming we use special languages – programming languages. While they might be hard to learn in the beginning, they allow as to do a pretty cool thing. We can write code that can then be translated into something an actual computer can execute, by a software called compiler.
But why do we need this? Wouldn't it be easier when we could just talk to a computer and tell him what to do? No, we can't and here is why. Out natural language requires interpretation and as we all know it's not always clear what they mean if people say something. The words we say can be ambiguous. You would probably not want that in your car.

He brakes. I guess he wants me stop, but maybe ...

Your car, the day you died

Yeah, you wouldn't want that. That's why we use programming languages. Programming languages are well-defined.

What programming language should I use?

You might have already heard, there is not one programming language, there are hundreds of them. So why is this?
Programming languages do another cool thing. They are establishing principles to make things easier for us. The reason why we are not using assembly to write all our programs is, that we would not be very productive with that. For those who don't known, assembly is a low level language and was used in the early days of programming. It can almost be directly translated to machine code by using a simple table. The problem is, an assembly language is very specific and varies between different platforms. Assembly for an Arduino Uno is much different than assembly for your PC. You essentially need to learn a whole new language for each platform.
This is why programming languages like C have been developed. C allows you to write code and use it for multiple platform. Well you still have to use different executables, but at least you can use the same code. How is this possible? Do you remember what I said earlier about programming languages being translated into machine code by a compiler? Well, I think you have your answer. The compiler is doing the work to make our lives easier. This principle is called abstraction. We can write our code without thinking about how the actual machine code will look like. Different programming languages provide different levels of abstraction.

C and C++ allow you to control most of the platform specific stuff by yourself. The problem is, with great power comes great responsibility. While languages like C and C++ allow you to manage things like memory as you want to, you can make a lot of mistakes with that too and run into troubles later. Languages like Java or C# handle things like that for you and you don't need to worry. This is why they are used for a lot of desktop applications. You can go even further and not tell the computer what it should do but instead tell him how things should look like. This is exactly what description languages like HTML do. You tell the browser how you want your web page to look like and it is doing all the rendering stuff for you.

Okay I think you got it, different languages provide different levels of abstractions. But does that justify hundreds of languages? The answer is no it doesn't. There are lots of languages that roughly provide the same amount of abstraction. Sometime languages are specialized for certain areas, like R which is used primarily for statistics and calculations, and sometimes it is just about which style you prefer. Python for example forces you to correctly indent all your code, because it uses the indentation rather than curly braces and semicolons to determine which lines of code belong together. You may like that or not, the language creator has decided for you.

So what language should I use then?
In general, use the language that others use too in the area you're creating software for. The advantage is that you find plenty of information about how to use the language to solve certain problems. In this blog we are targeting the Arduino. An Arduino is typically programmed in C++. So you should learn C++ to use it.
The reason Arduino uses C++ is, because we need to have full control over the platform in order to the things we want to do (e.g. blinking an LED). But wait don't just buy a book 'How to learn C++ really fast?'. Not all features defined by C++ are used in Arduino Code. A lot of things we use in C++ when programming regular applications won't fit on an Arduino and in most cases we don't need them either.
Books on C++ often focus on how to write object-oriented programs. If you don't know what that means, don't worry you won't need that if you're not planning to write your own Arduino libraries. If you don't know what a library is, it doesn't matter we will get to that in a later tutorial. For now, it's enough to known that you should search for material specifically targeted to the Arduino and not C++ in general. The good thing is Arduino tries to make things as easy as possible and you don't have to worry about all the stuff you might have heard about C++.

How programs are created

I want to start this section by using a quote from Steve Jobs, the founder and former leader of Apple.

Everybody in this country should learn to program a computer, because it teaches you how to think.

Steve Jobs

What does he mean by saying 'it teaches you how to think'? Well, writing programs is not solely writing code. Actually it's not for the majority of the time. The process of creating a program consists of multiple steps:

  1. Define your goals
    The first thing you want to do when writing a program is to define what it should do. When developing a professional piece of software, we call this a requirement analysis. What are your requirements for the final piece of software. Do you just want to blink with an LED or do all sorts of fancy stuff?

    Man, I don't know yet. We will see where we will get in the end.

    You, while reading this lines

    For a beginner it's often hard to tell what you want and what you will be able to do. There is no problem with that. I encourage you to experiment with your Arduino as much as you can, but when you want to implement a project you should better think about what you want. The problem with undefined requirements is, you will not know if you are done or not. You might never finish your project if you don't know where to stop. You might lose yourself in thinking about all the other things you could possibly integrate into your project. Just set yourself a clear goal. You have the freedom to choose one yourself. For a commercial piece of software, it is the customer who decides what it should look like. Sometimes customers are unsure too about what they want, but that's a whole different story ...

  2. Think about how to solve the problem
    Once you've got a clear picture about what your project should look like, you can start solving the problem. Stop immediately, if you wanted to start writing code now. Programming is a lot more about problem solving than coding.
    You've set your goals already. Now start thinking about how you get there. Writing code is not thinking. Thinking is the thing you do with your brain.

    Why am I saying all this? A lot of people just start coding and later they have to do the whole thing again, because the code they have written does not fulfil the requirements. You should plan how you want to solve the problem first and then check if it matches your requirements. As a beginner you might not know what to do to get to your goals. This is the step where tutorials become handy. Someone else does the thinking for you and tells you what you need and what you should do. That's great for learning the basics and getting to know the programming language, but it doesn't help you if you have a unique problem. My suggestion is to look at tutorials and then think about how you would use what you just saw. Start experimenting and try new things. You can also combine different ideas into one. By doing so, you will improve step by step and eventually get to the point where you know how to implement your own projects from ground up.

  3. Define a program structure
    If you know how to solve your problem, the next step is to think how you structure your program. Which steps are executed in which order? What needs to be done in each step? What parts do you need multiple times or can possibly reuse form older projects? You should decompose your project into its building blocks. This way you get smaller block with a lot less complexity which makes writing the actual code a lot easier. At this point often a lot of fancy diagrams are drawn to show the project structure visually. If you have a very simple project, that might not be necessary the just go ahead.

  4. Start writing the actual code and try if it works
    You finally reached the point, where you can write your program. Go ahead and write down your earlier thoughts in a programming language, to allow the creation of a binary which can then be executed. If you have a bigger project you might want to try out the individual building blocks you've implemented separately to locate possible errors. Note that I wrote down 'try if it works', this is what is usually done in hobbyist projects. In more professional projects you create an extensive test plan and create lots of test case to check if all requirements are fulfilled and everything works as expected. That would probably overkill for your projects.

How to get started

Now you know how a program is created. But how to start?

  • Look at what others have done
    I have already said that earlier, but tutorials are a really good start. You can gather knowledge about how other people solve problems and implement their projects. Look at example code and try to understand what it does.
  • Learn the language
    Programming gets a lot easier if you know how to write code. You can read books or watch tutorials, that show you how to use the language.
  • Practice
    There is no magic way of doing things. As with every new language you learn, you won't get better if you don't use them. This is why you should start writing actual code even if you are still not a hundred percent sure about how to do it.
  • Be curious and learn from your mistakes
    Try out stuff your curious about. Learn step-by-step and maybe an idea will evolve into a project.

I will publish more tutorials and project ideas for you here on DevXplained. This series will get you started in the basics of Arduino programming. So that might be your place to start if you want to.

Do I need math?

A question that is asked a lot is 'Do I need math?'. The short answer is 'Yes, you do!'. There is math involved in programming and also in electronics, but that should not get you stopped. It's not that hard and the computer can do most of the things for you. For most tutorials here on DevXplained you need little to no math at all.

The long answer is, math is all over the place. Programming languages are defined in a mathematically exact form also known as grammar. This makes it possible for the compiler to understand your code and translate it into machine code. We also use algorithms in programming. These have been proven earlier by using math. While these things might be of little interest for you, there are other things where you need to use math. For example, you might need to use geometrical functions. You might have to calculate the correct timing for communicating with other components and last but not least there is physics. Yes, you need physics too. The moment physic gets involved is the moment you start to design your own circuits. But don't worry, I will explain that to you in separate tutorials once we need that.

Next Post