Arduino实践:呼吸灯

Arduino例程,环境: Arduino学习平台搭建配置方式(一)

const int potPin = A0;
const int LEDpin = 10;
int potValue = 0;
int brightness = 0;
int fadeAmount = 5;
void setup() {
  // put your setup code here, to run once:
  pinMode(potPin, INPUT);
  pinMode(LEDpin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  potValue = analogRead(potPin);
  Serial.println(potValue);
  potValue = potValue / 10;
  //第一种呼吸灯
  //  for (int i = 0; i <= 200; i += fadeAmount) {
  //    analogWrite(LEDpin, i);
  //    delay(potValue);
  //  }
  //  for (int i = 200; i >= 0; i -= fadeAmount) {
  //    analogWrite(LEDpin, i);
  //    delay(potValue);
  //  }
  //第二种呼吸灯
  analogWrite(LEDpin, brightness);
  delay(potValue);
  brightness += fadeAmount;
  if (brightness >= 200 || brightness <= 0) {
    fadeAmount = -fadeAmount;
  }
}