硬件平台:PC机一台,ibox卡片电脑一只,arduino扩展板一个,电位器外设一个,LED灯外设一个 软件平台:WIN7操作系统,android4.0或android4.4系统 实验目标:通过电位器旋转改变模拟电压,程序通过模拟电压的变化进而控制LED灯的亮度,并通过调试窗口将模拟电压转换的数字信号和对应的LED亮度值显示出来。 打开arduino的IDE开发工具,依次点击文件->示例-> 03.Analog-> AnalogInOutSerial,AnalogInOutSerial的示例程序将会被打开,其源码如下: - // These constants won't change. They're used to give names
- // to the pins used:
- const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
- const int analogOutPin = PWM0; // Analog output pin that the LED is attached to
-
- int sensorValue = 0; // value read from the pot
- int outputValue = 0; // value output to the PWM (analog out)
-
- void setup() {
- // initialize serial communications at 9600 bps:
- Serial.begin(9600);
- }
-
- void loop() {
- // read the analog in value:
- sensorValue = analogRead(analogInPin);
- // map it to the range of the analog out:
- outputValue = map(sensorValue, 0, 1023, 0, 255);
- // change the analog out value:
- analogWrite(analogOutPin, outputValue);
-
- // print the results to the serial monitor:
- Serial.print("sensor = " );
- Serial.print(sensorValue);
- Serial.print("\t output = ");
- Serial.println(outputValue);
-
- // wait 2 milliseconds before the next loop
- // for the analog-to-digital converter to settle
- // after the last reading:
- delay(2);
- }
复制代码 在setup函数中,设置串口波特率为9600,在loop函数中通过analogRead函数读取模拟电压的值,然后通过map函数映射出和读取出来的电压值一一对应的LED亮度值,并通过PWM0输出对应脉宽的波形,从而控制LED灯的亮度。最终通过Serial.print函数将相关的值打印出来。
|