if statement,loop,PWM IN ARDUINO

 Arduino If statement 

if( ) statement checks for the condition, it executes a statement or a set of statements. 



The if ( ) statement is written as: 

if ( condition)

 { 

// include statements 

// if the condition is true 

// then performs the function or task specified inside the curly braces 

else

{

// include statements 

// if the condition is false

// then performs the function or task specified inside the curly braces 


}

Here, condition = It includes the boolean expressions, that can be true or false. 

The comparison operators can be used as a condition inside the parentheses.

 


Arduino for Loop 

The statements inside the curly brackets under for loop are executed repeatedly according to the specified condition. 

An increment counter in the for loop is used to increment or decrement the loop repetitions. 

The for statement is commonly used for repetitive task or operation or to operate on the group of data/pins in combination with arrays. 

The syntax is: 

for (initialization; condition; increment) 

    \\ statements 

where, 

initialization: It is defined as the initialization of the variable. 

condition: The condition is tested on every execution. If the condition is true, it will execute the given task. The loop ends only when the condition becomes false. 

increment: It includes the increment operator, such as i + +, i - - , i + 1, etc. It is incremented each time until the condition remains true. 

For example, 1. for ( i = 0 ; i < 5 ; i + +) 

The above statement will execute the loop for five times. The values of i will be from 0 to 4. 

Note: 

If we do not want to execute the for loop again and again. Then, we can insert the for loop in the void setup( ) function.


Arduino PWM 

The PWM (Pulse Width Modulation) is a method of controlling the average voltage. 

It is a stream of voltage pulses that reduces the electric power supplied by the electrical signal. 

The effective voltage is controlled by the width of individual pulses in a stream of voltage pulses of a PWM signal. 

The common use of PWM pins includes controlling LEDs and DC Motors. 

The PWM in LED controls the frequency of the light. It means the LED will be ON/OFF at a frequency detectable by our eyes. 

Principle of PWM 

The state of the Digital Input/Output pins in Arduino is either HIGH ( 1 ) or LOW ( 0). 

Here, HIGH means the voltage is approx to 5V. 

LOW means the voltage is equivalent to 0 volts. 

The PWM is a square wave signal, which is represented as: 

The duty cycle of the rectangular pulse is shown below: 





Here, to: It is the duration of the signal when the signal is HIGH. 

tc: It is the total duration of the signal as the sum of HIGH and LOW. 

Duty cycle of a PWM wave As defined above, the duty cycle is the ratio of the pulse width to the total width of a signal. 


0 Comments