Easy The C Language tips

of Acoptex.com (Not selected)

The C Language

Many languages are used to program microcontrollers, from hard-core Assembly language to graphical programming languages like Flowcode. Arduino sits somewhere in between these two extremes and uses the C programming language. It does, however, wrap up the C language, hiding away some of the complexity. This makes it easy to get started. The C language is, in computing terms, an old and venerable language. It is well suited to programming the microcontroller because it was invented at a time when compared to today’s monsters, the typical computer was quite poorly endowed. C is an easy language to learn, yet compiles into efficient machine code that only takes a small amount of room in our limited Arduino memory.


Comments can be single-line comments that start after a // and continue to the end of the line, or they can be multiline comments that start with a /* and end some lines later with a */.

Variables and Data Types

boolean memory: 1byte range: true or false (0 or 1) 

char      memory: 1byte  range: –128 to +128 Used to represent an ASCII character code (e.g., A is represented as 65). Its negative numbers are not normally used.

byte      memory:1byte  range: 0 to 255

int         memory 2bytes range: –32,768 to +32,767

unsigned int memory 2bytes range:0 to 65,536 Can be used for extra precision where negative numbers are not needed. Use with caution, as arithmetic with ints may cause unexpected results.

long         memory: 4bytes range: –2,147,483,648 to 2,147,483,647 Needed only for representing very large numbers.

unsigned long memory 4bytes range: 0 to 4,294,967,295 See unsigned int.

float              memory: 4bytes range:  –3.4028235E+38 to + 3.4028235E+38

double           memory: 4bytes as float Normally, this would be eight bytes and higher precision than float with a greater range. However, on Arduino, it is the same as float.

Conditional Statements

Conditional statements are a means of making decisions in a sketch. For instance, your sketch may turn the LED on if the value of a temperature variable falls below a certain threshold.

if (condition){action;}

Condition: <, >, <=, >=, to see if two numbers are equal, you can use == and to test if they are not equal, you can use !=. You can also make complex conditions using what are called logical operators. The principal operators being && (and) and || (or).


When programmers talk of Strings, they are referring to a string of characters such as the much-used message “Hello World.” In the world of Arduino, there are a couple of situations where you might want to use Strings: when writing messages to an LCD display or sending back serial text data over the USB connection.Strings are created using the following syntax:

char* message = "Hello World";

The char* word indicates that the variable message is a pointer to a character.


Loops allow us to repeat a group of commands a certain number of times or until some condition is met. The for loop is a bit like a function that takes three arguments, although here, those arguments are separated by semicolons rather than the usual commas. This is just a quirk of the C language. The compiler will soon tell you when you get it wrong. The first thing in the parentheses after “for” is a variable declaration. This specifies a variable to be used as a counter variable and gives it an initial value—in this case, 0. The second part is a condition that must be true for us to stay in the loop. In this case, we will stay in the loop as long as “i” is less than 100, but as soon as “i” is 100 or more, we will stop doing the things inside the loop. The final part is what to do every time you have done all the things in the loop. In this case, that is increment “i” by 1 so that it can, after 100 trips around the loop, cease to be less than 100 and cause the loop to exit.

for (int i = 0; i < 100; i ++){action;}

Another way of looping in C is to use the while command. The same example shown previously could be accomplished using a while command, as shown here:

int i = 0;while (i < 100){action;i ++;}

The expression in parentheses after while must be true to stay in the loop. When it is no longer true, the sketch will continue running the commands after the final curly brace. The curly braces are used to bracket together a group of commands. In programming parlance, they are known as a block.


Arrays are a way of containing a list of values. The variables we have met so far have only contained a single value, usually an int. By contrast, an array contains a list of values, and you can access any one of those values by its position in the list. C, in common with the majority of programming languages, begins its index positions at 0 rather than 1. This means that the first element is actually element zero.

int durations[] = {100, 200, 300, 400, 500, 600}

Other projects of Acoptex.com
Medium Basics: Project 083w Sipeed Maixduino board - Using PlatformIO IDE of Acoptex.com in Sipeed Maixduino 08-08-2019
Medium Basics: Project 083e Sipeed Maixduino board - Uploading MaixPy of Acoptex.com in Sipeed Maixduino 04-08-2019
Medium Basics: Project 083f Sipeed Maixduino board - Using MycroPython of Acoptex.com in Sipeed Maixduino 04-08-2019

Published at 10-08-2017
Viewed: 1380 times