Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Arduino Basic Software

In this report we will get to know some basic Arduino codes in (C++ and C) languages that are
used to program an Arduino.
void setup () and void loop ():
void setup (): the code between this statement brackets will run only once as soon as
the program is downloaded to the Arduino. Usually, this statement is used to set the pins
and identify their main purpose
(Pin works as input or as an output).

void loop (): the code between this statement brackets will run repeatedly. As long as
the Arduino is working.

Note: the space before the void setup () statement is usually used for
including libraries and global codes declarations that can be used between
any of these two statements brackets. For example:
include <Servo.h>
int x;
pinMode (pin name , pin main purpose ): this command here is usually
used in the void setup to set the pin purpose (for input either an INPUT or 0 and for
output either OUTPUT or 1 ).

digitalWrite(pin name , pin state): this statement is used like an output for the
Arduino. For example, like turning a led on or off. The pin state here is either on with full
pin power or off with no power at all. there is not anything in between. This means that
we cannot control the voltage amount in this pin by using this statement. We use (HIGH
or 1) in pin state to turn the pin on and (LOW or 0) to turn it off.
delay() and delayMicroseconds() : these two are used for delaying the execution
of the code for an amount of time chose by the user :

delay () : is for milli seconds


delayMicroseconds (): is for micro seconds
for example : if we want to make a led blink we will turn it on for one second an turn it
off for another seconds ( 1 second = 1000 milli seconds ).

So this led will be on for one second and will be off for one second and keep blinking
because its in void loop().
digitalRead (pin name): this statement is used to read the digital value of data in
the pin like reading from a PIR sensor…etc., and the reading value is either ( 1 ) or ( 0 ).

analogWrite(pin name, operating value) : this statement is used with the


pwm pins that can generate a pulse. This means that we can control the amount of
voltage that we use in this pin. For example like controlling a led brightness.
And the value of operating is between ( 0 – 255 ).
analogRead(pin name): this statement is used with pins to read varying values
from the analog pin (A0,A1…etc.) which are used for reading analog values. Like reading
the values of sensor which will be between (0-1024).

Serial.begin(baud rate): this statement is used to turn on the serial monitor and
the serial plotter and to set the baud rate (Serial data transmission rate in bits per
second). Now we can use the serial monitor to print data in it or to read data from it.

Serial.print(data): this statement is used to print data in the Serial monitor and
Serial plotter.

Serial.println(data): by using (ln) the Serial monitor will move to the next line every
time it prints data (works like [endl] in C++)

Note: you can turn on the Serial monitor and the Serial plotter by using the tool menu.
You should also check that you have chose the right type of your Arduino and the com
that you are using.
You can also find the right com in here.

Using inputs with Serial monitor:


1- first, we will need a variable to store the print a sentence to ask for an input form
the user for example:
String Name; // a variable to store the input
Serial.println(“Enter your name“); // ask for an input from user
Serial.print(“name:”); // a place to print the input
2- now we will have to wait for the user to enter an input by using this code.
while( Serial.available()==0 ) { }

3- last but not least we will store the input in the variable and then print it So we
know what we are entering as input.
Name=Serial.readString();
Serial.println(Name);

Note: there are different types of inputs like int, float, char and strings.
integers : use Serial.parseInt();
float : use Serial.parseFloat();
String : use Serial.readString();
Char : use Serial.read();
pulseIn( pin name , state ) : The pulseIn function measures the time period of a
high or low pulse input signal. The State here represent which pulse to measure. The
state here is either HIGH or LOW.

--------------------------------------------------------------------------------

map (variable , from Low , from High , to Low , to High): this command is
used to transform variables from a set of values to another set of values.
For example like transforming from (0-1024) to (0-255).
Note:- if your eyes are hurt from the bright colors of the Arduino ide default
theme you can change that by downloading the Dark theme from the internet
to make your eyes comfortable.

then use it by following these steps:-


1- You need first to download the theme zip file from the internet.

2- Now you need to store the Dark theme zip in a file named theme (you
need to create a file and name it theme if it’s not already exist in that
section of Documents) in the Arduino section of the documents, or follow
this path:
My computer >> Document >> Arduino >> theme
3- We start Arduino ide and we will follow this path:
File >> preferences
4- Now we will choose the theme we want and restart the Arduino

You might also like