Introduction to Programming
Programming is the art of making a stupid computer to do intelligent things.
Programming is a process where you:
- Completely identify the problem
- Design a step-by-step solution to that problem
- Write out basic instructions to implement that solution
C is a mature programming language and is probably the most commonly used in the world.
For this reason we will use C. C is picky. As we proceed with this topic we will see that we have to be very careful when we type in our code.
C is a programming language. It is what’s called a “high level” language. This means that the instructions we give in C are at a higher level than a CPU can perform directly.
A compiler is a piece of software that converts the high-level C instructions into low level CPU instructions.
C can be used to program almost any type of computer (Windows, OSX, Linux, tablets, phones, GPS units, etc.). In this class, we will be programming microcontrollers. A microcontroller is a small computer built into a single chip.
A microcontroller has a processor, memory, input, and output. It does not have a power supply. We must supply power ourselves (5V regulator). Microcontrollers use very little power, are pretty cheap to buy, and can be programmed to do almost anything you can imagine.
Algorithms
Dictionary.com: Algorithm: a set of rules for solving a problem in a finite number of steps, as for finding the greatest common divisor.
Number Types (Data Types)
In the C programming language, there are default number types that can be used. The specifics of the number types can differ based on the system you are programming (microcontroller vs. PC vs. Android device). However, the basics are the same.
Boolean (or bit)
A Boolean number is one that can either be 0 or 1.Boolean numbers are used to store TRUE/FALSE, ON/OFF, YES/NO type information. In HI-TECH C, Boolean numbers are simply called “bits”, more on this later.
Integers
Integers can only be whole numbers.
For example:
-
3, 5, 12, 234234908098230923420 are integers.
-
3.1, 5.342, 23423409234.3209, 2.0 are NOT integers.
-
(2.0 is a little tricky, but understand that if you write 2.0 in a program, it will be interpreted as a non-integer even though there is nothing after the decimal point.)
Integers – Signed or Unsigned
-
A signed integer is one that can have a negative sign.
-
Unsigned integers can only be positive numbers.
Floating Point Numbers
Floating point numbers can have decimal places.
These number types can hold values like: 2.5, 3.1415926535, -4822905.990636 etc.
Floating point numbers are named as such for technical reasons.
Essentially, the numbers 0.250, 02.50, and 025.0 are all the same, but with the decimal place in a different spot. The decimal place can “float around”.
Floating point numbers can be positive or negative.
Floating point numbers have a limit to their accuracy.
The accuracy of these numbers depends on how much memory is used to store them.
This is determined by the hardware you are working on (microcontroller, windows PC, IOS device, etc).
Variables
Variables are locations in memory that are used to store information.
Variables can be used to store all sorts of things like:
-
Program settings
-
Results from a calculation
-
Readings from a sensor
When you create a variable in a C program, you must do at least two things.
-
You must say what number type your new variable will have.
-
You must give the variable a name.
Declaring a Variable
If you wish to reserve a piece of memory to use in your program, do the following:
unsigned int variableName = 13;
First, declare the data type. In this example I’ve made an unsigned int (value from 0-65,535).
Next, give your variable a name. Names must start with a letter or underscore, can have no spaces, and are cAsE sEnSiTiVe.
Finally, you can optionally give your variable a starting value. This is the number that will be saved in that variable when your program first starts up.
Assigning a Variable
To assign a number to a variable, use the = operator.
int myValue = 0; //declare the variable
//myValue and set it to 0
myValue = 6; //now it is 6
myValue = 2; //now it is 2
Another Example:
int valueA = 6; //declare the first variable
//and initialize it to 6
int valueB = 8; //declare the second variable
//and initialize it to 8
valueA = 12; //valueA is now 12
valueB = 23; //valueB is now 23
valueA = valueB; //valueA is now 23
You will notice that I only used the “int” part in the first line. That is because we only declare what type a variable is once. Afterwards, we can just directly use it. Once a variable is declared, it cannot be switched to a different data type.
Comments
You will have noticed the green text in the code examples. These are code comments. Code comments are ignored by the compiler. They are used for documenting your code.
-
They help you keep track of what you are doing by explaining your code in plain English.
-
They help other people understand your code when they are looking at it.
In C, single line comments are denoted by a double forward slash:
//This is a comment
Anything after the // is ignored. Anything before is looked at.
int variable = 0; //declare a variable
The code at the beginning of the line is included but the text after the // is ignored.
Multiple line comments are surrounded by /*
and */
Example:
/* This is a multiline comment.
All the text between the opening and closing
symbols is ignored by the compiler. */
Constants
Code can also contain CONSTANTS. Constants are values that cannot change as a program runs.
Examples of things you might want to save as constants:
-
Pi: 3.1415926535…
-
mm per inch: 25.4
-
speed of light in m/s: 300000000
To declare a constant, use the following line:
#define SPEED_OF_LIGHT 300000000
#define tells the compiler that this is a constant value that will not change.
The next part is the name of the constant. It must start with a letter (or underscore), contain no spaces, and is case sensitive. Typically constants are named in ALL CAPS. The last part is the value you are defining it at. NOTICE: No semicolon (;) at the end of #define statements.
Mixing the two:
#define PI 3.14159
float diameter = 32.0;
float circumference = diameter * PI;
Here we have a float variable, and it is being initialized as 32.0 * 3.14159.
How Constants Work:
What happens when you use a constant is that the compiler automatically goes through all your code and searches for the each constant (PI for example).
Every time it finds PI, it replaces it with the value you defined PI as.
So
float circumference = diameter * PI;
would be replaced with
float circumference = diameter * 3.14159;
before the code is sent to the chip.
Math:
Once we start saving values, we can perform math with them.
In the previous slides you saw that we had a line:
float circumference = diameter * PI;
In this case * means “times”.
The common mathematical operators are:
+ : plus
- : minus
/ : divide
* : multiply
% : modulus (remainder). Example: 10 % 3 = 1
because the remainder of 10 /3 is 1.
Integer Math:
Doing math with integers (no decimal places) has some important implications. Let’s do some examples with a variable called “number” which is a char (0-255).
char number = 200;
number = number + 60; //number is now 4
Number can only be from 0-255. If we make it bigger, it will roll over like an odometer.
The numbers will also roll under.
number = 0;
number = number – 10; //number is now 246
The same rules apply for the other integers (char, int, long int) both signed and unsigned. The only difference is the values at which the rollover and roll under occur.
number = 10/3; //number is now 3
When you perform division, only the integer part of the result is saved because it is an integer variable.
number = 499/100; //number is 4
No rounding occurs. The numbers after the decimal place are simply cut off.
This concludes our programming terms and understanding for now. We will continue to update this section as often as possible to further help clarify your understanding of programming and master the basics.