标题: arduino AnalogInOutPWM 进步电机驱动程序 [打印本页]

作者: 大伟521    时间: 2018-12-3 20:49
标题: arduino AnalogInOutPWM 进步电机驱动程序
进步电机
  1. /*

  2. 通过检测电位器的模拟电压值,通过串口反映出对应的数字量,然后把
  3. 数据变化到0-255区间,使用PWM引脚调节led亮度或者电机速度


  4. */

  5. // 引脚定义
  6. const int analogInPin = A0;  // 模拟输入引脚
  7. const int analogOutPin = 9; //  PWM输出引脚

  8. int sensorValue = 0;        // 电位器电压值
  9. int outputValue = 0;        // 模拟量输出值(PWM)

  10. void setup() {
  11.   // 初始化串口参数
  12.   Serial.begin(9600);
  13. }

  14. void loop() {
  15.   // 读取模拟量值
  16.   sensorValue = analogRead(analogInPin);            
  17.   // 变换数据区间
  18.   outputValue = map(sensorValue, 0, 1023, 0, 255);  
  19.   // 输出对应的PWM值
  20.   analogWrite(analogOutPin, outputValue);           

  21.   // 打印结果到串口监视器
  22.   //这里可以使用arduino自带的串口调试器,也可以使用德飞莱串口调试软件
  23.   Serial.print("sensor = " );                       
  24.   Serial.print(sensorValue);      
  25.   Serial.print("\t output = ");      
  26.   Serial.println(outputValue);   

  27.   // 等待2ms进行下一个循环
  28.   // 取保能稳定读取下一次数值
  29.   delay(2);                     
  30. }
复制代码

AnalogInOutPWM.zip

1.17 KB, 下载次数: 4, 下载积分: 黑币 -5






欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1