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

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

글 보관함

카테고리


반응형




The 74HC595; 74HCT595 is an 8-bit serial-in/serial or parallel-out shift register with a storage register and 3-state outputs. Both th e shift and storage register have separate clocks. The device features a serial input (DS) and a serial output (Q7S) to enable cascading and an asynchronous reset MR input. A LOW on MR will reset the shift register.
Data is shifted on the LOW-to-HIGH transitions of the SHCP input. The data in the shift register is transferred to the storage register on a LOW-to-HIGH transition of the STCP input. If both clocks are connected together, the shift register will always be one clock pulse ahead of the storage register. Data in the storage register appears at the output whenever the output enable input (OE) is LOW. A HIGH on OE causes the outputs to assume a high-impedance OFF-state. Operation of the OE input does not affect the state of the registers. Inputs include clamp diodes. This enables the use of current limiting resistors to interface inputs to voltages in excess of VCC




Display 0~9 and A~E on a 7-segment


Circuit




Source Code 1


const int clockPin = 8; //SH
const int latchPin = 9; //ST
const int dataPin = 10; // DS

const byte seg[10] = {0b00000000, 0b00000001,0b00000010,0b00000100,
                    0b00001000,0b00010000,0b00100000,0b01000000,
                    0b10000000,0b1111111};
const byte digits[17] = {
  0b01110111, //0
  0b00000110, //1
  0b10110011, //2
  0b10010111, //3
  0b11000110, //4
  0b11010101, //5
  0b11110101, //6
  0b01000111, //7
  0b11110111, //8
  0b11010111, //9
  0b11100111, //A
  0b11110100, //B
  0b01110001, //C
  0b10110110, //D
  0b11110001, //E
  0b11110001, //F
  0b11111111  //ALL
};


void setup() {
  // put your setup code here, to run once:
  pinMode(clockPin, OUTPUT);
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

}

void LEDs(byte a) {
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, a);
  digitalWrite(latchPin, HIGH);
}

void loop() {
  // put your main code here, to run repeatedly:
  for(int i=0; i<10; i++) {
    LEDs(seg[i]);
    delay(500);
  }

  for(int i=0; i<18; i++) {
    LEDs(digits[i]);
    delay(500);
  }
}



Source Code 2


//www.elegoo.com
//2016.06.13

// Lab13 Using a 74hc595 and seven segment display to make reciprocal function
 
// define the LED digit patterns, from 0 - 9
// 1 = LED on, 0 = LED off, in this order:
//                74HC595 pin     Q0,Q1,Q2,Q3,Q4,Q5,Q6,Q7
//                Mapping to      a,b,c,d,e,f,g of Seven-Segment LED
byte seven_seg_digits[10] = { B11111100,  // = 0
                              B01100000,  // = 1
                              B11011010,  // = 2
                              B11110010,  // = 3
                              B01100110,  // = 4
                              B10110110,  // = 5
                              B10111110,  // = 6
                              B11100000,  // = 7
                              B11111110,  // = 8
                              B11100110   // = 9
                             };
 
// connect to the ST_CP of 74HC595 (pin 9,latch pin)
int latchPin = 9;
// connect to the SH_CP of 74HC595 (pin 8, clock pin)
int clockPin = 8;
// connect to the DS of 74HC595 (pin 10)
int dataPin = 10;
 
void setup() {
  // Set latchPin, clockPin, dataPin as output
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}
 
// display a number on the digital segment display
void sevenSegWrite(byte digit) {
  // set the latchPin to low potential, before sending data
  digitalWrite(latchPin, LOW);
    
  // the original data (bit pattern)
  shiftOut(dataPin, clockPin, LSBFIRST, seven_seg_digits[digit]); 
 
  // set the latchPin to high potential, after sending data
  digitalWrite(latchPin, HIGH);
}
 
void loop() {      
  // count from 9 to 0
  for (byte digit = 10; digit > 0; --digit) {
    delay(1000);
    sevenSegWrite(digit - 1);
  }
  
  // suspend 4 seconds
  delay(4000);
}





74HC595 with 8 LEDs


Circuit



in my case, I used Arduino Uno and changed the pin numbers.


//by Simon Monk
//www.elegoo.com
//2016.06.13

int latchPin = 9;
int clockPin = 8;
int dataPin = 10;

byte leds = 0;

void setup()
{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
}

void loop()
{
  leds = 0;
  updateShiftRegister();
  delay(500);
  for (int i = 0; i < 8; i++)
  {
    bitSet(leds, i);
    updateShiftRegister();
    delay(500);
  }
}

void updateShiftRegister()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, LSBFIRST, leds);
   digitalWrite(latchPin, HIGH);
}



Use Serial Monitor


Same Circuit as above


Source Code


//by Simon Monk
//www.elegoo.com
//2016.06.13

int latchPin = 9;
int clockPin = 8;
int dataPin = 10;

byte leds = 0;

void setup()
{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT); 
  pinMode(clockPin, OUTPUT);
  updateShiftRegister();
  Serial.begin(9600);
  while (! Serial); // Wait untilSerial is ready - Leonardo
  Serial.println("Enter LED Number 0 to 7 or 'x' to clear");
}

void loop()
{
  if (Serial.available())
  {
    char ch = Serial.read();
    if (ch >= '0' && ch <= '7')
    {
      int led = ch - '0';
      bitSet(leds, led);
      updateShiftRegister();
      Serial.print("Turned on LED ");
      Serial.println(led);
    }
    if (ch == 'x')
    {
      leds = 0;
      updateShiftRegister();
      Serial.println("Cleared");
    }
  }
}

void updateShiftRegister()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, LSBFIRST, leds);
   digitalWrite(latchPin, HIGH);
}


==> Type number on the Serial Monitor -> light on the LED of typed number





반응형

[Arduino] Library and RGB LED

2017. 1. 11. 08:18 | Posted by 솔웅


반응형

Libraries


Arduino 소프트웨어에 익숙해지고 내장 함수를 사용할 수 있게 되면 Arduino 기능을 추가 Library 확장  있습니다.

 


Library?

 

Library 센서디스플레이모듈 등에 쉽게 연결할 수있는 코드 모음입니다예를 들어내장  LiquidCrystal Library 사용하면 문자 LCD 디스플레이와 쉽게 대화   있습니다인터넷에 다운로드  수있는 수백 개의 추가 Library 있습니다내장 Library 이러한 추가 Library  일부는 이곳에 가면 보실 수 있습니다. 추가 Library 사용하려면 Library 설치해야합니다.

 


Library 설치 방법

 


Library 관리자 사용


 

Arduino IDE  Library 설치하려면 Library Manager(IDE 버전 1.6.2에서 사용 가능) 사용하면 됩니다.  IDE 열고 "스케치"메뉴를 클릭  다음 Library 포함> Library Manage 클릭하십시오.

 




그런 다음 Library 관리자가 열리고 이미 설치되었거나 설치할 준비가  Library 목록이 표시됩니다. 예제에서는 Bridge Library 설치합니다목록을 스크롤하여 설치하려는 Library 버전을 선택하십시오경우에 따라 Library  버전  사용할  있습니다버전 선택 메뉴가 나타나지 않아도 걱정하지 마세요정상입니다.



 


마지막으로 install 클릭하고 IDE에서  Library 설치할 때까지 기다립니다연결 속도에 따라 다운로드하는  시간이 걸릴  있습니다설치가 끝나면 Installed 태그가 Bridge Library 옆에 나타납니다.  그러면 Library 매니저를 닫을  있습니다.

 


 


이제  Include Library menu에서  Library 찾을  있습니다나만의 Library 추가하려면 github에서 new issue 엽니다.

 

 

.zip Library 가져 오기

 

Library 종종 ZIP 파일 또는 폴더로 배포됩니다폴더의 이름은 Library 이름입니다폴더 안에는.cpp 파일, .h 파일  종종 keywords.txt 파일예제 폴더  Library 필요한 기타 파일이 있습니다.버전 1.0.5부터는 3rd party Library IDE 설치할  있습니다다운로드  Library 압축을 풀지말고 그대로 두십시오.

 

Arduino IDE에서 스케치> Library 포함으로 이동하십시오드롭 다운 목록의 상단에서 ".ZIP Library추가"옵션을 선택하십시오.

 


 


추가하려는 Library 선택하라는 메시지가 나타납니다. .zip 파일의 위치로 이동하여 엽니 .

 


 


스케치> Library 가져 오기 메뉴로 돌아갑니다이제 드롭 다운 메뉴 하단에 Library 표시됩니다.스케치에서 사용할 준비가되었습니다. zip 파일은 Arduino 스케치 디렉토리의 libraries 폴더에서 확장됩니다.

 

참고 : Library 스케치에서 사용할  있지만 IDE restart  때까지 Library 예제가 파일예제에 표시되지 않습니다.

 


Manual Installation

 

Library 설치하려면 먼저 Arduino 응용 프로그램을 종료하십시오그런 다음 Library 포함  ZIP파일의 압축을 푸십시오예를 들어 "ArduinoParty"라는 Library 설치하는 경우 ArduinoParty.zip압축 해제하십시오. ArduinoParty라는 폴더와 ArduinoParty.cpp  ArduinoParty.h 같은 파일이 있어야합니다. (.cpp  .h 파일이 폴더에 없으면 폴더를 만들어야합니다. 경우 "ArduinoParty"라는폴더를 만들어 ZIP 있던 모든 파일로 이동하십시오 ArduinoParty.cpp  ArduinoParty.h 같은 파일).

 

ArduinoParty 폴더를이 폴더 (Library 폴더) 드래그하십시오. Windows에서는 "My Documents \ Arduino \ libraries"라고합니다. Mac 사용자의 경우 "Documents / Arduino / libraries"라고 불릴 것입니다리눅스에서는 스케치북의 "libraries"폴더가됩니다.

 

Arduino Library 폴더는 다음과 같아야합니다 (Windows 경우).

 

  My Documents\Arduino\libraries\ArduinoParty\ArduinoParty.cpp
  My Documents\Arduino\libraries\ArduinoParty\ArduinoParty.h
  My Documents\Arduino\libraries\ArduinoParty\examples
  ....

 

또는 다음과 같이 (Mac  Linux) :

 

  Documents/Arduino/libraries/ArduinoParty/ArduinoParty.cpp
  Documents/Arduino/libraries/ArduinoParty/ArduinoParty.h
  Documents/Arduino/libraries/ArduinoParty/examples
  ....

 

 

 

.cpp  .h 파일보다 많은 파일이있을  있습니다파일이 모두 있는지 확인하십시오. (.cpp  .h 파일을 Library 폴더에 직접 저장하거나 추가 폴더에 중첩  경우 Library 작동하지 않습니다 ( : Documents \ Arduino \ libraries \ ArduinoParty.cpp  Documents \ Arduino \ libraries \ ArduinoParty \ ArduinoParty \ ArduinoParty.cpp 작동하지 않습니다.)

 

Arduino 응용 프로그램을 다시 시작하십시오 Library 소프트웨어의 스케치 -> Library 가져 오기 메뉴 항목에 나타나는지 확인하십시오그게 다야! Library 설치했습니다!

 

 튜토리얼은 Limor Fried 텍스트를 기반으로합니다.

 

Arduino 시작 안내서의 텍스트는 Creative Commons Attribution-ShareAlike 3.0 라이선스 따라 사용이 허가되었습니다가이드의 코드 샘플은 공개 도메인으로 배포됩니다.

 

https://www.arduino.cc/en/reference/libraries



※ RGB LED with Arduino Mega 2560



Circuit




Source Code


//by Simon Monk
//www.elegoo.com
//2016.06.13

// Define Pins
#define RED 3
#define GREEN 5
#define BLUE 6

#define delayTime 10 // fading time between colors


void setup()
{
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
digitalWrite(RED, HIGH);
digitalWrite(GREEN, HIGH);
digitalWrite(BLUE, HIGH);
}

// define variables
int redValue;
int greenValue;
int blueValue;


// main loop
void loop()
{
  redValue = 255; // choose a value between 1 and 255 to change the color.
  greenValue = 0;
  blueValue = 0;

  analogWrite(RED, 0);
  delay(1000);

   for(int i = 0; i < 255; i += 1) // fades out red bring green full when i=255
  {
    redValue -= 1;
    greenValue += 1;
    analogWrite(RED, 255 - redValue);
    analogWrite(GREEN, 255 - greenValue);
    delay(delayTime);
  }

  redValue = 0;
  greenValue = 255;
  blueValue = 0;

  for(int i = 0; i < 255; i += 1)  // fades out green bring blue full when i=255
  {
    greenValue -= 1;
    blueValue += 1;
    analogWrite(GREEN, 255 - greenValue);
    analogWrite(BLUE, 255 - blueValue);
    delay(delayTime);
  }

  redValue = 0;
  greenValue = 0;
  blueValue = 255;


  for(int i = 0; i < 255; i += 1)  // fades out blue bring red full when i=255
  {
  redValue += 1;
  blueValue -= 1;
  analogWrite(RED, 255 - redValue);
  analogWrite(BLUE, 255 - blueValue);
  delay(delayTime);
  }
}




반응형

[Arduino] Servo Motor

2017. 1. 10. 09:53 | Posted by 솔웅


반응형


※ Servo Motor


Circuit




Source Code


Include Servo Library



#include <Servo.h>

Servo myservo;
int pos = 0;

void setup() {
  // put your setup code here, to run once:
  myservo.attach(9);
}

void loop() {
  // put your main code here, to run repeatedly:
  for(pos = 0; pos < 180; pos += 1)
  {
    myservo.write(pos);
    delay(15);
  }
}



※ Servo Motor with variable resistor


Circuit


Source Code


#include <Servo.h>

Servo myservo;

void setup() {
  // put your setup code here, to run once:
  myservo.attach(9);
}

void loop() {
  // put your main code here, to run repeatedly:
  myservo.write(map(analogRead(A0),0,1023,0,120));
  delay(15);
}


refer to youtube tutorial (in Korean) : https://www.youtube.com/watch?v=s_B8KYE7xvI&index=10&list=PL0Vl139pNHbe-JlsydLg3NFRk6nC_cC7w



반응형

[Arduino] LCD and 7 Segment

2017. 1. 10. 09:27 | Posted by 솔웅


반응형


※  Control LCD with illuminance sencor



Circuit




Source Code


LiquidCrystal Library. This library allows an Arduino board to control LiquidCrystal displays (LCDs) based on the Hitachi HD44780 (or a compatible) chipset, which is found on most text-based LCDs.


include LiquidCrystal Library.




#include <LiquidCrystal.h>

LiquidCrystal lcd(12,11,2,3,4,5);

void setup() {
  // put your setup code here, to run once:
  lcd.begin(16,2);
  lcd.print("More Fun Life");
  Serial.print("Test");
}

void loop() {
  // put your main code here, to run repeatedly:
  lcd.setCursor(0,1);
  lcd.print(analogRead(A0));
  delay(200);
}


==> Display Illuminance sensor value on LCD



※ Control LCD using ultrasonic sencor


Circuit




Source Code


#include <LiquidCrystal.h>
#define TRIG 8
#define ECHO 9

LiquidCrystal lcd(12,11,2,3,4,5);

void setup() {
  // put your setup code here, to run once:
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  lcd.begin(16,2);
}

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;
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(distance);
  lcd.print(" cm");
  delay(200);
}


refer to youtube tutorial (in Korean) : https://www.youtube.com/watch?v=coFwvg_O1q4&index=8&list=PL0Vl139pNHbe-JlsydLg3NFRk6nC_cC7w




7 Segment Anode


7 Segment Cathode




※ 7 Segment



Circuit



Source Code


// Common Anode version
byte digits[10][7] =
{
  {0,0,0,0,0,0,1 }, // display '0'
  {1,0,0,1,1,1,1 }, // display '1'
  {0,0,1,0,0,1,0 }, // display '2'
  {0,0,0,0,1,1,0 }, // display '3'
  {1,0,0,1,1,0,0 }, // display '4'
  {0,1,0,0,1,0,0 }, // display '5'
  {0,1,0,0,0,0,0 }, // display '6'
  {0,0,0,1,1,1,1 }, // display '7'
  {0,0,0,0,0,0,0 }, // display '8'
  {0,0,0,1,1,0,0 }  // display '9'
};

// Common Cathode version
/*
byte digits[10][7] = {
  {1,1,1,1,1,1,0 }, // display '0'
  {0,1,1,0,0,0,0 }, // display '1'
  {1,1,0,1,1,0,1 }, // display '2'
  {1,1,1,1,0,0,1 }, // display '3'
  {0,1,1,0,0,1,1 }, // display '4'
  {1,0,1,1,0,1,1 }, // display '5'
  {1,0,1,1,1,1,1 }, // display '6'
  {1,1,1,0,0,0,0 }, // display '7'
  {1,1,1,1,1,1,1 }, // display '8'
  {1,1,1,0,0,1,1 }  // display '9'
};
*/
void setup() {
  // put your setup code here, to run once:
  for(int i=2;i<10;i++) {
    pinMode(i,OUTPUT);
  }
  digitalWrite(9,HIGH); // turn off DP LED
}

void loop() {
  // put your main code here, to run repeatedly:
  for(int i=0; i<10;i++) {
    delay(1000);
    displayDigit(i);
  }
}

void displayDigit(int num) {
  int pin = 2;
  for(int i=0; i<7; i++) {
    digitalWrite(pin+i, digits[num][i]);
  }
}



※ 7 Segment with buttons


Circuit


Source Code


#define PLUS 11
#define MINUS 12

int digit = 0;

// Common Anode version
byte digits[10][7] =
{
  {0,0,0,0,0,0,1 }, // display '0'
  {1,0,0,1,1,1,1 }, // display '1'
  {0,0,1,0,0,1,0 }, // display '2'
  {0,0,0,0,1,1,0 }, // display '3'
  {1,0,0,1,1,0,0 }, // display '4'
  {0,1,0,0,1,0,0 }, // display '5'
  {0,1,0,0,0,0,0 }, // display '6'
  {0,0,0,1,1,1,1 }, // display '7'
  {0,0,0,0,0,0,0 }, // display '8'
  {0,0,0,1,1,0,0 }  // display '9'
};

void setup() {
  // put your setup code here, to run once:
  pinMode(PLUS,INPUT);
  pinMode(MINUS, INPUT);

  for(int i=2;i<10;i++) {
    pinMode(i,OUTPUT);
  }
  digitalWrite(9,HIGH); // turn off DP LED
}

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

  if(digitalRead(NINUS) == HIGH)
  {
    --digit;
    if(digit < 0)
    {
      digit=9;
    }
  } 

  displayDigit(digit);
  delay(100);
}

void displayDigit(int num) {
  int pin = 2;
  for(int i=0; i<7; i++) {
    digitalWrite(pin+i, digits[num][i]);
  }
}


refer to youtube tutorial (in Korean) : https://www.youtube.com/watch?v=SRtYpBnmPfA&index=9&list=PL0Vl139pNHbe-JlsydLg3NFRk6nC_cC7w

반응형


반응형


더 재밌게 놀기 연구소



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

반응형


반응형


Arduino Basic


Open Source Hardware

- Arduino UNO : 8 bit CPU

- Arduino DUE

- Arduino TRE : Arduino + Beaglebone black

- Arduino Yun :(Built-in Wifi

- Arduino Mini Nano

- Netduino

- Raspberry Pi : 32 bit, Linux, 700 MB

- Beaglebone black : 32 bit, 1 GB, 512 MB Memory, Android O

- Intel Galileo : Pentium CPU,  can share Arduino shield

 

IDE

- Arduino Sketch : Language - C/C++

- Arduino Scratch : GNU Development Tool

- Arduino Processing : for Art

 

Arduino Shields


Sensor & Actuator

 

Breadboard

 

 



◎ Control Arduino Built-in LED


Source Code (Sketch Sample : Blink)


`/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino model, check
  the Technical Specs of your board  at https://www.arduino.cc/en/Main/Products
 
  This example code is in the public domain.

  modified 8 May 2014
  by Scott Fitzgerald
 
  modified 2 Sep 2016
  by Arturo Guadalupi
 
  modified 8 Sep 2016
  by Colby Newman
*/


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}


==> The built-in LED will blink every second.




◎ Control External LED


circuit diagram




Source Code : Same Source Code as above one


==> The External LED will blink every second.


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




◎ Control multiple external LEDs


LED : Light-emitting diode 


circuit diagram




Source Code


#define DELAY_TIME 100

void setup() {
  // put your setup code here, to run once:
  pinMode(2. OUTPUT);
  pinMode(3. OUTPUT);
  pinMode(4. OUTPUT);
  pinMode(5. OUTPUT);
  pinMode(6. OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(2, HIGH);
  delay(DELAY_TIME);
  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
  delay(DELAY_TIME);
  digitalWrite(3, LOW);
  digitalWrite(4, HIGH);
  delay(DELAY_TIME);
  digitalWrite(4, LOW);
  digitalWrite(5, HIGH);
  delay(DELAY_TIME);
  digitalWrite(5, LOW);
  digitalWrite(6, HIGH);
  delay(DELAY_TIME);
  digitalWrite(6, LOW);
  digitalWrite(5, HIGH);
  delay(DELAY_TIME);
  digitalWrite(5, LOW);
  digitalWrite(4, HIGH);
  delay(DELAY_TIME);
  digitalWrite(4, LOW);
  digitalWrite(3, HIGH);
  delay(DELAY_TIME);
  digitalWrite(3, LOW);
 
}


==> The LEDs will turn on and off sequentially.



Refactoring above code using FOR statement


#define DELAY_TIME 100

void setup() {
  // put your setup code here, to run once:
  for(int thisPin = 2; thisPin < 7; thisPin++) {
    pinMode(thisPin,OUTPUT);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  // loop from the lowest pin to the highest:
  for (int thisPin = 2; thisPin < 7; thisPin++) {
    // turn the pin on:
    digitalWrite(thisPin, HIGH);
    delay(DELAY_TIME);
    // turn the pin off:
    digitalWrite(thisPin, LOW);
  }

  // loop from the highest pin to the lowest:
  for (int thisPin = 6; thisPin >= 2; thisPin--) {
    // turn the pin on:
    digitalWrite(thisPin, HIGH);
    delay(DELAY_TIME);
    // turn the pin off:
    digitalWrite(thisPin, LOW);
  }
 
}



◎ Button Control


circuit diagram



Source Code


#define LED 12
#define BUTTON 7

void setup() {
  // put your setup code here, to run once:
  pinMode(LED, OUTPUT);
  pinMode(BUTTON, INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  if(digitalRead(BUTTON) == HIGH) {
    digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED,LOW);
  }
}


==> If press the button then turn on the LED for 0.5 second.


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





Application

I am going to combine both Button control and Multiple LEDs control source codes.



Source Code



#define BUTTON 8
#define DELAY_TIME 100

void setup() {
  // put your setup code here, to run once:
  for(int thisPin = 2; thisPin < 7; thisPin++) {
    pinMode(thisPin,OUTPUT);
  }
  pinMode(BUTTON, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(digitalRead(BUTTON) == HIGH) {
    // loop from the lowest pin to the highest:
     for (int thisPin = 2; thisPin < 7; thisPin++) {
      // turn the pin on:
       digitalWrite(thisPin, HIGH);
      delay(DELAY_TIME);
       // turn the pin off:
      digitalWrite(thisPin, LOW);
     }

    // loop from the highest pin to the lowest:
    for (int thisPin = 6; thisPin >= 2; thisPin--) {
      // turn the pin on:
      digitalWrite(thisPin, HIGH);
      delay(DELAY_TIME);
      // turn the pin off:
      digitalWrite(thisPin, LOW);
    }
  }
}


==> The LEDs will turn on and off sequentially when press button





반응형
이전 1 2 다음