본문 바로가기
2022/ARDUINO

가변저항 (+RGB)

by Or0i쿠 2022. 10. 20.

가변저항의 입력값을 시리얼 통신으로 전달하기

void setup()
{
	Serial.begin(9600);
}

void loop()
{
	int value = analogRead(A0);
    Serial.print("input value : "):
    Serial.print(value);
    
    delay(15);
}

 

가변저항을 이용해 LED 밝기 제어 하기

void setup()
{
	Serial.begin(9600);
}
void loop()
{
	int value = analogRead(A0);
    int brighht = map(value, 0, 1023, 255);
    Serial.print("input value : ");
    Serial.print(value);
    
    Serial.print("comput value:");
    Serial.print(bright);
    analogWrite(11, bright);
    delay(15);
}

 

가변저항으로 서브 모터 제어

#include<Servo.h>

servo s;

void setup()
{
	Serial.begin(9600);
    s.attach(9);
}

void loop()
{
	int value = analog Read(A0);
    int angle = map(value, o ,1023, 0 ,180);
    Serial.print("input value :")
    Serial.print(value);
    Serial.print("input angle : ");
    s.write(angle);
    delay(15);
}

랜덤함수을 이용한 RGB LED 제어(하드웨어)

int red = 11;
int green = 10;
int blue = 9;
void setup()
{
randomSeed(analogRead(0));
}
void loop() {
int r= random(256); 
int g= random(256);
int b = random(256);

analogWrite(red, r); 
analogWrite(green, g);
analogWrite(blue, b);

delay(10);
}

RGB LED 무지개색 변환

int red = 11;
int green = 10;
int blue = 9;

int rainbow[7][3] = {{255, 0, 0}, {255, 50, 0}, {255, 255, 0}, {0, 255, 0}, {0, 0, 255}, {0, 5, 255}, {100, 0, 255}};

void setup() {
}
void RGB(int r, int g, int b);
void loop() {
  for (int i = 0; i < 7; i++) {
    RGB(rainbow[i][0], rainbow[i][1], rainbow[i][2]);
    delay(500);
  }
}
void RGB(int r, int g, int b) {
  analogWrite(red, r);
  analogWrite(green, g);
  analogWrite(blue, b);
}