Week 3 - Arduino Programming
Week 3 - Arduino Programming
void setup() {
//setup code
}
void loop() {
//looping code
}
setup()
void setup {
pinMode(pin,OUTPUT); //instantiates that pin as OUTPUT
}
loop()
• After creating a setup() function, which initializes and sets the initial
values, the loop() function does precisely what its name suggests, and
loops consecutively, allowing your program to change and respond.
Use it to actively control the Arduino board.
loop()
• loop() runs endlessly once setup is done.
void loop(){
digitalWrite(pin,HIGH); // +5V to pin
outputs
delay(1000); //pauses for a second
digitalWrite(pin,LOW); // 0V to pin
outputs
delay(1000); //pauses for a second
//loops
}
{ }
• Single line comments begin with // and end with the next line
of code. Like block comments, they are ignored by the
program and take no memory space.
/* ... */
• These constants define pin levels as HIGH or LOW and are used
when reading or writing to digital pins. HIGH is defined as logic
level 1, ON, or 5 volts while LOW is logic level 0, OFF, or 0 volts.
TRUE / FALSE
• These are Boolean constants that define logic levels. FALSE is easily
defined as 0 (zero) while TRUE is often defined as 1, but can also be
anything else except zero. So in a Boolean sense, -1, 2, and -200 are all
also defined as TRUE.
INPUT / OUTPUT
Assignment = Stores the value to the right of the equal sign in the 20
variable to the left of the equal sign.
Addition + Adds two operands 30
not equal to != True when both values are the same TRUE
less than < True when left operand has lesser value than right FALSE
greater than > True when left operand has greater than right TRUE
less than or equal True when left operand is less than or equal to the
<= FALSE
to right.
greater than or True when left operand is greater than or equal to
>= TRUE
equal to the right
A = 20 ; B = 10;
Name Symbol Description Result when is A the first
term of operation
compound multiplication *=
if { ... }
• if statements test whether a certain condition has been reached, such
as an analog value being above a certain number, and executes any
statements inside the brackets if the statement is true. If false the
program skips over the statement.
if(someVar ?? someValue){
someAction();
}
if { ... } else {...}
• if… else allows for ‘either-or’ decisions to be made. For example, if you
wanted to test a digital input, and do one thing if the input went HIGH
or instead do another thing if the input was LOW
if(someVar ?? someValue){
someAction();
}
else {
someOtherAction();
}
for {...}
• The for statement is used to repeat a block of statements enclosed in curly braces
a specified number of times. An increment counter is often used to increment and
terminate the loop. There are three parts, separated by semicolons (;)
while(someVar ?? someValue){
someAction();
}
do { ... } while()
• The do loop is a bottom driven loop that works in the same manner as the while
loop, with the exception that the condition is tested at the end of the loop, so the
do loop will always run at least once.
do
{ someAction
();
}
while(someVar ??
someValue);
• A function is a block of code that has a name and a block of
statements that are executed when the function is called.
THANK YOU!