OpenCR入门教程-开发示例-旋转角度传感器
说明
- 本教程介绍OpenCR板上的旋转角度传感器测试示例
传感器参数
- 供电电压:3.3~5v
- 接口:Analog
- 转动角度:360°
代码
- 旋转传感器是一个模拟传感器,其输出值取决于旋转程度
- LED根据旋转程度打开/关闭
- 信号引脚连接到OpenCR的A0
#define BDPIN_LED_USER_1 22
#define BDPIN_LED_USER_2 23
#define BDPIN_LED_USER_3 24
#define BDPIN_LED_USER_4 25
const int analogInPin = A0;
int sensorValue = 0;
void setup()
{
Serial.begin(9600);
pinMode(BDPIN_LED_USER_4, OUTPUT);
pinMode(BDPIN_LED_USER_3, OUTPUT);
pinMode(BDPIN_LED_USER_2, OUTPUT);
pinMode(BDPIN_LED_USER_1, OUTPUT);
}
void loop()
{
sensorValue = analogRead(analogInPin);
Serial.print(" sensorValue : ");
Serial.println(sensorValue);
if(sensorValue>0 && sensorValue<50)
{
digitalWrite(BDPIN_LED_USER_4, LOW);
digitalWrite(BDPIN_LED_USER_3, HIGH);
digitalWrite(BDPIN_LED_USER_2, HIGH);
digitalWrite(BDPIN_LED_USER_1, HIGH);
}
if(sensorValue>50 && sensorValue<100)
{
digitalWrite(BDPIN_LED_USER_4, LOW);
digitalWrite(BDPIN_LED_USER_3, LOW);
digitalWrite(BDPIN_LED_USER_2, HIGH);
digitalWrite(BDPIN_LED_USER_1, HIGH);
}
if(sensorValue>100 && sensorValue<150)
{
digitalWrite(BDPIN_LED_USER_4, LOW);
digitalWrite(BDPIN_LED_USER_3, LOW);
digitalWrite(BDPIN_LED_USER_2, LOW);
digitalWrite(BDPIN_LED_USER_1, HIGH);
}
if(sensorValue>200 && sensorValue<250)
{
digitalWrite(BDPIN_LED_USER_4, LOW);
digitalWrite(BDPIN_LED_USER_3, LOW);
digitalWrite(BDPIN_LED_USER_2, LOW);
digitalWrite(BDPIN_LED_USER_1, LOW);
}
delay(100);
}
演示视频
获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号