If Command in Arduino

Let's Sketch Yesterday modification program becomes as follows:
If Command in Arduino

Sketch  Modification timeDelay
 // www.ioisalman.com
   // coder Salman

   // Pin 8 for LED
  const int pinLED = 8;
 void setup() {
  // pin LED as output
 pinMode(pinLED, OUTPUT);
}

 // The start time delay 1000 | 1 Second
 int timeDelay = 1000;
  void loop() {
  // each looping, value timeDelay was reduced in 100
 timeDelay = timeDelay - 100; 

 }
 

/*if the time delay is 0 or negative ,
The value  f time delay is reset to 1000
/*

if(timeDelay <= 0){
timeDelay = 1000;
}

//Turning on and off led over time delay
digitalWrite(pinLED, HIGH); 
delay(timeDelay); 
digitalWrite(pinLED, LOW);
delay(timeDelay); 
}

If you can imagine what will happen with the LED? The LED flashes slowly initially, in the long blinks rapidly, and will eventually flashes slowly again. Would you believe? If not, please try. 

When the initial burn it, then timeDelay is 1000. This value is initialized on line 14. This line is not used as a constant (const) as pinLED because timeDelay value will be changed.
int timeDelay = 1000; 
Upon entry to the main part of the application, on line 18 timeDelay value minus 100.
timeDelay = timeDelay - 100;
So timeDelay first used to turn on and turn off the LED is 900. By looping the next, timeDelay back minus 100, so looping into two using timeDelay 800 (shorter than timeDelay the beginning), and so on until the timeDelay 100.

At the time passes timeDelay 100, when it was reduced by 100, then timeDelay worth 0. This condition matches the line to 23,
if(timeDelay <= 0){
timeDelay = 1000; } 
 
If (IF) timeDelay less than or equal to 0, then timeDelay will be filled by 1000. So the value timeDelay never be negative and it will be repeated continuously. This is what makes the duration of the flame alive-die LEDs can be changed more quickly.

Some mathematical operators are required in the programming language:


OperatorMeaning
=Operator assignment, to give value to a variable
+operator additions
-operator reduction
*multiplication operator
/ Operator pembagian. Sebagai catatan:
- If the type of data used is integer (int),
then the quotient is a real value, not decimal. Example
5/2 = 2, not 2.5 or 3. But if the data type used is double / float, then the quotient is a decimal number. For example, 5/2 = 2.5
%
Modulo operator (remainder of the division). Example:
- 10% 2 = 0, 10 divided by 2 = 5 + 0
- 10% 3 = 1, 10 divided by 3 = 3 + 1
- 10% 4 = 2, 10 divided by 4 = 8 + 2
- 10% 5 = 0, 10 divided by 5 = 2 + 0

IF command would definitely followed by the condition is True enclosed by parentheses, if (condition). Sketch 2.1 above on the conditions used is timeDelay <= 0, timeDelay less than or equal to zero. That is, if timeDelay value is 0 or less than 0, then the code in the if block is executed.

In addition to the <=, the following are some of the operators are often used:

OperatorMeaning
==Equal to
!=Not equal to
<Smaller
>Larger
<=Less than or equal to
>=Greater than or equal to

Yep, that's how the IF and some operators that can be used to check the condition of the IF. Simple is not it?

Subscribe to receive free email updates:

0 Response to "If Command in Arduino"

Post a Comment