Date: May 14, 2007
My son has seldom seen a C program before. This is some info for him to start with. Not much but enough to get him started.
It is just like our Chinese dialects. We have Mandarin, Cantonese, Hokkien, Teochew, Hakka, etc etc. When two person talks, they use these different languages or dialects to communicate.
Programming language works the same way. It has many many different kind of languages too. We have C language, we have Python scripting language, C++, Scratch programming, Swift programming, etc etc. The purpose of the programming languages is to write instruction in a some what plain english, easy to follow languages, and then, the compiler (computer programming program) will compile and change it to machine languages that the micro-processor such as Arduino and Raspberry Pi can understand. Which is 1-0-1-0-1-0 binary form.
C programming language is one of the earliest languages that all the programmers of my generations knows. Other similar languages includes Pascal, BASIC, C++.
This is a sample program from C. As you notice, every C programs must have a main() function, and the compiler will execute from there.
AS you can guess, this program will print a “Hello World” on your monitor text terminal screen.
Arduino uses C Programming
Every Arduino .ino file has only setup() and loop() function. There is no main() function. And Arduino does not need a .c file.
So, Arduino codes is not a C Program?
The answer is Yes and No.
Arduino programming is a variation of C Programming. It follows almost 99% of C programming language syntax and structures.
The only different is it does not need a main() function.
Arduino programming must have a setup() function which only run once in the Arduino executable program & a loop() function which implied by its name that the Arduino executable program will run this loop over and over again. It is automatic. It is kind of a way that Arduino programming were made simple because of this.
u8glib or U8g2 OLED display Library
Let’s look at the code closely.
For our example here, U8g2lib.h is the header files for U8g2 OLED display library.
A header files contains all the constance declaration, as well as functions declarations for all the library functions for displaying things on the OLED display.
In the setup() function, we only run u8g2.begin() to initialise the OLED display. This function is only run once.
In the loop() function block, you always need to write this to draw or print text onto the OLED screen.
You can simply write the loop() function this way. But a good program has a good practice, and they always put similar lines of codes into functions so that those codes with a purpose get to be used again and again without rewriting it.
So, you can write a function call draw(), and your loop() becomes simplify. The reason you want to separate out a draw() function is because you might use the draw() function more than 1 time. You can pass in any number of arguments into the function. In our case, we don’t pass any arguments, so, we put “void” in the bracket. Your function can return any value too. But in our case, we return “void” means, the function returns nothing.
In order to draw anything, you will need to redraw that thing all the time. That is why, the loop() function always will draw the things you need. So, just remember, if you need to print something onto the OLED, you will need to use the u8g2.begin() to initialise the display in setup(), and run an extra loop for the pair of u8g2.firstPage() & u8g2.nextPage() functions as follow. If you don’t write exactly as below, your draw function will not properly displayed on the OLED screen.
// do something here, like draw()
} while (u8g2.nextPage());
Compile the program, and upload to your Arduino UNO, then, your Arduino will print “Hello World” on the OLED screen.
I daisy chain all my OLED together to the Arduino, so all will display the same thing.
A variable is a place holder to remember things for you. It can be global or local. If it is a global variable, you declare outside of any functions, and this variable can be read by any one any where. If it is local to that particular function, you declare it within that function, and this local variable can only be used within that function. Once you are outside that function, it forgets.
Let’s declare two global integer variable, with “int”. We initialise the values to zero. Then, we increase the first counter within the do while loop, and we increase the second counter within the loop() function but outside of the do while loop.
Let’s try to print these counter values.
Here is another way of printing text.
u8g2.print(“Your Text or value here”)
This print function is cool. Because you can pass a “text” for it to print or you can simply pass the integer value for it to print, and it will take care of the type conversion. ie. if the value is integer, it will change that integer into a String value (text) so that it can print correctly.
Let’s compile it and upload it to the Arduino.
And it does exactly what you tell it to do. It prints the counter for both the counter1 and counter2.
So, the second counter is slower. That means, the do while loop got executed more times. That is because, the Arduino needs time to sent the graphics (i.e. text) to the display to allow it to display.
EXPLORE! Now, if you let it run forever, the integer has a finite number, and it will get reset back to zero. And you will end up to have counter2 larger than counter1 until counter2 get reset again or overtaken.
Let’s put a delay(1000) there to simply wait 1 second (1000ms) every time it call the draw function. And see what happen?
Well, here’s the video. As you can see, some of the numbers are drawn halfway, and the loop() counter is still zero. Until the nextPage() is done, i.e. finish drawing, the loop() counter increase to 1.
If you are a beginner to C programming or any programming language, you can use your imagination to try to figure things out. Machine is machine, it will follow what you tell it to do. Instead of blindly copy the programs, may be it is a good thing to learn why and how he wrote it.
It is extra satisfy when you find out these findings.
A beginner need not to learn C program immediately.
As said before, all he needs to do is explore. Explore means, you cut and paste the code in there, and run it. Look at the way how people write the codes, and then, remember next time you will need to follow same style of writing the codes.
Exploring the codes is fun. It makes you see, and think. And then, you go back to your code and trace each line of codes and figure out what it behave that way.
This lesson emphasis the codes to make the display work. It also show the basic troubleshooting skills. If you are not sure of anything, just print the values or things on the screen to see if the program does what you want it to do.
So, now you have a screen, you learn how to print the text, and you learn how to do basic troubleshooting by printing something on the screens to see the counters, the value, or status, etc. You are good to go to the next stage.