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

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함

카테고리

'2017/01/09'에 해당되는 글 2

  1. 2017.01.09 [Arduino] Illuminance (Photocell) and Ultrasonic Sensor
  2. 2017.01.09 [Arduino] Tri LED and Buzzer


반응형


더 재밌게 놀기 연구소



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





반응형

[Arduino] Tri LED and Buzzer

2017. 1. 9. 01:33 | Posted by 솔웅


반응형

PWM (Pulse Width Modulation)
Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of "on time" is called the pulse width. To get varying analog values, you change, or modulate, that pulse width. If you repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is a steady voltage between 0 and 5v controlling the brightness of the LED.




※ Tri LED control


Circuit


Source Code


#define RED 11
#define GREEN 10
#define BLUE 9

void setup() {
  // put your setup code here, to run once:
  randomSeed(analogRead(0));
}

void loop() {
  // put your main code here, to run repeatedly:
  analogWrite(RED, random(255));
  analogWrite(GREEN, random(255));
  analogWrite(BLUE, random(255));
  delay(1000);
}



※ Control Tri LED using Buttons




Source Code


#define RED 11
#define GREEN 10
#define BLUE 9
#define RED_BUTTON 4
#define GREEN_BUTTON 3
#define BLUE_BUTTON 2

int r=0, g=0, b=0;

void setup() {
  // put your setup code here, to run once:
  pinMode(RED_BUTTON, INPUT);
  pinMode(GREEN_BUTTON, INPUT);
  pinMode(BLUE_BUTTON, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(digitalRead(RED_BUTTON) == HIGH) {
    ++r;
    if(r>255) {
      r=0;
    }
  }

   if(digitalRead(GREEN_BUTTON) == HIGH) {
    ++g;
    if(g>255) {
      g=0;
    }
  }

  if(digitalRead(BLUE_BUTTON) == HIGH) {
    ++b;
    if(b>255) {
      b=0;
    }
  }

  analogWrite(RED,r);
  analogWrite(GREEN,g);
  analogWrite(BLUE,b);
  delay(10);
}


Youtube Tutorial of above examples (in Korean) : https://www.youtube.com/watch?v=7h8TJtRekNI&list=PL0Vl139pNHbe-JlsydLg3NFRk6nC_cC7w&index=4





※ Passive Buzzer (piezo speaker)




Source Code


To include pitches.h, first copy the file to Sketch.

(Examples - 02. Digital - toneMelody)


#include "pitches.h"

int melody[] = {
  NOTE_G4,
  NOTE_G4,
  NOTE_A5,
  NOTE_A5,
  NOTE_G4,
  NOTE_G4,
  NOTE_E4,
  NOTE_G4,
  NOTE_G4,
  NOTE_E4,
  NOTE_E4,
  NOTE_D4,
  0,
  NOTE_G4,
  NOTE_G4,
  NOTE_A5,
  NOTE_A5,
  NOTE_G4,
  NOTE_G4,
  NOTE_E4,
  NOTE_G4,
  NOTE_E4,
  NOTE_D4,
  NOTE_E4,
  NOTE_C4,
  0 };

  int noteDurations[] = {
  1,1,1,1,
  1,1,2,
  1,1,1,1,
  3,1,
  1,1,1,1,
  1,1,2,
  1,1,1,1,
  3,1
  };
 
void setup() {
  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly:
  for(int thisNote = 0; thisNote < 26; thisNote++) {
    int noteDuration = 250 * noteDurations[thisNote];
    tone(8, melody[thisNote], noteDuration);

    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    noTone(8);
  }
}


==> Buzzer (Piezo speaker) will play the Korean children song.





※ Let's make a Piano using the Buzzer


Circuit


Source Code


#define NOTE_C4 262
#define NOTE_D4 294
#define NOTE_E4 330

int pins[] = {2,3,4};
int notes[] = {NOTE_E4, NOTE_D4, NOTE_C4};

void setup() {
  // put your setup code here, to run once:
  for(int i=0; i<3; i++) {
    pinMode(pins[i], INPUT);
  }
  pinMode(8, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  for(int i=0; i<3; i++) {
    if(digitalRead(pins[i] == HIGH)) {
      tone(8, notes[i], 20);
    }
  }
}


Youtube Tutorial of above examples (in Korean) : https://www.youtube.com/watch?v=irbyDUGmNYk&index=5&list=PL0Vl139pNHbe-JlsydLg3NFRk6nC_cC7w

반응형
이전 1 다음