我正在 UNO r3 上创建我的第一个 Arduino 程序。我之前用过 Arduino Uno 玩过一些小示例程序等。我使用两个模拟输入来感应距离,使用 2 个具有 0-5vdc 缩放比例的激光传感器。这两个输入为 0-5vdc,我已确保自始至终共同接地。两个传感器分别命名为left和right,分别输入到A0和A1。我还有一个差分电位器,它使用 10K 欧姆电位器抽头电压作为 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);
}