Arduino Basic
※ Open Source Hardware
- Arduino UNO : 8 bit CPU
- Arduino TRE : Arduino + Beaglebone black
- Arduino Yun :(Built-in Wifi
- Netduino
- Raspberry Pi : 32 bit, Linux, 700 MB
- Beaglebone black : 32 bit, 1 GB, 512 MB Memory, Android O
- Intel Galileo : Pentium CPU, can share Arduino shield
※ IDE
- Arduino Sketch : Language - C/C++
- Arduino Scratch : GNU Development Tool
- Arduino Processing : for Art
◎ Control Arduino Built-in LED
Source Code (Sketch Sample : Blink)
`/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
the correct LED pin independent of which board is used.
If you want to know what pin the on-board LED is connected to on your Arduino model, check
the Technical Specs of your board at https://www.arduino.cc/en/Main/Products
This example code is in the public domain.
modified 8 May 2014
by Scott Fitzgerald
modified 2 Sep 2016
by Arturo Guadalupi
modified 8 Sep 2016
by Colby Newman
*/
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
==> The built-in LED will blink every second.
◎ Control External LED
circuit diagram
Source Code : Same Source Code as above one
==> The External LED will blink every second.
Youtube Tutorial of above examples (in Korean) : https://www.youtube.com/watch?v=YcAdhH3Y-HU&list=PL0Vl139pNHbe-JlsydLg3NFRk6nC_cC7w&index=2
◎ Control multiple external LEDs
LED : Light-emitting diode
circuit diagram
Source Code
#define DELAY_TIME 100
void setup() {
// put your setup code here, to run once:
pinMode(2. OUTPUT);
pinMode(3. OUTPUT);
pinMode(4. OUTPUT);
pinMode(5. OUTPUT);
pinMode(6. OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(2, HIGH);
delay(DELAY_TIME);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
delay(DELAY_TIME);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
delay(DELAY_TIME);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
delay(DELAY_TIME);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
delay(DELAY_TIME);
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
delay(DELAY_TIME);
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
delay(DELAY_TIME);
digitalWrite(4, LOW);
digitalWrite(3, HIGH);
delay(DELAY_TIME);
digitalWrite(3, LOW);
}
==> The LEDs will turn on and off sequentially.
Refactoring above code using FOR statement
#define DELAY_TIME 100
void setup() {
// put your setup code here, to run once:
for(int thisPin = 2; thisPin < 7; thisPin++) {
pinMode(thisPin,OUTPUT);
}
}
void loop() {
// put your main code here, to run repeatedly:
// loop from the lowest pin to the highest:
for (int thisPin = 2; thisPin < 7; thisPin++) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(DELAY_TIME);
// turn the pin off:
digitalWrite(thisPin, LOW);
}
// loop from the highest pin to the lowest:
for (int thisPin = 6; thisPin >= 2; thisPin--) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(DELAY_TIME);
// turn the pin off:
digitalWrite(thisPin, LOW);
}
}
◎ Button Control
circuit diagram
Source Code
#define LED 12
#define BUTTON 7
void setup() {
// put your setup code here, to run once:
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(BUTTON) == HIGH) {
digitalWrite(LED, HIGH);
delay(500);
digitalWrite(LED,LOW);
}
}
==> If press the button then turn on the LED for 0.5 second.
Youtube Tutorial of above examples (in Korean) : https://www.youtube.com/watch?v=wtbjzGIQe_I&index=3&list=PL0Vl139pNHbe-JlsydLg3NFRk6nC_cC7w
Application
I am going to combine both Button control and Multiple LEDs control source codes.
Source Code
#define BUTTON 8
#define DELAY_TIME 100
void setup() {
// put your setup code here, to run once:
for(int thisPin = 2; thisPin < 7; thisPin++) {
pinMode(thisPin,OUTPUT);
}
pinMode(BUTTON, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(BUTTON) == HIGH) {
// loop from the lowest pin to the highest:
for (int thisPin = 2; thisPin < 7; thisPin++) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(DELAY_TIME);
// turn the pin off:
digitalWrite(thisPin, LOW);
}
// loop from the highest pin to the lowest:
for (int thisPin = 6; thisPin >= 2; thisPin--) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(DELAY_TIME);
// turn the pin off:
digitalWrite(thisPin, LOW);
}
}
}
==> The LEDs will turn on and off sequentially when press button
'IoT > Arduino' 카테고리의 다른 글
[Arduino] Motion Sensor and Water Level Sensor (0) | 2017.01.17 |
---|---|
[Arduino] Joystick, LED Dot Matrix and ADXL335 Module (0) | 2017.01.17 |
[Arduino] Ultrasonic, Keypad and Temperature and Humidity Sensor (0) | 2017.01.15 |
[Arduino] 7 Segment, Servo, LCD and Thermometer (0) | 2017.01.14 |
[Arduino] Using 74HC595 8-bit serial-in/serial or parallel-out shift register (0) | 2017.01.12 |
[Arduino] Library and RGB LED (0) | 2017.01.11 |
[Arduino] Servo Motor (0) | 2017.01.10 |
[Arduino] LCD and 7 Segment (0) | 2017.01.10 |
[Arduino] Illuminance (Photocell) and Ultrasonic Sensor (0) | 2017.01.09 |
[Arduino] Tri LED and Buzzer (0) | 2017.01.09 |