硬件平台:PC机一台,ibox卡片电脑一只,arduino扩展板一个,电位器一个,LED灯一个 软件平台:WIN7操作系统,android4.0或android4.4系统 实验目标:通过ADC从电位器读取ADC的值,然后通过得到的值控制LED灯的亮度。 打开arduino的IDE开发工具,依次点击文件->示例-> 03.Analog->AnalogWriteMega,AnalogWriteMega的示例程序将会被打开,其源码如下: - const int sensorPin = A0; // pin that the sensor is attached to
- const int ledPin = PWM0; // pin that the LED is attached to
-
- // variables:
- int sensorValue = 0; // the sensor value
- int sensorMin = 1023; // minimum sensor value
- int sensorMax = 0; // maximum sensor value
-
-
- void setup() {
- // turn on LED to signal the start of the calibration period:
- pinMode(LED1, OUTPUT);
- digitalWrite(LED1, HIGH);
-
- // calibrate during the first five seconds
- while (millis() < 5000) {
- sensorValue = analogRead(sensorPin);
-
- // record the maximum sensor value
- if (sensorValue > sensorMax) {
- sensorMax = sensorValue;
- }
-
- // record the minimum sensor value
- if (sensorValue < sensorMin) {
- sensorMin = sensorValue;
- }
- }
-
- // signal the end of the calibration period
- digitalWrite(LED1, LOW);
- }
-
- void loop() {
- // read the sensor:
- sensorValue = analogRead(sensorPin);
-
- // apply the calibration to the sensor reading
- sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255);
-
- // in case the sensor value is outside the range seen during calibration
- sensorValue = constrain(sensorValue, 0, 255);
-
- // fade the LED using the calibrated value:
- analogWrite(ledPin, sensorValue);
- }
复制代码 将电位器的两端接GND和5V,中间的脚接A0,PWM0接LED,运行程序,通过反复修改程序中的参数,观察LED灯的亮暗程度。
|