Προγραμματίζοντας με τον μικροελεγκτή Arduino
-
Upload
giannis-alifragis -
Category
Documents
-
view
96 -
download
4
description
Transcript of Προγραμματίζοντας με τον μικροελεγκτή Arduino
-
Arduino
2015
-
1, 2015
ISBN 978-960-93-6760-8 Creative Commons - 4.0 (http://creativecommons.org/licenses/by-sa/4.0/). :
, . (2015). Arduino. . : .
-
/
(), ()
(), ,
.
(Arduino UNO R3) Arduino (http://www.arduino.cc).
(open source) Fritzing (http://www.fritzing.org). Fritzing .
19, M.Ed.web: http://users.sch.gr/manpoulemail: [email protected]
-
..............................................................................................................................................7
1. Arduino.......................................................7
2. ......................................................................................................8
3. Arduino IDE.....................................................................................9
4. ..........................................................................................................................10
5. / (Pins).........................................................................................................10
6. - .......................................................................................116.1. ............................................................................................................116.2. .....................................................................................................................................126.3. (Pins)......................................................126.4. - ................................................................................12
6.4.1. ..............................................................................................................136.4.2. .............................................................................................................136.4.3. (PWM pins).......................................................................................136.4.4. ..........................................................................................................14
6.5. ...............................................................................................................146.5.1. delay()...............................................................................146.5.2. - delayMicroseconds().........................................................156.5.3. - millis()......................................................................15
6.6. - map()................................................................................156.7. (Serial)...................................................................................156.8. .........................................................................................................................166.9. (For)..........................................................................................................17
7. breadboard .................................................................17
8. .............................................................................18
9. .................................199.1. - Leds..................................................................................................................19
9.1.1. - RGB Leds.................................................................................209.2. - Buttons..................................................................................................................219.3. - Potentiometer..............................................................................................229.4. Sounder......................................................................................................................229.5. Photoresistor (LDR)................................................................239.6. - Servos.......................................................................................................................239.7. DC Motor...........................................................................................24
9.7.1. ....................................................................................................249.8. Ultrasonic sensor............................................................................259.9. - Shields....................................................................................................................26
10. ............................................................................................................................2610.1. 1 To led ......................................................................2710.2. 2 (Fade in-Fade out)........28
-
10.3. 3 (Fade in-Fade out), (Blink at peaks)............................................................................................................2910.4. 4 .........3010.5. 5 - ..................................................3110.6. 6 (RGB Led).................................................3210.7. 7 RGB Led.........................................................3310.8. 8 .....................................................................3410.9. 9 .........................................3510.10. 10 ...3610.11. 11 , (Led chase effect)...............................3710.12. 12 , (Led chase effect) . 3810.13. 13 , (Led effect + light sensor).....................3910.14. 14 (Sounder)..............................................4010.15. 15 & (Sounder & Light Sensor).....................4110.16. 16 (Servos)...............................................................4210.17. 17 (Servos) ..................................4310.18. 18 (DC Motor).................................................4410.19. 19 2 .......................................4510.20. 20 - .......4610.21. 21 .........................................................................47
11. ......................................................................................................................................4811.1. ........................................................................................................................4811.2. ................................................................................................................48
.......................................................49 1 To Led ..............................................................................49 2 (Fade in-Fade out).................49 3 (Fade in-Fade out), (Blink at peaks)......................................................................................49 4 ..................50 5 ..........................................................50 6 (RGB Led)..........................................................51 7 RGB Led..................................................................51 8 T .............................................................................52 9 T .................................................53 10 T .............54 11 , (Led chase effect)..........................................55 12 , (Led chase effect) ............56 13 , (Led effect + light sensor)................................57 14 (Sounder).........................................................57 15 & (Sounder & Light Sensor)................................58 16 (Servos)..........................................................................58 17 (Servos) .............................................59 18 (DC Motor)............................................................59 19 2 ..................................................59 20 .................61
-
Arduino, Arduino UNO R3. . , , , .
Arduino, . , . Arduino C/C++, , . Arduino, . , .
, , , . , 5, 6 9 . , . , Arduino. , , .
, . , . (.. , ).
. , ' , , Arduino, . 2014-15.
1. Arduino Arduino ,
E. - Arduino . 7 64
-
. , , , .
, , . Arduino IDE C/C++.
Arduino (http://arduino.cc/) , (http://arduino.cc/en/Main/Software).
Arduino IDE, Scratch*, Arduino, S4A - Scratch For Arduino*, . (blocks Scratch) . ArduBlock*, blocks . , (web ), BlocklyDuino* ArduinoMio*. *.
, Arduino IDE, , .
2. Arduino , , . , Arduino , , Arduino. Arduino, , Arduino . -ino, Funduino.
(http://store.arduino.cc/), (http://arduino.cc/en/Main/Buy).
:
, Arduino Uno R3 , 14 / 6 (Pins) ( usb ),
pins ,
* 11.2. ).
E. - Arduino . 8 64
-
(breadboard)
leds, , , , .
, (starter kit) , usb , breadboard, ( ), leds, , , , , , .. Arduino Starter Kit (http://store.arduino.cc/product/K000007).
Arduino, , Arduino, , , 1/3 .
3. Arduino IDE Arduino IDE ( 1). ( C/C++) . Arduino IDE Windows, Mac Linux (http://arduino.cc/en/Main/Software).
1 Arduino IDE
, ( => ).
E. - Arduino . 9 64
-
4. Arduino USB . 5V .
- 7V 12V, 5V (. http://arduino.cc/en/Main/arduinoBoardUno). pins : (+) Pin VCC IN (-) Gnd .
USB 5V USB .
5. / (Pins) Arduino Uno R3 14 (digital input/outpit pins) (analog input pins). 14 0 13, 0 5 (.. 3). pins 0 5V . 14 , 3, 5, 6, 9, 10, 11, PWM (Pulse Width Modulation), .
, :
, 14 0..13. , 0 5V, LOW HIGH .
, 14 0..13. , 0 5V, LOW HIGH .
, 0 5V, A0..A5.
, PWM (3, 5, 6, 9, 10, 11), 0 5V.
pinMode(), . .
, pins 0 1 , , (.. 12 pins ).
, 13 Led Arduino Uno, .
E. - Arduino . 10 64
-
6. - Arduino IDE .
Arduino - , setup() loop() :
setup() - , ( reset ). / ( Pin ).
loop() - . loop(), , . , Arduino reset.
, Arduino setup() loop() ( ) reset.
Reset setup() loop() , . ( ) Reset .
:
void setup() { /* Reset */}void loop() {/* , Reset */}
6.1. , . Arduino . :
boolean, 0 1 ( True False) byte, 0 255 int, -32768 32767 long, -2147483648 2147483647 float, char, ( Byte) string,
E. - Arduino . 11 64
-
:
int ledPin = 13; // ledPin 13
float SinVal; // SinVal
6.2. , . // ( // ), /* */ ( /* */ ).
:
int ledPin = 13; // Pin LED/* LED 1 sec */
6.3. (Pins) , . ( setup) Pins .
pinMode(Pin, Mode) ) Pin ) INPUT () OUTPUT().
14 Pins, 6 PWM, 0..13 0..5.
:
pinMode(12, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(2, INPUT);
6.4. - (pin) , . , .
, , .
E. - Arduino . 12 64
-
6.4.1. 14 pins Arduino , 0 5V. digitalWrite(Pin, Value), Pin , 0 V 5 V, value
LOW : 0 V (pin)
HIGH : 5 V (pin)
:
digitalWrite(ledPin, HIGH);
: setup(), pinMode.
:
pinMode(10, OUTPUT);
6.4.2. 14 pins Arduino , 0 5V. digitalRead(Pin), Pin , . H 0V 5V, :
LOW : 0 V (pin)
HIGH : 5 V (pin)
:
Val = digitalRead(ledPin);
: setup(), pinMode.
:
pinMode(10, INPUT);
6.4.3. (PWM pins) 14 Pins Arduino PWM, .
, 0 255 () 0 5V. analogWrite(Pin, Value), Pin , 0 V 5 V, value. 0 0V (pin), 255 5V (pin),
E. - Arduino . 13 64
-
(.. 122 2,5V).
:
analogWrite(ledPin, 122);
: PWM pins . PWM pins 3, 5, 6, 9, 10, 11.
: setup(), pinMode.
:
pinMode(10, OUTPUT);
6.4.4. To Arduino 6 , A0, A1, A2, A3, A4, A5. (.. ) . analogRead(Pin), Pin , . 0 1023. .
:
int r = analogRead(A1);
: setup(), pinMode.
:
pinMode(A1, INPUT);
6.5. . .
6.5.1. delay() . delay(time) time ms (1/1000 sec). delay(time) time.
:
delay(1000); // 1000 ms = 1 sec
delay(500); // 500 ms = 0.5 sec
E. - Arduino . 14 64
-
6.5.2. - delayMicroseconds() delay, delayMicroseconds(), microseconds (1/106 sec).
:
delayMicroseconds(10); // 10sec = 1/100000 sec
6.5.3. - millis() Arduino , ( reset). millis(), milliseconds (1/1000 sec) . , .
, (.. ), .
:
lastPress = millis();
if (lastPress millis() > 1000) {...}
6.6. - map() . , Arduino ,
map(, , , , )
( ) ( ) :
val = map(val, 0, 1023, 0, 179); /* (0 1023) (0 179) */
6.7. (Serial)To Arduino . USB ( ) pins 0 1 (.. ). ,
E. - Arduino . 15 64
-
, pins .
setup() Serial.begin(BaudRate), BaudRate bits ( 9600 ).
:
Serial.begin(9600);
, . (debugging) , . print(), println() .
:
Serial.print(H epikoinwnia ksekinhse); /* */
Serial.println(distance); /* distance */
Arduino USB , ( 2), .
2
6.8. . ,
if { }
else { }
, , (>, =, 500. , ( || ', && ), .. (potVal > 500) && (timePass >= 1000) .
{ } . , . , else { } .
E. - Arduino . 16 64
-
, , .
6.9. (For) . , . . :
for (; ;)
{ }
, .. i = 0 , .. i+5 ( i++ i+1) , .. i < 10 ( )..
for (i=1;i
-
3 Breaboard
, (5 ), . 4 .
4 Breadboard
SparkFun.Com, (https://learn.sparkfun.com/tutorials/how-to-use-a-breadboard).
8.
5
E. - Arduino . 18 64
-
USB . Arduino , / .
Arduino ( 5) :
- . .. Arduino Uno -
Arduino USB . Windows COMX (.. COM3, COM11), Linux /dev/tty.
6 , ( 6) . , .
7 , , , , , ( 7) . , .
9. .
, , .
, . .
9.1. - Leds , leds , (. 8). led ,
E. - Arduino . 19 64
-
.
R = (V V) / I
R , V (5V Arduino), V led I led. , 220 led .
8 Leds
, leds , . (+) (-). , () (+). , , ( ).
9.1.1. - RGB Leds leds, RGB leds ( 9), , RGB. led ( - Red, - Green, - Blue) , , .
9 RGB led
RGB leds ( ) . , (+) (-), . , , (-), (GND). , Pin Arduino, .
: , Arduino, . . led - 150-180 75-100 . ,
E. - Arduino . 20 64
-
( ).
led (2 9), pin (1 ), pin (3 ) pin (4 ).
9.2. - Buttons ( 10) , .. .
10 (button)
. ( LOW input pin HIGH) pull-down resistor 11.
11 Pull-down resistor 12 Pull-up resistor
( HIGH input pin LOW pull-up resistor 12 .
. . , 12 (pull-up) , 5V ( ), (5V Input). 11 (pull-down), , ( ), (5V Input). , .
E. - Arduino . 21 64
-
9.3. - Potentiometer . , , ( 13).
13 (potentiometer)
(5V), (GND) ( pin Arduino). , . , .
9.4. Sounder Arduino . led, , (5V) (GND), 14. (.. 5V) , (.. (5V), PWM pin Arduino ) , .
14 () 15
( 15). .
E. - Arduino . 22 64
-
9.5. Photoresistor (LDR) ( 16). ( ) . , (.. 5V GND Arduino) .
16 (LDR)
(.. led ). .
9.6. - Servos Arduino ( 17), TowerPro SG90. 0 180 . . , 180 . , Pin Arduino .
17 (servo)
(#include ), servo (servo myservo;) pin .
#include
int servoPin = 13;
Servo myservo;
servoPin setup, setup()
myservo.attach(servoPin);
() (5V), () (GND) () pin (),
E. - Arduino . 23 64
-
servo.write(num);
num 0 179, 180 .
9.7. DC Motor (DC motors). ( 18).
18 (DC motor)
, , , . , .
9.7.1. . - (H-bridges) L298N ( 19), -. http://adhocnode.com/motor-control/.
19 L298N ( )
pins , . pin , ( ), 1, 2 3, 4 . , pins (5V) (GND),
E. - Arduino . 24 64
-
() GND VMS . , , , MOTOR A MOTOR B . , .
pin ( ) .
1 ( 3)
-
distance = (( HIGH )*( : 340m/s) ) / 2
2 , . pulseIn microseconds (10-6 sec). http://arduino.cc/en/Reference/pulseIn , .
, ( 0 ), ( 180 ) ( 90 ).
9.9. - Shields Arduino (shields) pins . , WiFi Shield, BlueTooth Shield , Arduino . , , .
, ArduinoSensor Shield ( 21) pin (Ground, Voltage, Pin) pin ( ) (5V) (GND).
21 Arduino Sensor Shield v.5.0
10. , , . , . , , , , .
E. - Arduino . 26 64
-
10.1. 1 To led led , , ' .
Led 5V Led (.. Led ). 22 .
22 1
Pin 10, Led . Led. led +, Pin 10 . , led GND .
, . , :
1. Pin 10.2. Pin10 .3. 5V Pin10.4. 1
led.5. ( 0V) Pin10.6. 1
led. 1,2 . 3-6 .
. - Arduino . 27 64
: (5), (6.1), (6.3), (6.4.1), (6.5.1), Breadboard(7), (8), Led (9.1)
: (5), (6.1), (6.3), (6.4.1), (6.5.1), Breadboard(7), (8), Led (9.1)
-
10.2. 2 (Fade in-Fade out) led , . , . , .
led , 23.
23 2
led PWM Arduino, . 0 255, 0 5V. , analogWrite(Pin, Brightness) Pin , 0 255. !
, :
1. .2. 0. 3. (.. 5) , 255.4. 255
.
. - Arduino . 28 64
: (5), (6.1), (6.3), (6.5.1), Breadboard(7), (8), Led (9.1)
: (6.4.3.)
-
10.3. 3 (Fade in-Fade out), (Blink at peaks)
, . , led (blink) . , .
led , 24.
24 3
, . :
1. (, , ).2. 0 (.. 5) 255.3. (255), led
(blink).4. (.. -5) ,
0.5. (0), led (blink)
.
. - Arduino . 29 64
: (5), (6.1), (6.3), (6.4.3.), (6.5.1), Breadboard(7), (8), Led (9.1)
: (6.8)
-
10.4. 4 led . led pin, pin, ( ) led.
led, pin (PWM). , ( Arduino Uno) . , 25. (5V), (GND) . 0 1023, Pin (.. 4). breadboard, .
25 4
, led (OUTPUT) (INPUT). , , 0 1023, led, 4. AnalogWrite(PIN, Value) AnalogRead(Pin). Pin A0, A1, , A5, 0, 1, 2,..13.
1. (-).2.
( ) led . 3.
led (.. 50 msec).
. - Arduino . 30 64
: (5), (6.1), (6.3), (6.4.3.), (6.5.1), Breadboard(7), (8), Led (9.1)
: (6.4.4), (9.3)
-
10.5. 5 - , .
, USB.
, .
1. setup() 9600.2. loop(), H
epikoinwnia ksekinhse: .3. 1 20
. 4. ,
, .. Twra vlepw thn epanalhpsh : X, ' .
5. ( ), .
6. , .
7. .
: (debugging). , :
1. .
2. ( ) .
3. , !
. - Arduino . 31 64
: (6.7)
-
10.6. 6 (RGB Led) RGB led .
RGB led , . 26 . , . , pin . (2) led (GND), (1) , (3) (4) . , PWM .
26 6
led PWM Arduino, . :
1. (, , ).2. led.
led, .. for(i=0;i
-
10.7. 7 RGB Led led, , , RGB led .
led (, , ) , . 27. pin . PWM . , led, !
27 7
leds PWM . , . RGB led! , , , Led .
1. ( , , /).2. led,
.3.
!
. - Arduino . 33 64
: (5), (6.1), (6.3), (6.4.3.), (6.5.1), Breadboard(7), (8), Led (9.1)
: RGB led (9.1.1), (6.9)
-
10.8. 8 . , . *, , , , , ...
led, , ( ) , . 28 . GND, breadboard (-) GND Arduino.
28 8
, . , :
1. 5V led 0V .2. 1
led .3. 1, 2 , Led.4. .
.
* , . ( ) . .
. - Arduino . 34 64
: (5), (6.1), (6.3), (6.4.1.), (6.5.1), Breadboard(7), (8), Led (9.1)
-
10.9. 9 , 8, . , , , . .
, 8, led, . 29 . GND breadboard.
29 9
, . .
. - Arduino . 35 64
: (5), (6.1), (6.3), (6.4.1.), (6.5.1), Breadboard(7), (8), Led (9.1)
-
10.10. 10 , ( 8 9), , : (), , . ( ) . , (). .
, 8 9, . . 30 .
30 10
. , :
1. (, , /).2. .3. (pin)
. , , millis(). .
.
. - Arduino . 36 64
: (5), (6.1), (6.3), (6.4.1.), (6.5.1), Breadboard(7), (8), Led (9.1)
: (9.2), (6.5.3)
-
10.11. 11 , (Led chase effect) led, , .. 8. pins , , . led .
led . led , 31.
31 11
led , . , , led, .
1. led (, digitalWrite(led, value)).
2. .3. ( ),
led , , . led :
for (i=0;i
-
10.12. 12 , (Led chase effect) , . delay(500) led . ( ) , delay.
, leds , . , . . , 32. (5V), (GND) . 0 1023, . breadboard, .
32 12
. , delay. !
, . - .
!
. - Arduino . 38 64
: (5), (6.1), (6.3), (6.4.1.), (6.4.4), (6.5.1), (6.9), Breadboard(7), (8), Led (9.1), (9.3)
-
10.13. 13 , (Led effect + light sensor) . led .
led Arduino . (photoresistor), . ( 1), : , ( ) ( 33). ( ), ( ) .
33 13
. pin led . ( 0-1023) led ( 0-255). (.. 4) map !
1: . . . , . ;
2: , !
. - Arduino . 39 64
: (5), (6.1), (6.3), (6.5.1), (6.4.3.), (6.4.4), Breadboard(7), (8), Led (9.1)
: map (6.6), (9.5)
-
10.14. 14 (Sounder) . (sounders) . .
. ( - ), (5V) (GND). . 34 , (PWM) Arduino, .
34 14
. pin Arduino .
1. .
2. pin . 0 255, (.. 500msec) .
3. , 0.
. - Arduino . 40 64
: (5), (6.1), (6.3), (6.4.3), (6.5.1), (6.9), Breadboard(7), (8)
: (9.4)
-
10.15. 15 & (Sounder & Light Sensor) . . led ( 13).
pin. (photoresistor). ( 1), : , ( ) Arduino (.. 1) . 35 . ( ), .
35 15
13, . () led . ( 0-1023) led ( 0-255), . 1: . . . , . ; 2: , !
. - Arduino . 41 64
: (5), (6.1), (6.3), (6.4.3.), (6.4.4), (6.5.1), map (6.6), Breadboard(7), (8), (9.4), (9.5)
-
10.16. 16 (Servos) , 180 . .
. : (5V), pin Arduino. . TowerPro SG90 () (5V), () (GND) () pin . 36 .
36 16
, . , . .
1. pin servo2. .3. servo . 4. .
setup() , attach(Pin).
5. write() servo , : 90 180 90 0 , .
. - Arduino . 42 64
: (5), (6.1), (6.3), (6.5.1), Breadboard(7), (8)
: (9.6)
-
10.17. 17 (Servos) . . : .
. , . 37 . breadboard, .
37 17
. :
1. , .
2. , , . : 0 1023 .
3. .
. - Arduino . 43 64
: (5), (6.1), (6.3), (6.4.4.), (6.5.1), map (6.6), Breadboard(7), (8), (9.3), (9.6)
-
10.18. 18 (DC Motor) (DC motor). , pin . .
. . 38.
38 18
.
1. pin , .
2. (pin ) . 3. (pin ) (0 255),
. 4.
.
. - Arduino . 44 64
: (5), (6.1), (6.3), (6.5.1), Breadboard(7), (8)
: (9.7)
-
10.19. 19 2 H- (H-bridge). chip L298N. chip . *.
L298N. , .
, . :
1. pin .2. .3. .4. .5. .
.6. .
.7.
, . 8. ;
;
* 4 L293D, , .
. - Arduino . 45 64
: (5), (6.1), (6.3), (6.5.1), Breadboard(7), (8), (9.7)
: (9.7.1)
-
10.20. 20 - , . HC-SR04, .
(. 39). (5V), (GND) pin, (trigger) (echo).
39 20
. , .
1. pins . 2. 9600.3. ( cm), .. 0 200,
0 2 .4. trigger ,
digitalWrite() 2 s.5. trigger 10 s.6. , echo
pulseIn(echoPin, HIGH);7.
( distance = duration/58.2; )8. .9. .10. 50 ms .
. - Arduino . 46 64
: (5), (6.1), (6.3), (6.5.1), Breadboard(7), (8)
: (9.8), (6.5.2)
-
10.21. 21 , . . :
( ), .
, .
( ) Arduino , . , . , . !
. - Arduino . 47 64
-
11.
11.1.
Banzi, M. (2011). Getting Started with Arduino (2nd Edition). USA: Make:Books(O' Reilly)
Borchers, J. (2013). Arduino In a Nutshell (v.1.8). 27/12/2014 http://hci.rwth-aachen.de/arduino
McRoberts, M. (2013). Beginning Arduino (2nd Edition). USA: Apress
SparkFun Electronics (2012). Introduction to Arduino Educational Material. 27/12/2014 https://dlnmh9ip6v2uc.cloudfront.net/learn/materials/1/Arduino_final_handout.pdf
11.2.
Ad Hoc Node: http://adhocnode.com/category/arduino/
ArduBlock: http://blog.ardublock.com/
Arduino: http://www.arduino.cc
ArduinoMio: http://www.mastrohora.it/arduinomio/en/xuino.php
BlocklyDuino: http://www.gasolin.idv.tw/public/blockly/demos/blocklyduino/index.html
Fritzing: http://fritzing.org
Scratch: http://scratch.mit.edu
Scratch4Arduino: http://s4a.cat/
SparkFun: https://learn.sparkfun.com/
Tolabaki: http://wiki.tolabaki.gr/w/To_Labaki:About
E. - Arduino . 48 64
-
1 To Led
int ledPin = 10;
void setup() {pinMode(ledPin, OUTPUT);
}
void loop() {digitalWrite(ledPin, HIGH);delay(1000);digitalWrite(ledPin, LOW);delay(1000);
}
2 (Fade in-Fade out)
int ledPin = 10;int brightness = 0;
void setup() {pinMode(ledPin, OUTPUT);
}
void loop() {if (brightness == 255) {
brightness = 0;};analogWrite(ledPin, brightness);brightness = brightness + 5;delay(100);
}
3 (Fade in-Fade out), (Blink at peaks)
int ledPin = 10;int brightness = 255;int interval = 5; //
void setup() {pinMode(ledPin, OUTPUT);
}
E. - Arduino . 49 64
-
void loop() {if (brightness == 255 || brightness == 0) {// (blink)
digitalWrite(ledPin, HIGH);delay(100);digitalWrite(ledPin, LOW);delay(100);digitalWrite(ledPin, HIGH);delay(100);digitalWrite(ledPin, LOW);delay(100);
/* , () () */
interval = (-1)*interval;};analogWrite(ledPin, brightness);brightness = brightness + interval;delay(100);
}
4
int ledPin = 10;int potPin = A4;int potValue = 0; //
void setup() {pinMode(ledPin, OUTPUT);pinMode(potPin, INPUT);
}
void loop() {potValue = analogRead(potPin);analogWrite(ledPin, potValue/4);delay(50);
}
5
int i;
void setup() {Serial.begin(9600);
} void loop() {
Serial.print("H epikoinwnia ksekinhse");for(i=1;i
-
Serial.println(i);delay(100);
}for(i=1;i
-
int i;
void setup() {pinMode(redPin, OUTPUT);pinMode(greenPin, OUTPUT);pinMode(bluePin, OUTPUT);
}
void loop() {// analogWrite(redPin, 255);analogWrite(greenPin, 0);analogWrite(bluePin, 0);delay(2000);
// analogWrite(redPin, 0);analogWrite(greenPin, 255);analogWrite(bluePin, 0);delay(2000);
// analogWrite(redPin, 0);analogWrite(greenPin, 0);analogWrite(bluePin, 255);delay(2000);
}
8 T
int ledRed = 11;int ledOrange = 10;int ledGreen = 9;
void setup() {pinMode(ledRed, OUTPUT);pinMode(ledOrange, OUTPUT);pinMode(ledGreen, OUTPUT);
}
void loop() {// 3 digitalWrite(ledRed, HIGH);digitalWrite(ledOrange, LOW);digitalWrite(ledGreen, LOW);delay(3000);
// 5 digitalWrite(ledRed, LOW);
E. - Arduino . 52 64
-
digitalWrite(ledOrange, LOW);digitalWrite(ledGreen, HIGH);delay(5000);
// 1 digitalWrite(ledRed, LOW);digitalWrite(ledOrange, HIGH);digitalWrite(ledGreen, LOW);delay(1000);
}
9 T
int ledRed = 11;int ledOrange = 10;int ledGreen = 9;int pedRed = 4;int pedGreen = 3;
void setup() {pinMode(ledRed, OUTPUT);pinMode(ledOrange, OUTPUT);pinMode(ledGreen, OUTPUT);pinMode(pedRed, OUTPUT);pinMode(pedGreen, OUTPUT);
}
void loop() {// 3 , digitalWrite(ledRed, HIGH);digitalWrite(ledOrange, LOW);digitalWrite(ledGreen, LOW);digitalWrite(pedRed, LOW);digitalWrite(pedGreen, HIGH);delay(3000);
// , 1 digitalWrite(pedRed, HIGH);digitalWrite(pedGreen, LOW);delay(1000);
// 5 digitalWrite(ledRed, LOW);digitalWrite(ledOrange, LOW);digitalWrite(ledGreen, HIGH);delay(5000);
// 1 digitalWrite(ledRed, LOW);
E. - Arduino . 53 64
-
digitalWrite(ledOrange, HIGH);digitalWrite(ledGreen, LOW);delay(1000);
}
10 T
int ledRed = 11;int ledOrange = 10;int ledGreen = 9;int pedRed = 4;int pedGreen = 3;int btn = 5; // pin 5int lastPedPass = 0; // int btnState; //
void setup() {pinMode(ledRed, OUTPUT);pinMode(ledOrange, OUTPUT);pinMode(ledGreen, OUTPUT);pinMode(pedRed, OUTPUT);pinMode(pedGreen, OUTPUT);pinMode(btn, INPUT);
// digitalWrite(pedRed, HIGH);digitalWrite(pedGreen, LOW);digitalWrite(ledRed, LOW);digitalWrite(ledOrange, LOW);digitalWrite(ledGreen, HIGH);
}
void loop() {int btnState = digitalRead(btn);if (btnState == HIGH && (millis() - lastPedPass) > 5000) { // 5 sec
// 1 digitalWrite(ledRed, LOW);digitalWrite(ledOrange, HIGH);digitalWrite(ledGreen, LOW);delay(1000);
// 5 , 1 sec digitalWrite(ledRed, HIGH);digitalWrite(ledOrange, LOW);digitalWrite(ledGreen, LOW);delay(1000);digitalWrite(pedRed, LOW);
E. - Arduino . 54 64
-
digitalWrite(pedGreen, HIGH);delay(3000);
// , 1 digitalWrite(pedRed, HIGH);digitalWrite(pedGreen, LOW);delay(1000);
// 5 digitalWrite(ledRed, LOW);digitalWrite(ledOrange, LOW);digitalWrite(ledGreen, HIGH);lastPedPass = millis(); //
}}
11 , (Led chase effect)
// ,
int lPin1 = 6;int lPin2 = 7;int lPin3 = 8;int lPin4 = 9;int lPin5 = 10;int lPin6 = 11;int lPin7 = 12;int lPin8 = 13;
void setup() {pinMode(lPin1, OUTPUT);pinMode(lPin2, OUTPUT);pinMode(lPin3, OUTPUT);pinMode(lPin4, OUTPUT);pinMode(lPin5, OUTPUT);pinMode(lPin6, OUTPUT);pinMode(lPin7, OUTPUT);pinMode(lPin8, OUTPUT);
}
void loop() {digitalWrite(lPin1, HIGH);delay(200);digitalWrite(lPin1, LOW);digitalWrite(lPin2, HIGH);delay(200);digitalWrite(lPin2, LOW);digitalWrite(lPin3, HIGH);delay(200);
E. - Arduino . 55 64
-
digitalWrite(lPin3, LOW);digitalWrite(lPin4, HIGH);delay(200);digitalWrite(lPin4, LOW);digitalWrite(lPin5, HIGH);delay(200);digitalWrite(lPin5, LOW);digitalWrite(lPin6, HIGH);delay(200);digitalWrite(lPin6, LOW);digitalWrite(lPin7, HIGH);delay(200);digitalWrite(lPin7, LOW);digitalWrite(lPin8, HIGH);delay(200);digitalWrite(lPin8, LOW);
}
//
int ledPin[8]; // 8 int i;
void setup() {for (i=0;i
-
pinMode(ledPin[i], OUTPUT);};pinMode(potPin, INPUT);
}
void loop() {for (i=0;i
-
delay(50);}
}
15 & (Sounder & Light Sensor)
int sndPin = 10;int resPin = A1;int resVal = 0; // int ledVal = 0;
void setup() {pinMode(sndPin, OUTPUT);pinMode(resPin, INPUT);
}
void loop() {resVal = analogRead(resPin);ledVal = map(resVal, 0, 1023, 0, 255);analogWrite(sndPin, ledVal); // map, analogWrite(sndPin, resVal / 4);delay(50);
}
16 (Servos)
#include
int sPin = 10;Servo s;
void setup() {s.attach(sPin);
}
void loop() {s.write(90);delay(50);s.write(179);delay(50);s.write(90);delay(50);s.write(0);delay(50);
}
E. - Arduino . 58 64
-
17 (Servos)
#include
int sPin = 10;int potPin = A1; // pin
Servo s;int potVal = 0; // int sVal = 0;
void setup() {s.attach(sPin);pinMode(potPin, INPUT);
}
void loop() {potVal = analogRead(potPin);sVal = map(potVal, 0, 1023, 0, 180);s.write(sVal);delay(50);
}
18 (DC Motor)
int mtrPin = 10;int i; void setup() {
pinMode(mtrPin, OUTPUT);}
void loop() {digitalWrite(mtrPin, HIGH);delay(3000);digitalWrite(mtrPin, LOW);for (i=0; i
-
int speedPinA = 6;
// B int dir1PinB = 4;int dir2PinB = 2; int speedPinB = 3;
int speed = 255; int time = 5000; void setup() {
pinMode (dir1PinA, OUTPUT); pinMode (dir2PinA, OUTPUT); pinMode (speedPinA, OUTPUT); pinMode (dir1PinB, OUTPUT); pinMode (dir2PinB, OUTPUT); pinMode (speedPinB, OUTPUT);
} void loop() {
// analogWrite (speedPinA, speed); analogWrite (speedPinB, speed);digitalWrite (dir1PinA , HIGH); digitalWrite (dir2PinA, LOW); digitalWrite (dir1PinB, HIGH); digitalWrite (dir2PinB, LOW); delay(time);
// analogWrite (speedPinA, speed); analogWrite (speedPinB, speed);digitalWrite (dir1PinA, LOW); digitalWrite (dir2PinA, HIGH); digitalWrite (dir1PinB, LOW); digitalWrite (dir2PinB, HIGH);delay(time);
// digitalWrite (speedPinA, LOW); digitalWrite (speedPinB, LOW);digitalWrite (dir1PinA , LOW); digitalWrite (dir2PinA, LOW); digitalWrite (dir1PinB, LOW); digitalWrite (dir2PinB, LOW);delay(time);
// analogWrite (speedPinA, speed); analogWrite (speedPinB, LOW);
E. - Arduino . 60 64
-
digitalWrite (dir1PinA , HIGH); digitalWrite (dir2PinA, LOW); digitalWrite (dir1PinB, HIGH); digitalWrite (dir2PinB, LOW); delay(time);
// analogWrite (speedPinA, LOW); analogWrite (speedPinB, speed);digitalWrite (dir1PinA , HIGH); digitalWrite (dir2PinA, LOW); digitalWrite (dir1PinB, HIGH); digitalWrite (dir2PinB, LOW); delay(time);
// digitalWrite (speedPinA, 255); digitalWrite (speedPinB, 255);digitalWrite (dir1PinA , LOW); digitalWrite (dir2PinA, LOW); digitalWrite (dir1PinB, LOW); digitalWrite (dir2PinB, LOW);delay(time);
}
20
int echoPin = 12; // Echo Pinint trigPin = 13; // Trigger Pin
int maximumRange = 200; // Maximum range neededint minimumRange = 0; // Minimum range neededlong duration, distance; // Duration used to calculate distance void setup() {
pinMode(trigPin, OUTPUT);pinMode(echoPin, INPUT);Serial.begin(9600);
} void loop() {
/* , . */ digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH);delayMicroseconds(10); digitalWrite(trigPin, LOW);duration = pulseIn(echoPin, HIGH);
E. - Arduino . 61 64
-
// ( cm) .distance = duration/58.2;
if (distance >= maximumRange || distance
-
ISBN 978-960-93-6760-8 .
1. Arduino2. 3. Arduino IDE4. 5. / (Pins)6. - 6.1. 6.2. 6.3. (Pins)6.4. - 6.4.1. 6.4.2. 6.4.3. (PWM pins)6.4.4.
6.5. 6.5.1. delay()6.5.2. - delayMicroseconds()6.5.3. - millis()
6.6. - map()6.7. (Serial)6.8. 6.9. (For)
7. breadboard 8. 9. 9.1. - Leds9.1.1. - RGB Leds
9.2. - Buttons9.3. - Potentiometer9.4. Sounder9.5. Photoresistor (LDR)9.6. - Servos9.7. DC Motor9.7.1.
9.8. Ultrasonic sensor9.9. - Shields
10. 10.1. 1 To led 10.2. 2 (Fade in-Fade out)10.3. 3 (Fade in-Fade out), (Blink at peaks)10.4. 4 10.5. 5 - 10.6. 6 (RGB Led)10.7. 7 RGB Led10.8. 8 10.9. 9 10.10. 10 10.11. 11 , (Led chase effect)10.12. 12 , (Led chase effect) 10.13. 13 , (Led effect + light sensor)10.14. 14 (Sounder)10.15. 15 & (Sounder & Light Sensor)10.16. 16 (Servos)10.17. 17 (Servos) 10.18. 18 (DC Motor)10.19. 19 2 10.20. 20 - 10.21. 21
11. 11.1. 11.2.
1 To Led 2 (Fade in-Fade out) 3 (Fade in-Fade out), (Blink at peaks) 4 5 6 (RGB Led) 7 RGB Led 8 T 9 T 10 T 11 , (Led chase effect) 12 , (Led chase effect) 13 , (Led effect + light sensor) 14 (Sounder) 15 & (Sounder & Light Sensor) 16 (Servos) 17 (Servos) 18 (DC Motor) 19 2 20