not able to continuously get the value of pot

Hello everyone,

I have a simple problem, I'm new to coding so sorry it this is easy for some of you.

But what I would like to do is have the display show select puzzle# and then print out the pot value.

For the pot.h file I have

class Potentiometer
{
private:
  const int pot1 = analogRead(0); // Local variable  pot on pin A0
public:
  int r_pot();
}; // end Class Potentiometer

then for the pot.cpp I have

//*****************************************Functions**************************//
int Potentiometer::r_pot()
{
  int potmap1 = map(pot1, 0, 1023, 1, 3); // map values 1-3 to set timer length
  return (potmap1);
} // Close r_pot

and for the display I have

void Display::print(int x, int y, int value)
{   

    u8g2.clearBuffer();              // clear the internal memory
    u8g2.setFlipMode(1);             // Flips display 180 (1) = True
    u8g2.setFont(u8g2_font_9x18_tf); // choose a suitable font
    u8g2.drawStr(0, 12, "Select Puzzle");
    u8g2.drawStr(x, y, "#");
    u8g2.setCursor(60, 30); // set cursor location
    u8g2.print(value);
    u8g2.sendBuffer(); // transfer internal memory to the display
} // end void OLEDdraw

and for the main.cpp I have

//******************************************Declare****************************//
Potentiometer Potentiometer0;

void loop()
{
 Display0.print(45, 30,Potentiometer0.r_pot() );
}

I'm able to plot the value of pot but only one time, Why doesn't this run continuously if it is in void loop. Do I need to have this in a while loop?

Thank you for response,
Joe

34 posts and you still don't use code tags? It is time to learn...
For the pot.h file I have

class Potentiometer
{
private:
  const int pot1 = analogRead(0); // Local variable  pot on pin A0
public:
  int r_pot();
}; // end Class Potentiometer

then for the pot.cpp I have

int Potentiometer::r_pot()
{
  int potmap1 = map(pot1, 0, 1023, 1, 3); // map values 1-3 to set timer length
  return (potmap1);
} // Close r_pot

and for the display I have

void Display::print(int x, int y, int value)
{  

    u8g2.clearBuffer();              // clear the internal memory
    u8g2.setFlipMode(1);             // Flips display 180 (1) = True
    u8g2.setFont(u8g2_font_9x18_tf); // choose a suitable font
    u8g2.drawStr(0, 12, "Select Puzzle");
    u8g2.drawStr(x, y, "#");
    u8g2.setCursor(60, 30); // set cursor location
    u8g2.print(value);
    u8g2.sendBuffer(); // transfer inter

and for the main.cpp I have

void loop()
{
  Display0.print(45, 30,Potentiometer0.r_pot() );
}

How is anybody supposed to test this? You need a complete sketch..

Do you have to work with those pastel colours ?

This method should read the potentiometer:

int Potentiometer::r_pot()

and this will be a source of disappointment, especially if you are expecting to see some activity when turning the potentiometer:

const int pot1 = analogRead(0) ; // Local variable pot on pin A0

6v6gt:
Do you have to work with those pastel colours ?

This method should read the potentiometer:

int Potentiometer::r_pot()

and this will be a source of disappointment, especially if you are expecting to see some activity when turning the potentiometer:

const int pot1 = analogRead(0) ; // Local variable pot on pin A0

Hello 6v6gt, No The colors for some reason when I copy and paste the code came out weird, but I am using normal dark mode colors.

The method does read the pot value but it seems to only get the number one time. When I try turning the value of the pot it doesn't change.

Do you mean I'm a disappointment b/c I can't figure this out? I am expecting to see some activity. Do you think this has to do with me using the const int ?

blh64:
How is anybody supposed to test this? You need a complete sketch..

Sorry Sir, I now understand what you are talking about, I did end up fixing that issue.

What do you mean you need a complete Sketch?

Ok I figured it out, I was only grading the value you time .

void loop()
{
  Potentiometer Potentiometer1;
  Display0.print(45, 30, Potentiometer1.r_pot() );

} //end void loop

Adding Potentiometer Potentiometer1;
in my void loop fixed the issue

OK. But that is a rather rough solution because you have to instantiate the object on each loop iteration to refresh a "constant" value.

Here is how I'd do it. It also allows you to use multiple potentiometers if necessary

class Potentiometer
{
  private:
    uint8_t _aPin ;
  public:
    Potentiometer( uint8_t ) ; // Constructor parameter analog pin number e.g. A0
    int r_pot();
}; // end Class Potentiometer


// constructor
Potentiometer::Potentiometer( uint8_t aPin ) {
  _aPin = aPin ;
}

int Potentiometer::r_pot()
{
  int potmap1 = map(analogRead( _aPin ), 0, 1023, 1, 3); // map values 1-3 to set timer length
  return (potmap1);
} // Close r_pot




Potentiometer Potentiometer0( A0 ) ;  // now analog pin is specified

void setup() {
  Serial.begin( 115200 ) ;
}


void loop() {
  // Display0.print(45, 30,Potentiometer0.r_pot() );
  Serial.println( Potentiometer0.r_pot() ) ;
  delay( 1000 ) ;
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.