You are on page 1of 57

1

3 ARDUINO


Arduino Arduino (Arduino
Programming Language) Arduino Wiring
Arduino 2 (Structure) (Function)
Arduino C/C++ Arduino (
Arduino) C Arduino


3.1 setup()
3.2 loop()
3.3
3.4
3.5
3.6 C / C++ Arduino
3.7
3.8
3.9 Arduino
3.10 (constants)
3.11
3.12 Arduino

1. Arduino
2. Arduino
3. Arduino
3 Arduino
2

1.
2. setup()
3. loop()
4.
5.
6.
7. C / C++ Arduino
8.

3 Arduino
3

3
Arduino

Arduino 20
(2127-2107)
(.)
*****************************************************************************************
1. 10 (10 )
2. ()

1. Arduino 2
. /
. /
. /
. /
2. Arduino
. void up() void loop()
. void set() void setup()
. void setup () void looping()
. void setup() void loop()
3. loop()
.
. 5
. 10
. 15
4. if...else
.
.
.
.

3 Arduino
4

5.
. + (),- (), * ( ), / () % ()
. + (),- (), * ( ), / () % ()
. * (), / () % ()
. + (),- (), % ()
6. AND
. &&
. AND
.
. &
7. OR
. I
. II
.
. OR
8. Exclusive OR
. ^
. $
.
.
9. C (semicolon ; )
.
.
.
.
10.
. long 2
. float 2
. long 4
. float 4

3 Arduino
5

3
Arduino

Arduino Arduino (Arduino


Programming Language) Arduino Wiring
Arduino 2
1. (Structure)
2. (Function)
Arduino C/C++ Arduino
( Arduino) C Arduino

Arduino void setup() void
loop()
setup()
loop()

loop()

-

3.1 setup()


3.1
int buttonPin = 13;
void setup()
{
Serial.begin(9600);
pinMode(buttonPin, INPUT);
}

3 Arduino
6

void loop()
{
if (digitalRead(buttonPin) == HIGH)
Serial.println('H');
else
Serial.println('L');
delay(1000);
}
C AVR GCC ( C C
GCC AVR)
int main(void)
{
init();
setup();
for (;;)
loop();
return ;
}

3.2 loop()
setup() loop()


3.2
int buttonPin = 13;
// setup initializes eerial and the button pin
void setup()
{
Serial.begin(9600);
pinMode(buttonPin, INPUT);
}
// loop checks the button pin each time,

3 Arduino
7

// and will send serial if it is pressed


void loop()
{
if (digitalRead(buttonPin) == HIGH)
Serial.println('H');
else
Serial.println('L');
delay(1000);
}
loop() setup()
setup()
loop()
setup() loop()

3.2.1 if

if (somevariable > 50)


{
// do something Here
}
someVariable 50



x == y (x y)
x != y (x y)
x < y (x y)
x > y (x y)
x <= y (x y)
x >= y (x y)
== ( if (x==10))

3 Arduino
8

= ( if (x=10))
x 10
if if....else
3.2.2 if...else
if
500
500
3.3
if (pinFiveInput < 500)
{
// do thing A
}
else
{
// do thing B
}
else if
if....else...if
3.4
if (pinFiveInput < 500)
{
// do Thing A
}
else if (pinFiveInput >= 1000)
{
// do Thing B
}
else
{
// do Thing C
}
else if ( switch case

3 Arduino
9

if...else...if ) if...else
else
3.2.3 for()
for


for() 3
for (initialization; condition; increment)
{
//statement(s);
}
initialization
condition
increment
3.5
for (int i=1; i <= 8; i++)
{
// statement using the value i;
}
for C for
for C++
initialization condition increment for
3.2.4 switch-case

var
default
break case

switch-case break
break

3 Arduino
10

3.6
switch (var)
{
case 1:
//do something when var == 1
break;
case 2:
//do something when var == 2
break;
default:
// if nothing else matches, do the default
}
3.2.5 while

while()

while() while
while(expression)
{
// statement(s)
}
expression ()
3.7
var = 0;
while(var < 200)
{
// do something repetitive 200 times
var++; }

3 Arduino
11

3.3
5 + (),- (), * ( ), / () % ()
3.3.1

9/4 2 9 4
(int) (overflow)
9/4 = 2
9/4.0 = 2.25

result = value1 + value2;
result = value1 - value2;
result = value1 * value2;
result = value1 / value2;

value1 :
value2:
3.8
y = y + 3;
x = x - 7;
i = j * 6;
r = r / 5;


(0 1)
(0 -32768)
float

cast (int)myfloat
3.3.2 %
2
(float)
result = value1 % value2;

3 Arduino
12

value1 - byte,char,int long


value2 - byte,char,int long

3.9
x = 7 % 5; // x now contains 2
x = 9 % 5; // x now contains 4
x = 5 % 5; // x now contains 0
x = 4 % 5; // x now contains 4

(Roll Over)
3.10
// check a sensor every 10 times through a loop
void setup()
{
i++;
if ((i % 10) == 0)
{
x = analogRead(sensPin); // read sensor every ten times through loop
}
}
// setup a buffer that averages the last five samples of a sensor
int senVal[5]; // create an array for sensor data
int i, j; // counter variables
long average; // variable to store average

void loop()
{
// input sensor data into oldest memory slot
senVal[(i++) % 5] = analogRead(sensPin);
average = 0;
for (j=0; j<5; j++)
{

3 Arduino
13

average += sensVal[j]; // add samples


}
average = average / 5; // divide by total
}

3.4
if() while()
()
x == y (x y)
x != y (x y)
x < y (x y)
x > y (x y)
x <= y (x y)
x >= y (x y)
if() 3 &&, || !
3.4.1 && ( )

3.11
if (x > 0 && x < 5)
{
// ...
}
x 0 5 ( 1 4)

3.4.2 || ( )

3.12
if (x > 0 || y > 0)
{ // ...
} x y 0
3.4.3 ! ()

3 Arduino
14

3.13
if (!x)
{ // ...
} x ( x = 0 )
3.4.4
&&
& ||
() | () NOT
(~) (!)
3.14
if (a >= 10 && a <= 20){}
// true if a is between 10 and 20

3.5

C ( Arduino) 6
& (bitwise AND)
| (OR)
^ (Exclusive OR)
~ (NOT)
<< ()
>> ()
3.5.1 AND (&)
AND C &
(AND)
1 1 0

0 0 1 1 Operand1
0 1 0 1 Operand2

0 0 0 1 Returned result
Arduino int 16 AND

3 Arduino
15

16
3.15
int a = 92; // in binary: 0000000001011100
int b = 101; // in binary: 0000000001100101
int c = a & b; // result: 0000000001000100
// or 68 in decimal.
16 a b AND
16 c 01000100 68
AND ()
int masking
3.5.2 OR (|)
OR C |

(OR) 1 1 0
0
0 0 1 1 Operand1
0 1 0 1 Operand2

0 1 1 1 Returned result
3.16
OR
int a = 92; // in binary: 0000000001011100
int b = 101; // in binary: 0000000001100101
int c = a | b; // result: 0000000001111101
// or 125 in decimal.
3.17
AND OR AND
OR Read-Modify-Write on a port 8
8 8
8
PORTD 0,1,2,3,4,5,6,7
1 HIGH pinMode()

3 Arduino
16

PORTD = B00110001 2, 3 7 HIGH


0 1 Arduino


PORTD ( AND)
PORTD ( OR)

int i; // counter variable
int j;
void setup()
{
DDRD = DDRD | B11111100;
// set direction bits for pins 2 to 7,
// leave 0 and 1 untouched (xx | 00 == xx)
// same as pinMode(pin,OUTPUT) for pins 2 to 7
Serial.begin(9600);
}
void loop()
{
for (i=0; i<64; i++)
{
PORTD = PORTD & B00000011;
// clear out bits 2 - 7, leave pins 0
// and 1 untouched (xx & 11 == xx)
j = (i << 2);
// shift variable up to pins 2 - 7
// to avoid pins 0 and 1
PORTD = PORTD | j;
// combine the port information with
// the new information for LED pins
Serial.println(PORTD, BIN);
// debug to show masking

3 Arduino
17

delay(100);
}}
3.5.3 Exclusive OR (^)
C/C++ exclusive OR ( XOR)
^ OR
1 0
0 0 1 1 Operand1
0 1 0 1 Operand2
________
0 1 1 0 Returned result
XOR 0
1
3.18
int x = 12; / / binary: 1100
int y = 10; / / binary: 1010
int z = x ^ y; / / binary: 0110, or decimal 6
XOR int 0 1
1 0
XOR mask 1 mask
1 5(Di5)
3.19
/ / Blink_Pin_5
/ / demo for Exclusive OR
void setup ( )
{
DDRD = DDRD I B00100000;
/ / set digital pin five as OUTPUT
Serial. begin (9600);
}
void loop ()
{
PORTD = PORTD ^ B00100000;

3 Arduino
18

/ / invert bit 5 (digital pin 5),


/ / leave others untouched
delay (100);
}
3.5.4 NOT (~)
NOT ~
0 1 1 0

0 1 Operand1
____
1 0 ~ Operand1
int a = 103; / / binary: 0000000001100111
int b = ~a; / / binary: 1111111110011000
b -104 ()
() int 1
(2s complement)
int signed int

3.5.5 (<<) (>>)


C/C++ <<

3.6 C / C++ Arduino


3.6.1 semicolon ;

3.23
int a = 13;



3.6.2 curly brace { }

{ }

3 Arduino
19

Arduino IDE

BASIC C
RETURN Subroutine (function)
ENDIF NEXT FOR

Enter



(function)
void myfunction (datatype argument)
{
statements (s)
}
(loops)
while (boolean expression)
{
statement (s)
}
do
{
statement (s)
}
while (boolean expression);
for (initialisation; termination condition; incrementing expr)
{
statement (s)
}

3 Arduino
20

(condition)
If (boolean expression)
{
statement (s)
}
else if (boolean expression)
{
statement (s)
}
else
{
statement (s)
}
3.6.3 / / / * . . . */


C 2
(1) // 2
(2) / *
/* blabla */
3.6.4 # define


Arduino # define C

# define constantName value
#
3-24
# define ledpin 3
ledpin 3

3 Arduino
21


# define

3.6.5. # include


# include <file>
# include file
3.25
# include <stdio.h>
# include lcd.h
stdio.h
Arduino Arduino
2 lcd.h C

Arduino

3.7

Arduino
3.7.1 char :
1 (8 )
A (
ABC) ASCII
A +1 66 ASCII A 65

charsign = ' ';

char var = x;
var char
x

3 Arduino
22

3.7.2 byte : 8 1
byte 8 0 - 255
3.26
byte b = B10010111; // B is the binary formatter (151 decimal)
3.7.3 int :
interger int
2 -32,768 32,767
(2s complement) sign bit 1
Arduino
(>>)

int var = val;

var int
val
3.27
int ledPin = 31;

(Roll Over)

3.28
int x
x = -32,768;
x = x - 1; // x now contains 32,767
// - rolls over in neg. direction
x = 32,767;
x = x + 1; // x now contains -32,768 - rolls over
3.7.4 unsigned int :
int 2
0 65,535

unsigned int var = val;

3 Arduino
23


var int
val
3.29
unsigned int ledPin = 31;



3.30
unsigned int x
x = 0;
x = x - 1; // x now contains 65535
// - rolls over in neg direction
x = x + 1; // x now contains 0 - rolls over
3.7.5 long : 32
int long
32 (4 ) -2,147,483,648 2,147,483,647

long var = val;

var long
val
3.31
long time;
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.print("Time: ");
time = millis();

3 Arduino
24

Serial.println(time); //prints time since program started


delay(1000); // wait a second so as not to send massive amounts of data
}
3.7.6 unsigned long : 32
32 (4 )
0 4,294,967,295

unsigned long var = val;

var unsigned long
val
3.32
long time;
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.print("Time: ");
time = millis();
Serial.println(time); //prints time since program started
delay(1000); // wait a second so as not to send massive amounts of data
}
3.7.7 float :

int 3.4028235 x 1038 -3.4028235 x 1038
32 (4 ) float
int float

3 Arduino
25

floating point


float var = val;

var float
val
3.33
float myfloat;
float sensorCalbrate = 1.117;
3.34
int x;
int y;
float z;
x = 1;
y = x / 2; // y now contains 0,
// integers cant hold fractions
z = (float)x / 2.0;
// z now contains .5
// (you have to use 2.0, not 2)
serial.println()
10
3.7.8 double :
8 1.7976931348623157 x
10308 Arduino
3.7.9 string :
C char
3.35
char Str1[15];
char Str2[8] = {a,r,d,u,i,n,o};
char Str3[8] = {a,r,d,u,i,n, o,\0'};
char Str4[ ] = arduino;

3 Arduino
26

char Str5[8] = arduino;


char Str6[15] = arduino;
Str1
Str2
null character
Str3 null
String
Str4
+ 1 null string
Str5

Str6
3.7.10 (null termination)
(null string)
\0 () + 1
Str2 Str3 3.35 Arduino 7
[8]
3.35 Str1 Str6 14
3.7.11
"Abc" (char)
'A'
3.7.12 (array)

Arduino C


int myInts [6];
int myPins [ ] = {2, 4, 8, 3, 6};
int mySensVals [6] = {2, 4, -8, 3, 2};
char message [6] = hello;
myInts myPins

3 Arduino
27

mySersVals
char


0 mySensVals
mySensVals [0] == 2, mySensVals [1] == 4,

mySensVals [0] = 10;

x = mySensVals [4];


int x [3] 3 x [0], x [1] x [2] x [3]

()

()
for
for for

int i;
for (i = 0; I < 5; I = I + 1)
{
Serial.println (myPins [i] );
}
Tutorials
www.arduino.cc

3 Arduino
28

3.8
C Arduino (scope)
BASIC global
3.8.1
(global variable)



3.36
int PWMval; // any function will see this variable
void setup () {
//
}
void loop () {
int i; // I is only visible inside of loop
float f; // f is only visible inside of loop
}
3.8.2 (static)
(Keyword)

()
static

3 Arduino
29

3.9 Arduino
Arduino 123
(decimal)

10 (decimal) 123
2 (binary) B1111011
8 (octal) 0173
16 (hexadecimal) 0x7B
Decimal
101== 101 decimal ( (1 * 2^2) + (0 * 2^1) + 1)
Binary 0 1
B101 == 5 decimal ( (1 * 2^2) + (0 * 2^1) + 1)
8 ( 1 ) 0 (B0) 255 (B11111111)
16 ( int)
myInt = (B11001100 * 256) +B10101010; // B11001100 is the high byte
Octal 0 7
0101 == 65 decimal ((1 * 8^2) + (0 * 8^1) + 1)
0
8
Hexadecimal (hex) 0 9 A 10, B
11 F 15
0x101 == 257 decimal ( (1 * 16^2) + (0 * 16^1) + 1)

3.10 (constants)
Arduino

3.10.1 HIGH,LOW :
2 HIGH LOW
HIGH +5V +3V
HIGH HIGH 1 (TRUE)
LOW 0V +2V
LOW LOW 0 (FLSE)

3 Arduino
30

3.10.2 INPUT, OUTPUT :


2 (INPUT) (OUTRUT)

3.11
3.11.1 cast :
Cast


(type) variable
Type ( int, float, long)
Variable
3.37
int i;
float f;
f = 3. 6;
i = (int) f; // now i is 3
float int (int)3.2 (int)3.7
3
3.11.2 sizeof :



sizeof (variable)
sizeof (variable)
Variable (int, float, long)
3.38
sizeof ()

3 Arduino
31

char myStr [ ] =this is a test;


int i;
void setup ( )
{
Serial . begin(9600);
{
void loop ( )
{
for (i = 0; I < sizeof (myStr) 1; i++
{
Serial .print (i, DEC);
Serial .print ( = );
Serial .println (myStr[i], BYTE);
}
}

3 Arduino
32

3.12 Arduino
C Arduino

# # # # #
Constants Port Constants Datatypes other other
HIGH DDRB boolean += abs
LOW PINB byte +[ acos
INPUT PORTB char ] analogRead
OUTPUT DDRC class =&& analogWrite
SERIAL PINC default = attachInterrupts
DISPLAY PORTC do || asin
PI DDRD double = atan
HALF_ PI PIND int ,/ atan2
TWO_PI PORTD long / available
LSBFIRST private ?: begin
MSBFIRST protected << bit
CHANGE public << bitRead
FALLING return = bitWrite
RISING shot log bitSet
false signed && bitClear
true static !| boolean
null switch | byte
throw ^^ case
Try = ceil
Unsigned ++ char
Void != char
while - class
% abs
/ acos

3 Arduino
33

* constrain
{ cos
} default
// delay
** delayMicroseconds
. detachInterrupts
- digitalWrite
= digitalRead
== else
<< exp
= false
() find
>> findUntil
; float
floor
for
HALF_PI
if
int
log
loop
map
max
micros
millis
min
new
noInterrupts
noTone
null
parseInt
parseFloat

3 Arduino
34

pinMode
print
println
pulseIn
radians
this
tone
true
write
# USB
Keyboard
Mouse
read
press
release
releaseAll
readBytes
readBytesUntil
return
round
serial
serial1
serial2
serial3
setTimeout
Setup
shiftIn
shiftOut
sin
sq
sqrt
tan

3 Arduino
35

loop() setup()
setup()
loop()
setup() loop()


float
cast (int)myfloat

3 Arduino
36

Arduino 20
*******************************************************************************************************
2 1 2 (20 )
2. 1 - 20 (10 )
3. 2 10 (10 )

1. #define
2. # define
3. #include
4. char : 1 (8 )
5. byte : 8 1 8 0 - 255
6. int : 1 -32,768 32,767
7. unsigned int : 0 65,535
8. long : (4 ) -2,147,483,648 2,147,483,647
9. unsigned long : 16 0
4,294,967,295
10. string :
char

3 Arduino
37

()

1. Arduino
. C/C++
. English / Thai
. Assembly Language
. Java Language
2. setup()
. 1
. 2
. 3
. 4
3. if
.
.
.
.
4. for() 3
. initialition; condition; increment
. initialization; contion; increment
. initialization; condition; crement
. initialization; condition; increment
5. x = 5 % 5
. 1
. 0
. 0.1
. 0.5

3 Arduino
38

6.
. if() while()
. switch() case()
. for() while()
. for() if()
7. if()
. && !
. || !
. &&, ||
. &&, || !
8. NOT
. ~
.
.
.
9.
. / / / * . . . */
. / / * / . . . /*
. / / */ . . . */
. / / / * . . . /*
10. unsigned long
. 8
. 16
. 32
. 64

3 Arduino
39

3
Arduino
*******************************************************************************************************
3 Arduino
180 ( 20 )

1. Arduino
2. Arduino Uno R3
3. Arduino Uno R3


1. Arduino IDE 1.6.9 1
2. USB Cable Arduino Uno R3 1
3. Arduino Uno R3 Board 1
6. Hook-up Wires 10
7. Breadboard 1
8. LED 6
9. Momentary Button or Pushbutton Switch 2
10. 10K Ohm Resistor 2
11. 220 Ohm Resistor 6
12. Potentiometer or Variable Resistor 1
13. Photoresistor or Another Analog Sensor 1

1. Arduino Uno R3

2. Arduino Uno R3

3. USB Arduino
Uno R3

3 Arduino
40

3.1 setup()

Hardware Required
1. Arduino Uno Board

Code
void setup()
{
Serial.begin(9600);
}
void loop()
{
int sensorValue = analogRead(A0);
float voltage = sensorValue * (5.0 / 1023.0);
Serial.println(voltage);
}
// Serial.begin

...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................

3 Arduino
41

3.2 loop()
setup() loop()

Hardware Required
1. Arduino Uno Board

Code
const int buttonPin = 13
void setup(){
Serial.begin(9600);
pinMode(buttonPin, INPUT);}
void loop(){
if (digitalRead(buttonPin) == HIGH)
Serial.write('H');
else
Serial.write('L');
delay(1000);}
// Serial.write('H'); Serial.write('L');

...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
..........................................................................................................................................................................

3 Arduino
42

3.3 if

Hardware Required

1. Arduino uno Board


2. Momentary button or Switch
3. 10K ohm resistor
4. hook-up wires
5. breadboard

Circuit

Schematic

3 Arduino
43

Code
const int buttonPin = 2;
const int ledPin = 13;
int buttonState = 0;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop()
{
buttonState = digitalRead (buttonPin);
if (buttonState == HIGH)
{
digitalWrite(ledPin, HIGH);
}
// if (buttonState == HIGH) if (buttonState == LOW)


.....................................................................................................................................................................
.....................................................................................................................................................................
.....................................................................................................................................................................
.....................................................................................................................................................................
.....................................................................................................................................................................
.....................................................................................................................................................................
.....................................................................................................................................................................
.....................................................................................................................................................................
.....................................................................................................................................................................
.....................................................................................................................................................................
.....................................................................................................................................................................
.....................................................................................................................................................................

3 Arduino
44

3.4 ifelse
if

Hardware Required

1. Arduino or Genuino Board


2. Potentiometer or variable resistor

Circuit

Schematic

3 Arduino
45

Code

const int analogPin = A0; // pin that the sensor is attached to


const int ledPin = 13; // pin that the LED is attached to
const int threshold = 400; // an arbitrary threshold level that's in the range of the
analog input
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communications:
Serial.begin(9600);
}
void loop() {
// read the value of the potentiometer:
int analogValue = analogRead(analogPin);
// if the analog value is high enough, turn on the LED:
if (analogValue > threshold) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
// print the analog value:
Serial.println(analogValue);
delay(1); // delay in between reads for stability
}


.....................................................................................................................................................................
.....................................................................................................................................................................
.....................................................................................................................................................................
.....................................................................................................................................................................
.....................................................................................................................................................................
.....................................................................................................................................................................

3 Arduino
46

3.5 for()
for


for() 3
for (initialization; condition; increment)
{
//statement(s);
}

Hardware Required

1. Arduino or Genuino Board


2. 220 ohm resistors 6 PCS
3. LEDs 6 PCS
4. hook-up wires
5. breadboard

3 Arduino
47

Circuit

Schematic

3 Arduino
48

Code

int timer = 100; // The higher the number, the slower the timing.
void setup() {
// use a for loop to initialize each pin as an output:
for (int thisPin = 2; thisPin < 8; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
void loop() {
// loop from the lowest pin to the highest:
for (int thisPin = 2; thisPin < 8; thisPin++) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);
}
// loop from the highest pin to the lowest:
for (int thisPin = 7; thisPin >= 2; thisPin--) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);
}
}
}

...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................

3 Arduino
49

3.6 switch-case

var
default
break case

switch-case break
break

Hardware Required

1. Arduino or Genuino Board


2. photoresistor, or another analog sensor
3. 10k ohm resistors
4. hook-up wires
5. breadboar

Circuit

3 Arduino
50

Schematic

Code
const int sensorMin = 0; // sensor minimum, discovered through experiment
const int sensorMax = 600; // sensor maximum, discovered through experiment
void setup() {
// initialize serial communication:
Serial.begin(9600);
}

void loop() {
// read the sensor:
int sensorReading = analogRead(A0);
// map the sensor range to a range of four options:
int range = map(sensorReading, sensorMin, sensorMax, 0, 3);

// do something different depending on the


// range value:
switch (range) {
case 0: // your hand is on the sensor

3 Arduino
51

Serial.println("dark");
break;
case 1: // your hand is close to the sensor
Serial.println("dim");
break;
case 2: // your hand is a few inches from the sensor
Serial.println("medium");
break;
case 3: // your hand is nowhere near the sensor
Serial.println("bright");
break;
}
delay(1); // delay in between reads for stability
}

.....................................................................................................................................................................
.....................................................................................................................................................................
.....................................................................................................................................................................
.....................................................................................................................................................................
.....................................................................................................................................................................
.....................................................................................................................................................................
.....................................................................................................................................................................
.....................................................................................................................................................................

3 Arduino
52

3.7 while

while()

while() while
while(expression)
{
// statement(s)
}
expression ()

Hardware Required

1. Arduino or Genuino Board


2. pushbutton or switch
3. photoresistor or another analog sensor
4. 10k ohm resistors 2 PCS
5. Breadboard

Circuit

3 Arduino
53

Schematic

Code

const int sensorPin = A2; // pin that the sensor is attached to


const int ledPin = 9; // pin that the LED is attached to
const int indicatorLedPin = 13; // pin that the built-in LED is attached to
const int buttonPin = 2; // pin that the button is attached to
// These variables will change:
int sensorMin = 1023; // minimum sensor value
int sensorMax = 0; // maximum sensor value
int sensorValue = 0; // the sensor value
void setup() {
// set the LED pins as outputs and the switch pin as input:
pinMode(indicatorLedPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {

3 Arduino
54

// while the button is pressed, take calibration readings:


while (digitalRead(buttonPin) == HIGH) {
calibrate();
}
// signal the end of the calibration period
digitalWrite(indicatorLedPin, LOW);
// read the sensor:
sensorValue = analogRead(sensorPin);
// apply the calibration to the sensor reading
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
// in case the sensor value is outside the range seen during calibration
sensorValue = constrain(sensorValue, 0, 255);
// fade the LED using the calibrated value:
analogWrite(ledPin, sensorValue);
}
void calibrate() {
// turn on the indicator LED to indicate that calibration is happening:
digitalWrite(indicatorLedPin, HIGH);
// read the sensor:
sensorValue = analogRead(sensorPin);
// record the maximum sensor value
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}
// record the minimum sensor value
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}

.....................................................................................................................................................................
.....................................................................................................................................................................
....................................................................................................................................................................

3 Arduino
55


...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................
...........................................................................................................................................................................


...........................................................................................................................................................................
...........................................................................................................................................................................


3.1 setup() 1
3.2 loop() 1
3.3 if 2
3.4 ifelse 4
3.5 for() 4
3.6 switch-case 4
3.7 while 4

............

3 Arduino
56

3
Arduino

Arduino 20
(2127-2107)
(.)
*****************************************************************************************
1. 10 (10 )
2. ()

1. Arduino 2
. /
. /
. /
. /
2. Arduino
. void set() void setup()
. void up() void loop()
. void setup() void loop()
. void setup () void looping()
3. loop()
. 5
. 10
. 15
.
4. if...else
.
.
.
.

3 Arduino
57

5.
. + (),- (), % ()
. + (),- (), * ( ), / () % ()
. + (),- (), * ( ), / () % ()
. * (), / () % ()
6. AND
. AND
. &&
. &
.
7. OR
. OR
. I
. II
.
8. Exclusive OR
. $
. ^
.
.
9. C (semicolon ; )
.
.
.
.
10.
. float 2
. float 4
. long 2
. long 4

3 Arduino

You might also like