Εφαρμογές Arduino
description
Transcript of Εφαρμογές Arduino
-
1 -7
2014
Arduino
1
-
arduino.
(12.10)
, ,
.
(Hands on Training).
arduino.
,
.
.
.
. , Arduino . 1,2 8
2
-
i
Arduino
: 1989 : 2084
: . ,
, 2012
3
-
vi
.............................................................................................................................................. vi
1 ............................................................................................................................................. 1
Arduino ....................................................................................................................................................... 1
2 ............................................................................................................................................. 4
Arduino Uno ............................................................................................................................................... 4
2.1 .......................................................................................................................................... 4
2.2 Arduino Uno ........................................................................................................... 4
2.3 ........................................................................................................................... 5
2.4 Arduino ................................................................................................... 6
2.5 ......................................................................................................................... 6
2.6 ............................................................................................................................... 7
2.7 ............................................................................................................................... 8
2.8 ..................................................................................................... 8
2.9 ........................................................................................... 8
2.10 Arduino ........................................................ 13
2.11 (Serial Monitor) ............................................................................................ 14
2.12 H ..................................................................................................... 15
2.13 ............................................................. 15
2.14 (Digital pins) .............................................................................................. 18
2.15 (Analog input pins) .................................................................. 19
2.16 (Libraries) ................................................................................... 20
4
-
viii
8 ......................................................................................................................................... 100
Processing ............................................................................................................................................. 100
8.1 Processing .......................................................................................... 100
8.2 Processing ..................................................................................... 100
8.3 ................................................................................................. 101
8.4 ..................................................................................................... 102
8.5 5: ....................... 103
9 ......................................................................................................................................... 110
................................................................................................. 110
5
-
1 : Arduino
1
1 Arduino
Arduino
Smart Project. , Arduino 2005 , Ivrea .
, Arduino
, . , , / . Arduino ( ) .
. . ; Arduino . , Arduino . , Projects ( hardware) software / ( Flash, Processing, MaxMSP). Arduino Atmega382 chip FTDI .
Arduino (BootLoader). Flash Memory Arduino USB .
, , Arduino .
6
-
1 : Arduino
2
Arduino:
: Arduino . , .
: Arduino .
: Arduino . , . , .
Arduino , :
Arduino
.
Arduino Uno Arduino Stamp Arduino Diecimila Arduino Fio Arduino Duemilanove Arduino NG Arduino Mega1280 Arduino NG+ Arduino Mega2560 Arduino Extreme Arduino Mini Arduino Bluetooth Arduino Nano LilyPad Arduino Arduino USB Serial Arduino
Arduino
Atmel AVR
Flash KiB
EEPROM KiB
SRAM KiB
/
PWM
Diecimila ATmega168 16 0.5 1 14 6 6 Duemilanove ATmega168/328 16 0.5 1 14 6 6
Uno ATmega328 32 1 2 14 6 6
Mega ATmega1280 128 4 8 54 14 16 Fio ATmega328P 32 1 2 14 6 8
Mega 2560 ATmega2560 256 4 8 54 14 16
7
-
1 : Arduino
3
1.1 Arduino
Arduino (IDE) Java . IDE Arduino , C, C++, Arduino, ..
, Arduino Wiring (C, C++). IDE Arduino GNU toolchain AVR Libc C, C++ AVR , avrudude Flash memory Arduino.
Arduino Creative Commons Attribution Share-Alike 2.5. , (IDE) Arduino GNU General Public License Version2.
8
-
2 : Arduino Uno
4
2 Arduino Uno
2.1
Arduino Uno . Arduino 14 input/output pins 6 . 20 pins (, LEDs, LCD ) (Ultrasonic, , accelometers .).
USB. , , . , .
2.2 Arduino Uno
Arduino 14 / - pinMode(), digitalWrite(), digitalRead() . 5 Volts 40mA. pin Pull-up 20-50K. , 6 . 0-1023. 0 5 volts. , 6 14 P3, P5, P6, P9, P10 P11 .
.
: 0 (RX) 1 (TX). (RX) (TX) TTL .
: 2 3. .
9
-
2 : Arduino Uno
5
attachInterrupt(). 0,1.
PWM: 3, 5, 6, 9, 10, 11 pins. 8-bit PWM analogWrite().
SPI: 10 (SS), 11 (MOSI), 12 (MISO), 13 (SCK). SPI, hardware Arduino.
LED: 13. 13 LED. HIGH, LED pin LOW .
2.3
Arduino . , . 0 (RX) 1 (TX), USB. , (USB), 0 1 .
, Arduino Arduino. Serial Monitor Serial.begin().
2.1 Arduino Uno
10
-
2 : Arduino Uno
6
:
begin() ( ) end() ( ) available() ( ) read() ( ) peek() ( byte ) flush() ( buffer ) print() ( ) println() ( Print(), ) write() ( )
2.4 Arduino
Microcontroller: ATmega328 : 5V : 7-12V (): 6-20V Digital I/O Pins: 14 ( 6 PWM
) Analog Input Pins: 6 DC I/O Pin: 40 mA DC 3.3V Pin: 50 mA Flash Memory: 32 KB 0,5 KB
bootloader SRAM: 2 KB EEPROM: 1 KB Clock Speed: 16 MHz
2.5
Arduino :
Flash memory (32 Kbytes) .
SRAM memory ( 2 Kbytes) .
EPROM memory (1 Kbytes) (OFF). Reset Arduino.
11
-
2 : Arduino Uno
7
, Flash EPROM ( ). SRAM .
SRAM, , . , , . , sketch ( ). , SRAM. , .
2.6
Arduino Uno 2.1mm ( ) Arduino USB . H . , 9Volt 220V. Arduino Vin GND . .
6 20 Volts. 7 Volt pin 5Volt 5 Volts. , 12 Volts . , 9 Volts.
:
VIN. . .
5V. 5Volt. . Vin , USB 5V
3V3. 50mA.
GND.
12
-
2 : Arduino Uno
8
2.7
rduino Uno , Arduino . ATMega382 L 5 Volt UART, ( RX) 0 ( TX) 1 .
, Arduino ATmega382 chip FTDI. , . Arduino USB COM . firmware ATmega382 USB COM . , Windows .inf .
Arduino Arduino. RX TX LED USB-to-chip USB ( 0 1).
2.8
Arduino Uno Wiring ( C++ ). . STK500.
Processing Wiring, (pen source). " ".
2.9
, , . Windows XP.
2.2 ATmega328
13
-
2 : Arduino Uno
9
1. Arduino USB Arduino Uno. USB .
2. Arduino Arduino-1.0 http://arduino.cc/en/Main/Software.
3. Arduino Uno USB. LED .
4. + , . + ,
.
14
-
2 : Arduino Uno
10
+ Arduino Uno. .
+ Arduino.
15
-
2 : Arduino Uno
11
+ drivers .
+ Arduino.
+ (COM & LPT) Serial Port COM 15 Arduino . Arduino 15.
16
-
2 : Arduino Uno
12
5. Arduino Arduino.exe
6. Board , Tools Board Arduino Uno COM15.
17
-
2 : Arduino Uno
13
2.10 Arduino
Arduino , , , , . Arduino .
sketch. sketch . / / . Arduino . , sketch, sketch .
(Tabs)
2.3 (IDE) Arduino
18
-
2 : Arduino Uno
14
:
Verify/Compile (/):
Upload:
New(): sketch
Open(): sketch,
Save(): sketch
Serial Monitor( ):
2.11 (Serial Monitor)
Arduino. , , send Enter. , , (baud) Arduino Serial.begin().
2.4 Serial Monitor
2.4: Serial Monitor
19
-
2 : Arduino Uno
15
2.12 H
Arduino :
// void setup () { // } void loop () { // }
sketch Arduino setup() loop(). setup() , sketch (reset) Arduino. , , (pins) . , loop() . sketch, .
2.13
, Arduino :
if ( ) if ... else ( ) for ( ) while ( ) do ... while ( ) switch ... case ( ) break ( ) continue ( ) return ( ) goto ( )
= ( ) + ( ) - ( ) * ( )
20
-
2 : Arduino Uno
16
/ ( ) % ( )
&& ( ) || ( ) ! ( )
& ( ) | ( ) ^ ( ) ~ ( ) > ( )
++ ( ) -- ( )
+=, -=, *=, /=, %= ( ) &=, |=, ^=, ~=, = ( )
== () != () < () > () = ( )
* ( ) & ( )
HIGH ( ) LOW ( ) false ( ) true ( ) INPUT ( ) OUTPUT ( ) A0, ..., 5 ( )
21
-
2 : Arduino Uno
17
boolean ( ) char ( 8 ) unsigned char ( 8 ) byte ( 8 ) int ( 16 ) unsigned int ( 16 ) word ( 16 ) long ( 32 ) unsigned long ( 32 ) float, double ( ) String ( )
char(), byte() int(), word(), long() float(), double()
pinMode() ( )
digitalWrite() ( ) digitalRead() ( )
analogReference() ( ) analogWrite() ( PWM ) analogRead() ( )
tone() ( ) noTone() ( ) shiftOut() ( ) pulseIn() ( s HIGH LOW)
millis() ( ms) micros() ( s) delay() ( - ms) delayMicroseconds() ( - s)
max() ( ) min() ( ) abs() ( ) constrain() ( ) map() ( ) pow() ( )
22
-
2 : Arduino Uno
18
sqrt() ( ) sin() ( ) cos() ( ) tan() ( )
random() ( ) randomSeed() ( )
lowByte() ( byte ) highByte() ( byte ) bitRead() ( ) bitWrite() ( ) bitSet() ( 1 ) bitClear() ( 0 ) bit() ( 2)
attachInterrupt() ( ) detachInterrupt() ( )
interrupts() ( ) noInterrupts() ( )
Serial ( )
2.14 (Digital pins)
Arduino , . , Arduino (Atmega), , . :
pinMode(): /
. : pinMode(pin, mode) :
pin: . mode: INPUT/OUTPUT
23
-
2 : Arduino Uno
19
digitalWrite(): (HIGH) (LOW) . pinMode(), : 5V HIGH 0V LOW. , HIGH digitalWrite() pullup- 20 LOW . : digitalWrite(pin,value) :
pin: .
Value: INPUT/OUTPUT
digitalRead(): , HIGH LOW. : digitalRead(pin) :
pin: .
: HIGH/LOW
2.15 (Analog input pins)
Atmega Arduino -- 6 . 10 bit, 0 1023. Arduino , /. :
analogWrite(): (PWM ) . LED . analogWrite(), analogWrite() ( digitalWrite() digitalRead() ). PWM 490 Hz. Arduino 3, 5, 6, 9, 10, 11. : analogWrite(pin, value) : pin: value: 0 255
24
-
2 : Arduino Uno
20
analogRead(): . : analogRead(pin) : pin: : 0 1023
2.16 (Libraries)
. sketch, Sketch Import Library. #include sketch. sketch, . sketch , .
, libraries (), sketchbook. .
Arduino.
( ): Messenger -
. NewSoftSerial - SoftwareSerial. OneWire - ( Dallas Semiconductor)
one Wire. PS2Keyboard - PS2. Simple Message System - Arduino SSerial2Mobile - e-mail
( AT ) Webduino - web server ( Arduino
Ethernet Shield) X10 - X10 Xbee - XBees API SerialControl - Arduino Servo Servo.
: Capacitive Sensing - Debounce - (..
).
25
-
2 : Arduino Uno
21
LED: Improved LCD library - LCD
Arduino LCD . GLCD LCD KS0108 chipset. LedControl - LED MAX7221
MAX7219. LedControl - Matrix
LED. LedDisplay - HCMS-29xx LED.
: Tone -
.
PWM: TLC5940 - 16 12 bit PWM .
: DateTime -
. MsTime2 - 2
.
: Streaming - .
[1], [2], [3], [4] [5].
26
-
8: Processing
100
8 Processing
Processing , .
, , . , , , Processing , .
8.1 Processing
/ GPL/LGPL. ,
GNU/Linux, Mac OS X Windows. , (2D)
(3D) . OpelGL 3D. stand-alone desktop Web-based
(applets). ,
, , ..
Processing Java, .
8.2 Processing
Processing .
27
-
8: Processing
101
Wiring Arduino, hardware. Wiring, Arduino , , Processing AVR Atmel. Processing : Design By Numbers, Fritzing, Mobile Processing, Processing.js, Spde, Processing in Clojure Processing Monsters.
8.3
Processing :
// void setup() { // } void draw() { // }
(Tabs)
9.1: (IDE) Processing
28
-
8: Processing
102
, Processing. setup() . , , , .. setup() , draw() . draw() setup() noLoop().
8.4
Processing , . , , , , , .. http://processing.org/reference/libraries. , Tools, .
Arduinoscope http://code.google.com/p/arduinoscope/downloads/list, ControlP5 http://www.sojamo.de/libraries/controlP5/. processing-1.5\modes\java\libraries ControlP5 arduinoscope ( ).
[22], [23].
29
-
9:
110
9
:
[] . (2009). , . [] Banzi, M. (2009). Getting Started with Arduino. O'Reilly.
: Arduino
[1] http://arduino.cc/en/Guide/Environment?from=Tutorial.Bootloader
[2] http://arduino.cc/en/Guide/Windows
[3] http://arduino.cc/en/Reference/HomePage
[4] http://arduino.cc/en/Main/ArduinoBoardUno
[5] http://arduino.cc/en/Tutorial/Memory
30
-
9:
111
Processing
[22] http://www.arduino.cc/playground/Interfacing/Processing [23] http://www.processing.org/reference/
31
-
3
o www.arduino.cc o ARDUINO IDE o ARDUINO IDE ARDUINO UNO
3.1
:
Arduino Uno
BreadBroad
USB Arduino
: , LED,
32
-
3.2 1
Led 1sec, / . ( - )
Arduino pin, , . , . pin HIGH LOW. HIGH '1' 5V DC, LOW '0' 0V DC (ground). pin
Arduino. led board
Blink
Arduino
: LED.
Arduino Board
breadboard
LED, 220,
, 220 pin 13.
() . ()
. Arduino , Arduino,
. Arduino LED pin 13.
LED , LED
.
33
-
Fritzing. ,
: Fritzing project page
34
-
[Get Code]
// Pin 13 has an LED connected on most Arduino boards. // give it a name: int led = 13; // Integer () led 13 led. // the setup routine runs once when you press reset: void setup() { // // initialize the digital pin as an output. pinMode(led, OUTPUT); // led, pin 13 0-5V } // the loop routine runs over and over again forever: void loop() { // digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) // 1 (5V) pin 13, led delay(1000); // wait for a second // 1000ms digitalWrite(led, LOW); // turn the LED off by making the voltage LOW // 0 (0V) pin 13, led delay(1000); // wait for a second // 1000ms } // digitalWrite(led, HIGH);
35
-
3.3 2
Led button polling
36
-
// constants won't change. They're used here to
// set pin numbers: const int buttonPin = 2; // the number of the pushbutton pin
// Integer () buttonPin 2
button. const int ledPin = 13; // the number of the LED pin
// Integer () ledPin 13
led. // variables will change:
int buttonState = 0; // variable for reading the pushbutton status // ? Integer () buttonState
0 button (on-off)
void setup() { // // initialize the LED pin as an output:
pinMode(ledPin, OUTPUT); // ledPin, pin 13 0-5V
// initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); // buttonPin, pin 2
0-5V }
void loop(){ // // read the state of the pushbutton value:
buttonState = digitalRead(buttonPin); // Pin 2 ( button) buttonState
// check if the pushbutton is pressed. // if it is, the buttonState is HIGH:
if (buttonState == HIGH) { // button , : // turn LED on:
digitalWrite(ledPin, HIGH); // 1 (5V) pin 13, led }
else { //: // turn LED off:
digitalWrite(ledPin, LOW); // 0 (0V) pin 13, led } }
37
-
3.4 3
Led button Interrupt ( loop )
const byte LED = 13; // Integer () LED 13 led. const byte BUTTON = 2; // Integer () BUTTON 2 button. // Interrupt Service Routine (ISR) void pinChange () // pinChange ( { noInterrupts (); // if (digitalRead (BUTTON) == HIGH) // Pin 2 ( button) button , digitalWrite (LED, HIGH); // 1 (5V) pin 13, led else //: digitalWrite (LED, LOW); // 0 (0V) pin 13, led interrupts (); // } // end of pinChange void setup () // { pinMode (LED, OUTPUT); // so we can update the LED // LED, pin 13 0-5V digitalWrite (BUTTON, HIGH); // internal pull-up resistor // BUTTON, pin 2 0-5V attachInterrupt (0, pinChange, CHANGE); // attach interrupt handler // . 0 . pinChange ISR . LOW, RISING, FALLING, CHANGE . } // end of setup void loop () // { // loop doing nothing // }
38
-
Below is a list of interrupts, in priority order, for the Atmega328: 1 Reset 2 External Interrupt Request 0 (pin D2) (INT0_vect) 3 External Interrupt Request 1 (pin D3) (INT1_vect) 4 Pin Change Interrupt Request 0 (pins D8 to D13) (PCINT0_vect) 5 Pin Change Interrupt Request 1 (pins A0 to A5) (PCINT1_vect) 6 Pin Change Interrupt Request 2 (pins D0 to D7) (PCINT2_vect) 7 Watchdog Time-out Interrupt (WDT_vect) 8 Timer/Counter2 Compare Match A (TIMER2_COMPA_vect) 9 Timer/Counter2 Compare Match B (TIMER2_COMPB_vect) 10 Timer/Counter2 Overflow (TIMER2_OVF_vect) 11 Timer/Counter1 Capture Event (TIMER1_CAPT_vect) 12 Timer/Counter1 Compare Match A (TIMER1_COMPA_vect) 13 Timer/Counter1 Compare Match B (TIMER1_COMPB_vect) 14 Timer/Counter1 Overflow (TIMER1_OVF_vect) 15 Timer/Counter0 Compare Match A (TIMER0_COMPA_vect) 16 Timer/Counter0 Compare Match B (TIMER0_COMPB_vect) 17 Timer/Counter0 Overflow (TIMER0_OVF_vect) 18 SPI Serial Transfer Complete (SPI_STC_vect) 19 USART Rx Complete (USART_RX_vect) 20 USART, Data Register Empty (USART_UDRE_vect) 21 USART, Tx Complete (USART_TX_vect) 22 ADC Conversion Complete (ADC_vect) 23 EEPROM Ready (EE_READY_vect) 24 Analog Comparator (ANALOG_COMP_vect) 25 2-wire Serial Interface (I2C) (TWI_vect) 26 Store Program Memory Ready (SPM_READY_vect)
Arduino pins
Most Arduino boards have two external interrupts: numbers 0 (on digital pin 2) and 1 (on digital pin 3). The table
below shows the available interrupt pins on various boards. Board int.0 int.1 int.2 int.3 int.4 int.5 Uno, Ethernet 2 3 Mega2560 2 3 21 20 19 18 Leonardo 3 2 0 1 7 Due (see below)
The Arduino Due board has powerful interrupt capabilities that allows you to attach an interrupt function on all
available pins. You can directly specify the pin number in attachInterrupt().
39
-
3.5 4
, Led1 Led2 PWM .
-
: int sensorPin = A0; // select the input pin for the potentiometer
// Integer () sensorPin 0 ( 0 pin, 1 ADC) .
int ledPin1 = 13; // select the pin for the LED1 // Integer () ledPin1 13 led1. int sensorValue = 0; // variable to store the value coming from the sensor
// Integer () sensorValue 0 ADC
int ledPin2 = 9; // LED2 connected to digital pin 9 // Integer () ledPin2 9 led2. PWM Output int outputValue = 0; // value output to the PWM (analog out)
// Integer () outputValue 0 led2
void setup() { // // declare the ledPin1 as an OUTPUT: pinMode(ledPin1, OUTPUT); // ledPin1, pin 13 0-5V pinMode(ledPin2, OUTPUT); // ledPin2, pin 9 0-5V } void loop() { // // read the value from the sensor: sensorValue = analogRead(sensorPin); // pin 0 ADC sensorValue // turn the ledPin on digitalWrite(ledPin1, HIGH); // 1 (5V) pin 13, led1 // stop the program for milliseconds: delay(sensorValue); // ms 0 1023 ms // turn the ledPin off: digitalWrite(ledPin1, LOW); // 0 (0V) pin 13, led1 // stop the program for for milliseconds:
41
-
delay(sensorValue); // ms 0 1023 ms //fade section // map it to the range of the analog out: outputValue = map(sensorValue, 0, 1023, 0, 255); // 0 1023 0-255 outputValue // change the analog out value: analogWrite(ledPin2, outputValue); // PWM pin 9 (led2) ADC 0 255 // wait 2 milliseconds before the next loop // for the analog-to-digital converter to settle // after the last reading: delay(2); // // 2ms ADC }
, . analogWrite() 0 255, , analogWrite(255) 100% , analogWrite(127) 50% ( ) [12].
(PWM)
42
-
3.6 5
, Led1 Led2 , /
:
int sensorPin = A0; // select the input pin for the potentiometer // Integer () sensorPin 0 ( 0 pin, 1 ADC) . int ledPin1 = 13; // select the pin for the LED1 // Integer () ledPin1 13 led1. int sensorValue = 0; // variable to store the value coming from the sensor // Integer () sensorValue 0 ADC int ledPin2 = 9; // LED2 connected to digital pin 9 // Integer () ledPin2 9 led2. int outputValue = 0; // value output // Integer () outputValue 0 void setup() { // // declare the ledPin1 as an OUTPUT: pinMode(ledPin1, OUTPUT); // ledPin1, pin 13 0-5V
43
-
pinMode(ledPin2, OUTPUT); // ledPin1, pin 9 0-5V // initialize serial communications at 9600 bps: Serial.begin(9600); // 9600 } void loop() { // // read the value from the sensor: sensorValue = analogRead(sensorPin); // pin 0 ADC sensorValue // turn the ledPin on if (sensorValue < 511 ) { // ADC 511
2,5 volt : digitalWrite (ledPin1, HIGH); // 1 (5V) pin 13, led1 digitalWrite (ledPin2, LOW); // 0 (0V) pin 9, led2
}
else //: {
digitalWrite (ledPin1, LOW); // 0 (0V) pin 13, led1
digitalWrite(ledPin2, HIGH); // 1 (5V) pin 9, led2 } //serial section // print the results to the serial monitor: // Serial.print("\t sensor = " ); Serial.println(sensorValue); // wait 2 milliseconds before the next loop // for the analog-to-digital converter to settle // after the last reading: delay(2); }
44
-
3.7 6
, EEPROM, / EEPROM
: #include // EEPROM // the current address in the EEPROM (i.e. which byte // we're going to write to next) int addr = 0; // addr 0 // start reading from the first byte (address 0) of the EEPROM int address = 0; // address 0 byte value; // byte value int sensorPin = A0; // select the input pin for the potentiometer
// Integer () sensorPin 0 ( 0 pin, 1 ADC) .
int sensorValue = 0; // variable to store the value coming from the sensor
// Integer () sensorValue 0 ADC
void setup() { //
Serial.begin(9600); // 9600 }
void loop() { // // read the value from the sensor: sensorValue = analogRead(sensorPin);
); // pin 0 ADC sensorValue
45
-
// need to divide by 4 because analog inputs range from // 0 to 1023 and each byte of the EEPROM can only hold a // value from 0 to 255. int val = analogRead(sensorPin) / 4; // val pin 0 ADC 4 8 bit // write the value to the appropriate byte of the EEPROM. // these values will remain there when the board is // turned off. EEPROM.write(addr, val); // addr, val // advance to the next address. there are 512 bytes in // the EEPROM, so go back to 0 when we hit 512. addr = addr + 1; // ( ) if (addr == 512) // EEPROM 512 byte, : addr = 0; // delay(100); // 100ms value = EEPROM.read(address); // address, value Serial.print(address); // Serial.print("\t"); Serial.print(value, DEC); // Serial.println(); // advance to the next address of the EEPROM address = address + 1; // ( ) // there are only 512 bytes of EEPROM, from 0 to 511, so if we're // on address 512, wrap around to address 0 if (address == 512) // EEPROM 512 byte, : address = 0; // delay(500); // 500ms }
46
-
3.8 7
Twinkle Twinkle Little Star
:
int speakerPin = 9; // speakerPin 9 buzzer int length = 15; // the number of notes // length 15 char notes[] = "ccggaagffeeddc "; // a space represents a rest // (char) ccggaagffeeddc int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 }; // 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 int tempo = 300; // tempo 300 () void playTone(int tone, int duration) { // playTone tone (khz) duration for (long i = 0; i < duration * 1000L; i += tone * 2) { digitalWrite(speakerPin, HIGH); delayMicroseconds(tone); digitalWrite(speakerPin, LOW);
47
-
delayMicroseconds(tone); } } void playNote(char note, int duration) {// playNote note duration o char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; // int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 }; // play the tone corresponding to the note name for (int i = 0; i < 8; i++) { if (names[i] == note) { playTone(tones[i], duration); // playTone } } } void setup() { pinMode(speakerPin, OUTPUT); } void loop() { for (int i = 0; i < length; i++) { if (notes[i] == ' ') { delay(beats[i] * tempo); // rest } else { playNote(notes[i], beats[i] * tempo); } // pause between notes delay(tempo / 2); } }
48
-
3.9 8
Display LCD
49
-
: /* LiquidCrystal Library - Hello World Demonstrates the use a 16x2 LCD display. The LiquidCrystal library works with all LCD displays that are compatible with the Hitachi HD44780 driver. There are many of them out there, and you can usually tell them by the 16-pin interface. This sketch prints "Hello World!" to the LCD and shows the time. The circuit: * LCD RS pin to digital pin 7 * LCD Enable pin to digital pin 6 * LCD D4 pin to digital pin 5 * LCD D5 pin to digital pin 4 * LCD D6 pin to digital pin 3 * LCD D7 pin to digital pin 2 * LCD R/W pin to ground * 10K resistor: * ends to +5V and ground * wiper to LCD VO pin (pin 3) Library originally added 18 Apr 2008 by David A. Mellis library modified 5 Jul 2009 by Limor Fried (http://www.ladyada.net) example added 9 Jul 2009 by Tom Igoe modified 22 Nov 2010 by Tom Igoe This example code is in the public domain. http://www.arduino.cc/en/Tutorial/LiquidCrystal */ // include the library code: #include // initialize the library with the numbers of the interface pins
50
-
LiquidCrystal lcd(7, 6, 5, 4, 3, 2); void setup() { // set up the LCD's number of columns and rows: lcd.begin(16, 2); // Print a message to the LCD. lcd.print("hello, world!"); } void loop() { // set the cursor to column 0, line 1 // (note: line 1 is the second row, since counting begins with 0): lcd.setCursor(0, 1); // print the number of seconds since reset: lcd.print(millis() / 1000); }
....
.
51
-
-
52
-
arduino
arduino
53
-
ATmega328
54
-
Hitachi HD44780 driver
55
-
56