标题:
arduino AnalogInOutPWM 进步电机驱动程序
[打印本页]
作者:
大伟521
时间:
2018-12-3 20:49
标题:
arduino AnalogInOutPWM 进步电机驱动程序
进步电机
/*
通过检测电位器的模拟电压值,通过串口反映出对应的数字量,然后把
数据变化到0-255区间,使用PWM引脚调节led亮度或者电机速度
*/
// 引脚定义
const int analogInPin = A0; // 模拟输入引脚
const int analogOutPin = 9; // PWM输出引脚
int sensorValue = 0; // 电位器电压值
int outputValue = 0; // 模拟量输出值(PWM)
void setup() {
// 初始化串口参数
Serial.begin(9600);
}
void loop() {
// 读取模拟量值
sensorValue = analogRead(analogInPin);
// 变换数据区间
outputValue = map(sensorValue, 0, 1023, 0, 255);
// 输出对应的PWM值
analogWrite(analogOutPin, outputValue);
// 打印结果到串口监视器
//这里可以使用arduino自带的串口调试器,也可以使用德飞莱串口调试软件
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
// 等待2ms进行下一个循环
// 取保能稳定读取下一次数值
delay(2);
}
复制代码
AnalogInOutPWM.zip
2018-12-3 20:49 上传
点击文件名下载附件
下载积分: 黑币 -5
1.17 KB, 下载次数: 4, 下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1