Arduino Programming Language.

What is the Arduino programming language?

  • The Arduino code is written in C++, with a few extra methods and functions that we'll go through later. 
  • C++ is a computer language that is easy to understand. 
  • A'sketch' (the term given to Arduino code files) is processed and compiled to machine language when you produce it.
  • The Arduino IDE must be downloaded and installed before you can begin programming the device. 
  • The Arduino IDE is available for Windows, Mac OS X, and Linux. 
  • Download the Arduino software from the official website (depending on your operating system) and follow the installation instructions.
  • The Arduino program has a straightforward structure. A minimum of two blocks are required in Arduino programming.
Preparation and Execution
  • Curly braces contain a collection of statements in each block:

The Arduino IDE is a program that allows you to program an Arduino.

  • The main text editing application for Arduino programming is the Arduino Integrated Development Environment (IDE). 
  • It's here that you'll write your code before uploading it to the board you're programming. The Arduino code is also known as sketches.
Arduino Program Format.

void setup( )
{
statements-1;
.
.
.
statement-n;
}
void loop ( )
{
statement-1;
.
.
.
statement-n;
}
  • The setup block is setup (), while the execution block is loop ().
  • When the program is run, the setup function is the first one to run, and it is only called once. 
  • The setup function is used to start serial connection and initialize the pin modes. Even if there are no statements to execute, this function must be included.
void setup ( )
{
pinMode (pin-number, OUTPUT); // set the ‘pin-number’ as output.
pinMode (pin-number, INPUT); // set the ‘pin-number’ as output.
}

  • After the setup () function has completed the execution block runs . Reading inputs, triggering outputs, verifying conditions, and so on are all part of the execution block.
  • The loop () function is a component of the execution block in the sample above. The loop() function, as the name implies, repeats the sequence of statements (wrapped in curly brackets).
Void loop ( )
{
digitalWrite (pin-number,HIGH); // turns ON the component connected to ‘pin-number’
delay (1000); // wait for 1 sec
digitalWrite (pin-number, LOW); // turns OFF the component connected to ‘pin-number’
delay (1000); //wait for 1 sec
}

  • Note that Arduino always measures time in milliseconds. As a result, whenever the delay is mentioned, retain it in milliseconds.
  • Let's take a big step forward and try some Arduino experiments.
  1. Blinking the LED.
  2. Fade-in and fade-out the LED.
Components required:
  1. Arduino UNO R3 -1
  2. Breadboard -1
  3. Breadboard connectors -3
  4. LED -1
  5. 1K resistor -1
1. Blinking LED

Steps for this experiment.

Step-1: 
  • Connect the Arduino to the Windows / Mac / Linux system via a USB cable.
Step-2: 
  • Connect the 13th digital pin of Arduino to the positive power rail of the breadboard and GND to the negative.
Step-3: 
  • Connect the positive power rail to the terminal strip via a 1K ohm resistor.
Step-4: 
  • Fix the LED to the ports below the resistor connection in the terminal strip

Step-5: 
  • Close the circuit by connecting the cathode (the short chord) of the LED to the negative power strip of the breadboard.
Blinking LED Connection


Arduino program for LED blinking (Example 1).

int LED =13; // The digital pin to which the LED is connected

void setup ( )
{
pinMode (LED, OUTPUT); //Declaring pin 13 as output pin
}
void loop( ) // The loop function runs again and again
{
digitalWrite (LED, HIGH); //Turn ON the LED
delay(1000); //Wait for 1 sec
digitalRead (LED, LOW); // Turn off the LED
delay(1000); // Wait for 1 sec
}

Arduino program for LED blink (Example 2).

void setup ( )
{
pinMode (13, OUTPUT); //pin 13 is set as output pin
}
void loop( ) // The loop function runs again and again
{
digitalWrite (13,HIGH); // Turn ON the LED on pin 13
delay (1000); //Wait for 1 sec
digitalWrite (13, LOW); //Turn OFF the LED on pin 13
}

2. Fade-in and fade-out the LED
  • The LED bling experiment follows the same steps as the breadboard circuit, with the exception that in step 2, you connect the 9th digital pin to the breadboard's positive power rail.
Fade-in and fade-out the LED

Arduino program for LED fade-in and fade-out (Example 1).

int led = 9; // The digital pin to which the LED is connected
int brightness = 0; // Brightness of LED is initially set to 0
int fade = 5; // By how many points the LED should fade
void setup()
{
pinMode(led, OUTPUT); //pin 10 is set as output pin
}
void loop() // The loop function runs again and again
{
analogWrite(led, brightness); // set the brightness of LED
brightness = brightness + fade; //Increase the brightness of LED by 5 points
if (brightness <= 0 || brightness >= 255) // check the level of brightness
{
fade = -fade;
}
delay(30); // Wait for 30 milliseconds
}

Arduino program for LED fade-in and fade-out (Example 2)

int led=19; // The digital pin to which the LED is connected
void setup()
{
pinMode(led, OUTPUT); //pin 10 is set as output pin
}
void loop() // The loop function runs again and again
{
for (int fade=0; fade<=255; fade=fade+5)
{
analogWrite (led, fade); // Change the brightness of LED by 5 points
delay (30);
}
}

Post a Comment

Previous Post Next Post