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

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함

카테고리

[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

반응형