0

好的,下一个问题是:如果我在数字输入上添加了一个按钮,我可以用它来将电位器校准为零吗?

因此,当我按下按钮时,无论罐子处于什么位置,所有值都从零开始?之后我打算在 Excel 中执行此操作,但是今天下午似乎可以尝试。你会使用 switch 语句还是某种 if 语句?

float ZPot = 0;
float YPot = 1;
float XPot = 2;
byte Reset = 10;

void setup()
{
    pinMode(XPot, INPUT);
    pinMode(YPot, INPUT);
    pinMode(ZPot, INPUT);
    pinMode(Reset, INPUT);

    Serial.begin(9600);
}

void loop()
{
    ZPot = analogRead(0)/ 1023.0 * 105.0;
    YPot = analogRead(1)/ 1023.0 * 105.0;
    XPot = analogRead(2)/ 1023.0 * 105.0;
    Reset = digitalRead(10);

    Serial.print("X Pot [mm] = ");
    Serial.print(XPot    );
    delay(500);

    Serial.print("   Y Pot [mm] = ");
    Serial.print(YPot    );
    delay(500);

    Serial.print("   Z Pot [mm] = ");
    Serial.println (ZPot  );
    delay(500);
}
4

3 回答 3

0

尝试添加功能

float convertToMM(float reading)
{
  return reading/1023.0*105.0;
}

然后当你做阅读时

ZPot = convertToMM(analogRead(0));
于 2011-04-13T13:09:58.567 回答
0

你已经快到了。你只需要改变两件事:

float ZPot = 0;           
float YPot = 1;          
float XPot = 2;
int Reset = 10;
float ZCalibration = 0;
float YCalibration = 0;
float XCalibration = 0;

Reset = digitalRead(10);
ZPot = (analogRead(0) / 1023.0 * 105.0) - ZCalibration;
YPot = (analogRead(1) / 1023.0 * 105.0) - YCalibration;
XPot = (analogRead(2) / 1023.0 * 105.0) - XCalibration;

if (Reset == HIGH) {
    ZCalibration = ZPot;
    YCalibration = YPot;
    XCalibration = XPot;
}
于 2011-04-13T13:11:24.123 回答
0

只是为了添加答案,您还可以使用map()函数:

ZPot = map(analogRead(0),0,1023.0,0,105);
YPot = map(analogRead(1),0,1023.0,0,105);
XPot = map(analogRead(2),0,1023.0,0,105);

手动操作可能比调用 map() 更快,但如果你的程序不是很复杂,这应该没问题。(analogRead(0) / 1023.0f * 105.0f)否则,您可能会考虑仅使用乘法来编写表达式:(analogRead(0) * 0.000977517107f * 105.0f)

高温高压

于 2011-04-13T14:19:17.880 回答