더 재밌게 놀기 연구소
※ Illuminance sensor with LED
Need to use Analog pin of Arduino
Circuit
Source Code
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
analogWrite(9,map(analogRead(A0),0,1023,0,255);
}
※ Illuminance sensor with Buzzer
Circuit
Source Code
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
tone(8,map(analogRead(A0),0,1023,31,4978),20);
delay(500);
}
Youtube Tutorial of above examples (in Korean) : https://www.youtube.com/watch?v=w397cxZQ5IA&index=6&list=PL0Vl139pNHbe-JlsydLg3NFRk6nC_cC7w
Circuit
Source Code
#define TRIG 2
#define ECHO 3
#define RED 11
#define RED2 12
#define GREEN 10
#define BLUE 9
void setup() {
// put your setup code here, to run once:
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(TRIG,LOW);
delayMicroseconds(2);
digitalWrite(TRIG,HIGH);
delayMicroseconds(10);
digitalWrite(TRIG,LOW);
long distance = pulseIn(ECHO, HIGH)/58.2;
analogWrite(RED, 0);
analogWrite(GREEN, 0);
analogWrite(BLUE, 0);
if(distance < 10) {
analogWrite(RED2,255);
}else if(distance < 20) {
analogWrite(GREEN,255);
analogWrite(RED2,0);
} else if(distance < 30) {
analogWrite(BLUE,255);
analogWrite(RED2,0);
}
delay(100);
}
Note : Red of 3 color LED of mine is not working properly so I've added Red LED to port number 12.
※ Ultrasonic Sensor with Buzzer
Circuit
Source Code
#define TRIG 2
#define ECHO 3
void setup() {
// put your setup code here, to run once:
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(TRIG,LOW);
delayMicroseconds(2);
digitalWrite(TRIG,HIGH);
delayMicroseconds(10);
digitalWrite(TRIG,LOW);
long distance = pulseIn(ECHO, HIGH)/58.2;
tone(4,1000,20);
delay(100);
tone(4,1000,20);
delay(distance);
}
Youtube Tutorial of above examples (in Korean) : https://www.youtube.com/watch?v=XzNHUctzjOM&list=PL0Vl139pNHbe-JlsydLg3NFRk6nC_cC7w&index=7
Application
Let's add some more LEDs and make Buzzer Piano with Ultrasonic sensor. Wow
#define TRIG 2
#define ECHO 3
#define RED 11
#define RED2 12
#define GREEN 10
#define GREEN2 7
#define YELLOW 8
#define BLUE 9
#define BLUE2 5
#define NOTE_C4 262
#define NOTE_D4 294
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_G4 392
#define NOTE_A4 440
int pins[] = {2,3,4};
int notes[] = {NOTE_E4, NOTE_D4, NOTE_C4};
void setup() {
// put your setup code here, to run once:
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(TRIG,LOW);
delayMicroseconds(2);
digitalWrite(TRIG,HIGH);
delayMicroseconds(10);
digitalWrite(TRIG,LOW);
long distance = pulseIn(ECHO, HIGH)/58.2;
analogWrite(RED, 0);
analogWrite(GREEN, 0);
analogWrite(BLUE, 0);
if(distance < 10) {
analogWrite(RED2,255);
tone(4,NOTE_C4,20);
}else if(distance < 15) {
analogWrite(GREEN,255);
analogWrite(BLUE2,0);
analogWrite(RED2,0);
analogWrite(YELLOW,0);
analogWrite(GREEN2,0);
tone(4,NOTE_D4,20);
} else if(distance < 20) {
analogWrite(BLUE,255);
analogWrite(BLUE2,0);
analogWrite(RED2,0);
analogWrite(YELLOW,0);
analogWrite(GREEN2,0);
tone(4,NOTE_E4,20);
} else if(distance < 25) {
analogWrite(YELLOW,255);
analogWrite(BLUE2,0);
analogWrite(RED2,0);
analogWrite(GREEN2,0);
tone(4,NOTE_F4,20);
} else if(distance < 30) {
analogWrite(GREEN2,255);
analogWrite(BLUE2,0);
analogWrite(RED2,0);
analogWrite(YELLOW,0);
tone(4,NOTE_G4,20);
} else if(distance < 30) {
analogWrite(BLUE2,255);
analogWrite(RED2,0);
analogWrite(YELLOW,0);
analogWrite(GREEN2,0);
tone(4,NOTE_A4,20);
}
delay(100);
}
'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] Tri LED and Buzzer (0) | 2017.01.09 |
[Arduino] Arduino Basic and control button and LEDs (0) | 2017.01.08 |