반응형
블로그 이미지
개발자로서 현장에서 일하면서 새로 접하는 기술들이나 알게된 정보 등을 정리하기 위한 블로그입니다. 운 좋게 미국에서 큰 회사들의 프로젝트에서 컬설턴트로 일하고 있어서 새로운 기술들을 접할 기회가 많이 있습니다. 미국의 IT 프로젝트에서 사용되는 툴들에 대해 많은 분들과 정보를 공유하고 싶습니다.
솔웅

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함

카테고리


반응형


더 재밌게 놀기 연구소



Illuminance sensor with LED


Need to use Analog pin of Arduino

Analog input pins

AnalogRead


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






Ultrasonic Sensor with LED


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);
}





반응형