Basics of Programming
We will be really quick in this tutorial. Let’s start programming by learning basics of programming for hardware. Programming concepts are same in basic computer programming and programming hardware. The only difference is in fact that for hardware programming we use same programming concepts and apply them along with interfacing of hardware pins for input and output.
Significance of Computers
Computers are the dumbest invention of human kind. We make them smart by programming them. Programming is more like putting instructions on them which make them smart. Either it’s a basic program that make a computer do different asks repetitively or we write artificial intelligence program which enable computer to take decision smartly and learn with previous instruction but still we need to make this smart program that enable computer to think intelligently. Then what are computers best at? Computers are insanely fast. They can process information at much faster rate than a human mind. Want to test? Multiply 3324 with 4536. Struck? Huh?
See how fast computer does it 😛
Before going forward let’s ponder upon different things. Whenever we need to talk to someone who speaks English we have to change our medium of talking to English, likewise if we talk to a Chinese we need to switch our medium of talking to Chinese. This means we need to learn a new language to communicate with respective person. Similarly, to talk to a computer or hardware devices we need to learn different language which help us in instructing them. These languages are known with such names C++, Python, Java etc. these are just three examples, there are thousands of such languages out there.
What are basic programming concepts?
Programming comprises of four core elements.
- Classes & Objects
Memory/Variable: Whenever we make a computer software, mobile application, a robot or a gadget. First thing we need is a temporary memory element or a constant storage to store information to run the program or to save some important information that we will need while running a program. We refer these memory elements as ‘Variables’ which are then also divided into two basic types; integers and characters. There are more types but these are the general ones.
Continuity: Whenever we make a program or application or robot we need to operate it continuously. Have you ever seen such a software which you open it closes itself (ignore the case when you have a faulty application or virus)? Obviously NO, this means we need continuity while running of our application for different functions or processes. We refer this continuity in programming terms as ‘Loops’
Conditions: We also need conditions in our programs and application to interrupt continuity and give it different functions. For example, we consider ‘close window’ button as condition to interrupt continuous running of program and condition is like if this button is press stop all task and close the window. Conditions are also needed to take certain decisions in any software, device, robot or gadget. Conditions are known with their name itself in programming terms.
Functions: Functions are fundamental part of programming. We tend to make functions of repetitive task in our program or device. For example, if we are showing an error again and again in a program instead of writing its code at every place in code where ever we need it. We tend to make a function something like my_error and call it at every place we need it. There are few pre-built functions in different programming languages to make process easier or to hide it in black box as one-word task.
We also have parameters in functions as well which can be different definitions of functions or how to use function under what conditions etc.
Significance of functions: Functions are of great significance we can understand them better by putting them in different use-cases. If I ask you to write ‘His name is Arduino’ on a piece of paper. You will possibly pick up and pen & paper and start writing this line. What you need to observe in this is the fact that for even a split of a second you didn’t think how to hold a pen or what writing is? Or how do we write? Is writing a dish to eat? Or how do we write different alphabets. All these procedures have closed in black box and are now available in our brain as functions. So, whenever we refer write we just start writing. While writing parameters can be what language we need to write in what orientation etc.
Exercise: Consider we make a robot that can walk from one point in room to a wall it can see. How will we break this task in programming terms?
Suppose: Eyes are distance sensor that can see how far are we from the wall. Legs are the actuators that can help us walk.
Program: A typical program for this task will be, Measure distance till wall and store it in your memory element named ‘wall_distance’ that how far it is. Then we will call a function ‘walk’ and run it continuously under a condition. We will walk while measuring distance from wall at every instance and condition will be like ‘walk until your current distance from wall is zero’.
Fundamentals of Programming:
A program or code needs to be logically correct or there should be a logical sequence of it to make sense. This logical sequence is referred as ‘algorithm’ which is flow of code for specific application along with logical sense.
For Example: If we want to make algorithm of ‘How to make a cup of tea?’. A typical process will be like,
- Take a sauce pan and pour water into it
- Heat water to a specific temperature on stove
- Pour water into cup and dip a tea bag into cup
- Add tea whitener and sugar to taste
- And… its done
This is a logically correct sequence of algorithm to make a cup of tea. What if I do this;
- Boil water and drink it.
- Then eat tea whitener and sugar
- Now put a tea bag in your mouth and start jumping
- Tea is ready 😀
Is this a right program? No its not.
*One more thing the writing our breaking down of code into logical and sequential steps is known as pseudo code which we write on paper in easy English language to make an algorithm before actually coding it as computer program.
We will learn all of them in depth going along making different things.