Жанры

Страница 12

19 августа 2026, 16:17

void setup() {

  pinMode(LED, OUTPUT); // LED is as an OUTPUT

  // Note: Analogue pins are

  // automatically set as inputs

}

void loop() {

  val = analogRead(0); // read the value from

                       // the sensor

  digitalWrite(13, HIGH); // turn the LED on

  delay(val); // stop the program for

              // some time

  digitalWrite(13, LOW); // turn the LED off

  delay(val); // stop the program for

              // some time

}

Теперь попробуйте пример 5-4, но перед этим вам придётся изменить свою схему. Посмотрите на рис. 5-4 ещё раз и соедините светодиод с выводом 9. Так как у вас уже есть собранная схема, вам надо найти точку платы где светодиод, провод и резистор не будет соединён с фотодатчиком.

Пример 5-4. Установка яркости светодиода, определяемая данными на аналоговом входе.

#define LED 9 // the pin for the LED

int val = 0; // variable used to store the value

              // coming from the sensor

void setup() {

  pinMode(LED, OUTPUT); // LED is as an OUTPUT

  // Note: Analogue pins are

  // automatically set as inputs

}

void loop() {

  val = analogRead(0); // read the value from

                       // the sensor

  analogWrite(LED, val/4); // turn the LED on at

                           // the brightness set

                           // by the sensor

  delay(10); // stop the program for

             // some time

Пока нет комментариев. Авторизуйтесь, чтобы оставить свой отзыв первым!