조도 아날로그 0~1023값
초음파
vcc - 5v
trig - 발사 (아웃) - 디지털
echo - 인식 (인) - 디지털
ground - ground
조도센서를 이용한 LED 밝기 조절
#define LIGHT A0
#define LED 6
void setup() {
// put your setup code here, to run once:
pinMode(LIGHT,INPUT);
pinMode(LED,OUTPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(map(analogRead(LIGHT),0,1023,0,255));
analogWrite(LED,map(analogRead(LIGHT),0,1023,0,255));
}
초음파 센서를 이용한 거리측정
#define TRIG 4
#define ECHO 5
#define R 9
#define G 10
#define B 11
void setup() {
// put your setup code here, to run once:
pinMode(R,OUTPUT);
pinMode(G,OUTPUT);
pinMode(B,OUTPUT);
pinMode(TRIG,OUTPUT);
pinMode(ECHO,INPUT);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
delayMicroseconds(10);
digitalWrite(TRIG,HIGH);
delayMicroseconds(10);
digitalWrite(TRIG,LOW);
long duration = pulseIn(ECHO,HIGH)/58.2; //58.2로 나눔 cm
if(duration<5){
analogWrite(R,0);
analogWrite(G,0);
analogWrite(B,0);
}
else if(duration<10)
{
analogWrite(R,255);
analogWrite(G,0);
analogWrite(B,0);
}
else if(duration<20)
{
analogWrite(R,0);
analogWrite(G,255);
analogWrite(B,0);
}
else
{
analogWrite(R,0);
analogWrite(G,0);
analogWrite(B,255);
}
Serial.println(duration);
delay(200);
}