Question

i'm working with I2C eeprom and 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); 

    }
 }

Serial Monitor

Était-ce utile?

La solution

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. This ceases to be valid once the function returns. Just use an int:

int readPot() ///read potentiometer value
{
   return analogRead(A0);
}

Next, I doubt that this is a valid argument to Serial.println:

Serial.println("Potentiometer reading:" + *potValue); 

but this should work:

int potValue = readPot();
Serial.print("Potentiometer reading: ");
Serial.println(potValue); 
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top