Inputs
In order to use a digital input we need to ask the question, is the input high or low? Is it a logic 1 or 0?
If it is a 1 then we execute some code, if it is a 0 then we execute some other code. So we are asking the question, if the input is a certain value then execute the code. Let us look at the C code for this If statement.
IF Statement
The if statement looks like this:
if (condition)
{
•
Code
•
}
As an example if x is equal to 2 then make y = 6 and z = 4 would be:
if (x==2)
{
y=6;
z=4;
}
NB. The expression x==2 means if x is equal to 2.
The expression x=2 in C code means make x=2.
If there is only one line of code, say y = 6, then a shorthand way of writing this without the brackets {} is
if (x==2) y=6;
If–Else ...
Get PIC Projects and Applications using C now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.