永发信息网

ARDUINO旋钮电位器读取错误问题

答案:2  悬赏:0  手机版
解决时间 2021-03-16 01:57
  • 提问者网友:骨子里的高雅
  • 2021-03-15 20:10
帮忙看下,旋钮电位器的电位数据读取没成功,求助
int dwq2 = A0;
int LED1 = 2;
int LED2 = 3;
int LED3 = 4;
int LED4 = 5;
void setup()
{
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
}
void loop()
{
int dwq2 = analogRead(A0);
if (80 > dwq2){digitalWrite(LED1,HIGH);digitalWrite(LED2,HIGH);digitalWrite(LED3,HIGH);digitalWrite(LED4,HIGH);}
if (160> dwq2 > 79){digitalWrite(LED1,LOW);digitalWrite(LED2,HIGH);digitalWrite(LED3,LOW);digitalWrite(LED4,LOW);}
if (224> dwq2 > 159){digitalWrite(LED1,HIGH);digitalWrite(LED2,HIGH);digitalWrite(LED3,LOW);digitalWrite(LED4,LOW);}
if (304> dwq2 > 223){digitalWrite(LED1,LOW);digitalWrite(LED2,LOW);digitalWrite(LED3,HIGH);digitalWrite(LED4,LOW);}
if (368> dwq2 > 303){digitalWrite(LED1,HIGH);digitalWrite(LED2,LOW);digitalWrite(LED3,HIGH);digitalWrite(LED4,LOW);}
if (432> dwq2 > 367){digitalWrite(LED1,LOW);digitalWrite(LED2,HIGH);digitalWrite(LED3,HIGH);digitalWrite(LED4,LOW);}
if (496> dwq2 > 431){digitalWrite(LED1,HIGH);digitalWrite(LED2,HIGH);digitalWrite(LED3,HIGH);digitalWrite(LED4,LOW);}
if (576> dwq2 > 495){digitalWrite(LED1,LOW);digitalWrite(LED2,LOW);digitalWrite(LED3,LOW);digitalWrite(LED4,HIGH);}
if (640> dwq2 > 575){digitalWrite(LED1,HIGH);digitalWrite(LED2,LOW);digitalWrite(LED3,LOW);digitalWrite(LED4,HIGH);}
if (704> dwq2 > 639){digitalWrite(LED1,LOW);digitalWrite(LED2,HIGH);digitalWrite(LED3,LOW);digitalWrite(LED4,HIGH);}
if (768> dwq2 > 703){digitalWrite(LED1,LOW);digitalWrite(LED2,LOW);digitalWrite(LED3,HIGH);digitalWrite(LED4,HIGH);}
if (832> dwq2 > 767){digitalWrite(LED1,HIGH);digitalWrite(LED2,HIGH);digitalWrite(LED3,LOW);digitalWrite(LED4,HIGH);}
if (896> dwq2 > 831){digitalWrite(LED1,HIGH);digitalWrite(LED2,LOW);digitalWrite(LED3,HIGH);digitalWrite(LED4,HIGH);}
if (960> dwq2 > 895){digitalWrite(LED1,LOW);digitalWrite(LED2,HIGH);digitalWrite(LED3,HIGH);digitalWrite(LED4,HIGH);}
if (1024> dwq2 > 959){digitalWrite(LED1,HIGH);digitalWrite(LED2,HIGH);digitalWrite(LED3,HIGH);digitalWrite(LED4,HIGH);}
}
最佳答案
  • 五星知识达人网友:西风乍起
  • 2021-03-15 21:19
你可以先打印旋钮的值
Serial.print(dwq2);
如果有输出,说明旋钮读取正常,如果没有,看看接线是否正确,旋钮坏了也有可能!
但是你的程序判断有个错误?
我记得我以前写代码时,这个不能这样用:160> dwq2 > 79,好像要分开来写。
现在我都习惯性的分开写,dwq2>79&&dwq2<160;
帮你写了个程序,实验了一下,果然,我没记错:
int d=1000;
void setup() {
// put your setup code here, to run once:
pinMode(13,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
//if (d > 959&&1024> d)
//digitalWrite(13,HIGH);
if (1024> d > 959)
digitalWrite(13,HIGH);
}
程序应该就是这个问题
全部回答
  • 1楼网友:七十二街
  • 2021-03-15 22:10
你要先看一下电位器输出的范围是什么 再看看别人怎么说的。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯