본문 바로가기

카테고리 없음

[아두이노] 6주차 - 가변저항

#define VOL A0
#define RLED 2
#define GLED 3
#define YLED 4
#define BUZ 6
#define OFF 99
#include "pitches.h"

int s_1000[4] = {NOTE_C5, NOTE_E5, NOTE_G5,NOTE_C6};
int  s_500[4] = {NOTE_C6, NOTE_G5, NOTE_E5,NOTE_C5};
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode( VOL, INPUT);
  pinMode(RLED,OUTPUT);
  pinMode(YLED,OUTPUT);
  pinMode(GLED,OUTPUT);
  pinMode( BUZ,OUTPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  int value;
  value = analogRead(VOL);
  Serial.print("가변저항 = ");
  Serial.println(value);
  if(value>=900) chLED(YLED);
  else if(value>=700) chLED2(YLED);
  else if(value>=500) chLED2(GLED);
  else if(value>=300) chLED(RLED);
  else chLED(OFF);
  if(value==1000) sound_1000();
  else if(value==500) sound_500();
  
  delay(500);
}
void chLED(int num){
  for(int i=RLED;i<=YLED;i++){
    if(i==num) digitalWrite(i,HIGH);
    else digitalWrite(i,LOW);
  } random(1023);
}

void chLED2(int num){
  for(int i=RLED;i<=YLED;i++){
    if(i<=num) digitalWrite(i,HIGH);
    else digitalWrite(i,LOW);
  }
}

void sound_1000(){
  for(int i=0;i<4;i++){
    tone(BUZ,s_1000[i],1000/4);
    delay(1000/4*1.3);
  }
}

void sound_500(){
  for(int i=0;i<4;i++){
    tone(BUZ,s_500[i],1000/4);
    delay(1000/4*1.3);
  }
}

가변저항 ㅗ 모양일때 GND ANALOG 5V 순서

GND와 5V 를 반대로 연결하면 값이 반대로 바뀜

EX) 0 -> 1023

가변저항에는 A/D 컨버터가 있음