好的,接下来的问题是:如果我在数字输入上添加一个按钮,那么我可以用它来将电位器校准到零吗?
因此,当我按下按钮时,无论花盆在什么位置,所有值都从零开始?之后我会在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);
}
答案 0 :(得分:0)
尝试添加功能
float convertToMM(float reading)
{
return reading/1023.0*105.0;
}
然后当你进行阅读时,请改为
ZPot = convertToMM(analogRead(0));
答案 1 :(得分: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;
}
答案 2 :(得分: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)
HTH