Day2 03_sensor_led

2024. 1. 18. 19:51arduino

  • 센서의 값을 받아와서 범위에 따라 led 점등
const int analogPin = A0;
const int ledPin[6] = {3,5,6,9,10,11};


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
  int sensorInput = analogRead(analogPin);
  // Serial.println(sensorInput);
  int level = map(sensorInput, 0, 1023, 0, 6);

  for(int i=0; i<6; i++) {
    if (i < level) {
      analogWrite(ledPin[i], 255);
    } else {
      analogWrite(ledPin[i], 0);
    }
  }
}

 

  • 밝기
const int analogPin = A0;
const int ledPin[6] = { 3, 5, 6, 9, 10, 11 };


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  for(int i=0;i<6;i++){
    pinMode(ledPin[i], OUTPUT);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  int sensorInput = analogRead(analogPin);
  // Serial.println(sensorInput);
  int bright = map(sensorInput, 0, 1023, 0, 255);

  for (int i = 0; i < 6; i++) {
    analogWrite(ledPin[i], bright);
  }
}

 

 

'arduino' 카테고리의 다른 글

Day2 05_IR  (0) 2024.01.18
Day2 04_sonic  (0) 2024.01.18
Day2 02_button  (0) 2024.01.18
Day2 01_python_rgb  (0) 2024.01.18
Day1 08_rgb_SerialInput  (0) 2024.01.17