Python – What is a Program ?

Before beginning with learning Python programming we need to understand what is a Program (more specifically Computer Program).

A Program is a sequence of instructions that specifies how to perform a computation. Computation can be anything like – taking input from user and perform mathematical operations, solving for roots of an equation or playing a audio/video file.

For different programming languages, the specifics to do these tasks can be different. But few basic instructions(below) appear in just about every language.

INPUT : take input in the program (can be user input from keyboard, a file, some data from network or other device)

MATH : performing basic mathematics operations (addition/subtraction)

CONDITIONAL EXECUTION : check for certain conditions and taking appropriate action

REPETITION : perform some action repetitively (that’s what computers are known for…)

OUTPUT : displaying the final output of program on screen, saving in a file or sending over a network/ other device

That’s the basic outline of every program you will ever gonna write.

So in a nutshell PROGRAMMING is the process of breaking a large and complex task into smaller and smaller sub-tasks until sub-tasks are simple enough to be solved with the basic instructions given above.

 

An Introduction to Functions in Arduino | How to Make Funciton

Arduino hd image

An Arduino function is a block of code that has a name and a block of statements that are executed when the function is called. The functions void setup() and void loop() have already been discussed here.

Arduino hd image

Custom functions can be written to perform repetitive tasks and reduce clutter in a program. Functions are declared by first declaring the function type. This is the type of value to be returned by the function such as ‘int’ for an integer type function. If no value is to be returned the function type would be void. After type, declare the name given to the function and in parenthesis any parameters being passed to the function.

type functionName(parameters)
{
statements;
}

The following integer type function delayValue() is used to set a delay value in a program by reading the value of a potentiometer. It first declares a local variable v, sets v to the value of the potentiometer which gives a number between 0-1023, then divides that value by 4 for a final value between 0-255, and finally returns that value back to the main program.

int delayVal()
{
int v;                               // create temporary variable ‘v’
v  = analogRead(pot);    // read potentiometer value
v /= 4;                            // converts 0-1023 to 0-255
return v;                        // return final value
}

Curly braces
It define the beginning and end of function blocks and statement blocks such as the void loop() function and the for and if statements. type function() {   statements; }

An opening curly brace { must always be followed by a closing curly brace }. This is often referred to as the braces being balanced. Unbalanced braces can often lead to cryptic, impenetrable compiler errors that can sometimes be hard to track down in a large program.

Semicolon
A semicolon must be used to end a statement and separate elements of the program. A semicolon is also used to separate elements in a for loop.

int x = 13;   // declares variable ‘x’ as the integer 13

 

Block comments, or multi-line comments
These are areas of text ignored by the program and are used for large text descriptions of code or comments that help others understand parts of the program. They begin with /* and end with */ and can span multiple lines.

Single line comments 
They begin with // and end with the next line of code. Like block comments, they are ignored by the program and take no memory space.
Single line comments are often used after a valid statement to provide more information about what the statement accomplishes or to provide a future reminder.

Learn Simple Hello World Program

These are all the basic tools you need to create a function in Arduino.

The setup() and loop() Functions in Arduino Programming

arduino infinity official logo

As we have seen in Basic Structure for Arduino that there are 2 basic functions in an Arduino Program.

arduino infinity official logo

setup()

The setup() function is called once when your program starts. Use it to initialize pin modes, or begin serial. It must be included in a program even if there are no statements to run.

void setup()
{
pinMode(pin, OUTPUT);      // sets the ‘pin’ as output
}

 loop()

After calling the setup() function, the loop() function does precisely what its name suggests, and loops consecutively, allowing the program to change, respond, and control the Arduino board.

void loop()
{
digitalWrite(pin, HIGH);   // turns ‘pin’ on
delay(1000);               // pauses for one second
digitalWrite(pin, LOW);    // turns ‘pin’ off
delay(1000);               // pauses for one second
}

By using these 2 functions you can create any Arduino Program.

Basic Structure of Arduino Programming | Setup & Loop Function

Basic Structure of Arduino Programming | Setup & Loop Function

The basic structure of the Arduino programming language is predefined and fairly simple. It runs in at least two parts. These two required parts, or functions, enclose blocks of statements.

Arduino official Logo

This is Basic Structure of Every Arduino Program

void setup()
{
statements;
}void loop()
{
statements;
}

Where setup() is the preparation, loop() is the execution. Both functions are required for the program to work.

The setup function should follow the declaration of any variables at the very beginning of the program. It is the first function to run in the program, is run only once, and is used to set pinMode or initialize serial communication.

The loop function follows next and includes the code to be executed continuously – reading inputs, triggering outputs, etc. This function is the core of all Arduino programs and does the bulk of the work.

So this is the primitive or basic structure of an Arduino Program.