[英]Trash on Arduino serial monitor. How to fix it?
i'm working with I2C eeprom and Arduino. 我正在使用I2C eeprom和Arduino。 For now i tried to create simple keyboard that will start specific functions.
现在,我尝试创建简单的键盘来启动特定功能。 I want to write to serial monitor potentiometer value, but i get trash instead it.
我想写入串行监视器电位器值,但是却收到了垃圾。 How to fix it?
如何解决? My functions:
我的职能:
int *readPot() ///read potentiometer value
{
int tempValue = analogRead(A0);
int *potValue = &tempValue;
return potValue;
}
void keyboardProcess() ///process keyboard input
{
int *potValue = readPot();
for(int i = 0; i < 2; i++)
{
btnReadings[i] = digitalRead(keysPins[i]);
}
if(btnReadings[0] == HIGH)
{
Serial.println("Potentiometer reading:" + *potValue);
}
}
One obvious problem is that you are returning the address to a local variable: 一个明显的问题是您正在将地址返回到局部变量:
int *readPot() ///read potentiometer value
{
int tempValue = analogRead(A0);
int *potValue = &tempValue;
return potValue;
}
Here, the returned pointer points to the address of tempValue
. 在这里,返回的指针指向
tempValue
的地址。 This ceases to be valid once the function returns. 函数返回后,该函数将不再有效。 Just use an
int
: 只需使用
int
:
int readPot() ///read potentiometer value
{
return analogRead(A0);
}
Next, I doubt that this is a valid argument to Serial.println
: 接下来,我怀疑这是
Serial.println
的有效参数:
Serial.println("Potentiometer reading:" + *potValue);
but this should work: 但这应该工作:
int potValue = readPot();
Serial.print("Potentiometer reading: ");
Serial.println(potValue);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.