gpt4 book ai didi

arduino - 使用 arduino 模拟输入

转载 作者:行者123 更新时间:2023-12-02 02:01:13 24 4
gpt4 key购买 nike

我正在 UNO r3 上创建我的第一个 Arduino 程序。我之前玩过 Arduino Uno,只是使用一些小的示例程序等。我使用两个模拟输入来感测距离,使用 2 个具有 0-5vdc 缩放比例的激光传感器。这两个输入是 0-5vdc,我已经确保了整个公共(public)接地。两个传感器分别命名为left和right,分别输入到A0和A1。我还有一个差分 POT,它使用 10K 欧姆 POT 抽头电压作为 A2 上的输入。该程序的原理是取左右激光器之间输入电压差的绝对值,然后确定结果是否大于或等于 POT 雨刮器引脚 A2 上的电压。根据得出的数学结果,打开或关闭通过晶体管驱动电路插入到引脚 D13 的继电器。

问题:我无法在引脚 A0、A1 或 A2 上实现精确的电压变化(0-1023)。我已经使用串行监视器来诊断这个问题。不确定问题是什么,任何帮助都会很棒。此外,我无法在上述任何模拟引脚上实现 0 值,即使是 POT 雨刮器也不行!!!

这是我的代码:

    const int lf_dist = A0;          //names A0
const int rt_dist = A1; //names A1
const int differential = A2; //names A2
const int relay = 13; // select the pin for the relay coil
unsigned int left = 0; // variable to store the value coming from the left sensor
unsigned int right = 0; // variable to store the value coming from the right sensor
unsigned int diff = 0; // variable to store the value coming from the differential POT for maximum distance differential
unsigned int offset = 0; // variable that stores the value between the two laser sensors

void setup() {
Serial.begin(9600);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(relay, OUTPUT); // declare the relay pin as an OUTPUT:
analogReference(DEFAULT);
}

void loop()
{

unsigned int left = 0; // variable to store the value coming from the left sensor
unsigned int right = 0; // variable to store the value coming from the right sensor
unsigned int diff = 0; // variable to store the value coming from the differential POT for maximum distance differential
unsigned int offset = 0; // variable that stores the value between the two laser sensors



left = analogRead(A0); // read the value from the left laser
delay(5);
right = analogRead(A1); // read the value from the right sensor
delay(5);
diff = analogRead(A2); // read the value from the differential POT
delay(5);

offset = abs(left - right);

if(offset >= diff) // does math to check if left and right distances are greater than the value clocked in by the differential POT
{
digitalWrite(relay, LOW); // turns off the relay, opens the stop circuit, and turns on the yellow light
}
else
{
digitalWrite(relay, HIGH); // turns on the relay if all is good, and that keeps the machine running
}
Serial.print("\n left = " );
Serial.print(left);
Serial.print("\n right = " );
Serial.print(right);
Serial.print("\n differential = " );
Serial.print(diff);
delay(1000);
}

最佳答案

事实上,这实际上应该是由于测量针周围的 float 针具有不稳定的值,因此扰乱了您的测量。您应该使用 arduinoscope 查看您的值,这将向您显示其他 float 引脚对您的测量引脚的干扰影响。

解决此问题的简单方法是将所有未使用的模拟输入引脚接地,并在两个输入之间留出尽可能多的空间,这样它们就不会相互干扰。

关于arduino - 使用 arduino 模拟输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17151622/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com