GLCD bar graph with pot

i`m trying to make movable bar that will be related to pot reading (0-1024), but for some reason nothing is happening.
any help much appreciated.

#include "ST7565.h"

int redPin = 12;
int greenPin = 11;
int bluePin = 10;
int pot = A7;
char Val[5]; 


// pin 9 - Serial data out (SID)
// pin 8 - Serial clock out (SCLK)
// pin 7 - Data/Command select (RS or A0)
// pin 6 - LCD reset (RST)
// pin 5 - LCD chip select (CS)
ST7565 glcd(9, 8, 7, 6, 5);

#define LOGO16_GLCD_HEIGHT 16 
#define LOGO16_GLCD_WIDTH  16 

// a bitmap of a 16x16 fruit icon
static unsigned char __attribute__ ((progmem)) logo16_glcd_bmp[]={
0x30, 0xf0, 0xf0, 0xf0, 0xf0, 0x30, 0xf8, 0xbe, 0x9f, 0xff, 0xf8, 0xc0, 0xc0, 0xc0, 0x80, 0x00, 
0x20, 0x3c, 0x3f, 0x3f, 0x1f, 0x19, 0x1f, 0x7b, 0xfb, 0xfe, 0xfe, 0x07, 0x07, 0x07, 0x03, 0x00, };


// The setup() method runs once, when the sketch starts
void setup()   {                
  Serial.begin(9600);

  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT); 
  pinMode(pot, INPUT);
  

  // initialize and set the contrast to 0x18
  glcd.begin(0x18);


  glcd.display(); // show splashscreen
  delay(2000);
  glcd.clear();
}

void loop()                     
{

  
  int sensorValue = analogRead(pot);
Serial.println(sensorValue);
glcd.drawstring(0, 0, "TEST ");
glcd.drawrect(20, 16, 6, 30, BLACK);
  
sprintf(Val, "%d", sensorValue);
glcd.drawstring(50, 0, Val);


if(sensorValue < 1000)
{  
glcd.drawstring(50, 0, Val);  
glcd.drawstring(68, 0, " "); // show empty space

}

else if(sensorValue < 100)
{  
  glcd.drawstring(50, 0, Val);
  glcd.drawstring(62, 0, "  "); // show 2 empty spaces
  
}

else if(sensorValue < 10)
{ 
 glcd.drawstring(50, 0, Val);
  glcd.drawstring(56,0, "   "); // show 3 empty spaces
  
}
else glcd.drawstring(50, 0, Val);
glcd.display();
glcd.clear();



}
void Bar(int value = pot, int low = 0, int high = 1024, int x = 20, int y = 16, int w = 6, int h = 30, int F_color =  BLACK, int B_color = WHITE)
{
  static int lastV = -1, move = 0;
  int Val = map(value, low, high, h, y);
      
    if (Val != lastV) // prevents it from constantly being redrawn on the screen

  {
    if ( Val > lastV)
    {
      for (move; move < Val; move++)
      {

        glcd.fillrect(x, y, w + 1, (h - 1) - move, F_color);
      }
      lastV = Val;
    }
    else
    {
      for (move; move > (lastV + 1); move--)
      {
        glcd.fillrect(x + w, y + h, w + 1, (h - 1) + move, B_color);
      }
      lastV = Val;
      glcd.display();
      
    }
  }
}

Bump

      for (move; move < Val; move++)

Why are you using a static variable for a loop index?

but for some reason nothing is happening.

It’s hard to believe that NOTHING is happening. Perhaps you could explain what this means.

this was a sample code that HazardsMind suggested to try. by meaning that nothing is happening i`ve try to say that nothing is displayed on the screen. not even a blank rect. that i gues should be filled by glcd.fillrect(). while pot goes from 0-1024

i`m trying to make movable bar that will be related to pot reading (0-1024)

That is NOT what the code is doing. It is printing text with varying numbers of leading spaces.

What, exactly, are you seeing on the LCD?

Are we talking about that?

void Bar(int value = pot, int low = 0, int high = 1024, int x = 20, int y = 16, int w = 6, int h = 30, int F_color =  BLACK, int B_color = WHITE)
{
  int sensorValue = analogRead(pot);
  static int lastV = -1, move = 0;
  int Val = map(value, low, high, h, y);
      
    if (Val != lastV) // prevents it from constantly being redrawn on the screen

  {
    if ( Val > lastV)
    {
      for (move; move < Val; move++)
      {

        glcd.fillrect(x, y, w + 1, (h - 1) - move, F_color);
      }
      lastV = Val;
    }
    else
    {
      for (move; move > (lastV + 1); move--)
      {
        glcd.fillrect(x + w, y + h, w + 1, (h - 1) + move, B_color);
      }
      lastV = Val;
      glcd.display();
      
    }
  }
}

Are we talking about that?

Yes, and no. That might be a useful function, IF you actually called it.

Defining a function will all default values rarely makes sense. In this case, it does NOT.

Can you direct me where i can find some info about how to do it? And YES i searched google,youtube etc. ST7565 seams not to have much backup.

ST7565 seams not to have much backup.

What does this have to do with calling, or not calling, a function that you've been given?

What does it have to do with answering, or not answering, the questions you've been asked?

Before we go any farther, are you able to see ANYTHING on the LCD, correct or not? If you are, what do you see?

Yes. The “test” "rect"and pot value us displayed correctly and how i want. Pot value showes from 0-1024 with right spaces.

mOskit:
Yes. The “test” "rect"and pot value us displayed correctly and how i want. Pot value showes from 0-1024 with right spaces.

OK. So delete all the code in loop that deals with making text appear on the LCD.

The loop() function should read the analog pin, and call Bar() with appropriate value(s). The simplest form would be

void loop()
{
   int potVal = analogRead(potPin);
   Bar(potVal);
}

Hi.
Right now the screen starts with solid rect.(7px by around 30px (hard to cunt)).
This Rect is not responding to pot.

#include "ST7565.h"

int redPin = 12;
int greenPin = 11;
int bluePin = 10;
int pot = A7;



ST7565 glcd(9, 8, 7, 6, 5);


void Bar(int value = pot, int low = 0, int high = 1024, int x = 20, int y = 16, int w = 6, int h = 30, int F_color =  BLACK, int B_color = WHITE);
void setup()   
{

  Serial.begin(9600);

  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT); 
  pinMode(pot, INPUT);
  
 
  
   // initialize and set the contrast to 0x18
  glcd.begin(0x19);
  glcd.display(); // show splashscreen
  delay(2000);
  glcd.clear();
  
 
   
}
void loop()
{
   
  int potVal = analogRead(pot);
  Serial.println(potVal);
  Bar(potVal);
  
  glcd.display();
  
      
  
}


 void Bar(int value, int low, int high, int x, int y, int w, int h, int F_color, int B_color)
{
  
  int potVal = analogRead(pot);
  static int lastV = -1, move = 0;
  int Val = map(value, low, high, h, w);
      
    if (Val != lastV) // prevents it from constantly being redrawn on the screen

  {
    if ( Val > lastV)
    {
      for (move; move < Val; move++)
      {

        glcd.fillrect(x, y, w + 1, (h - 1) - move, F_color);
      }
      lastV = Val;
    }
    else
    {
      for (move; move > (lastV + 1); move--)
      {
        glcd.fillrect(x + w, y + h, w + 1, (h - 1) + move, B_color);
      }
      lastV = Val;
      glcd.display();
      
                 
      
    }
  }
}

Why are you again reading the pot value in the Bar() function? The Bar() is NOT supposed to read the pot.

      for (move; move < Val; move++)

It makes NO sense to use a static variable for a for loop index.

It does not make sense to call glcd.display() in Bar(), when removing rectangles, but not when adding them. It makes no sense to call glcd.display() in Bar() and then again in loop() after Bar() is done. Make up your mind where you are going to call it.

Ok. i just have to say that i dont know what im doing and i don`t know what particular function does. I have to start from scratch, or maybe go to college to do some c++ course.

int value = pot should i use pot for value?

I have to start from scratch, or maybe go to college to do some c++ course.

These choices are not mutually exclusive.

Taking a course with a live instructor is a good idea. Blank looks are completely missed by a book or you-tube video.

However, this particular function is not all that difficult. It looks imposing because of the number of arguments. But, if you look at them, the last two deal with what color box to draw on what color background. For your purposes, these could be hardcoded, instead of passed as arguments.

The two before that are the height and width of the rectangles that make up the bar. For your purposes, these could be hardcoded, instead of passed as arguments.

The two before that are the start location of the first rectangle that makes up the bar. For your purposes, these could be hardcoded, instead of passed as arguments.

Now, we are down to three arguments - the value and the range. For your purposes, the range could be hardcoded, instead of passed as arguments.

That leaves one argument.

As to what the function does, that's relatively simple, too. Basically, it determines how many rectangles to draw. If that number of rectangles matches what is currently drawn, the function does nothing.

If there are fewer rectangles drawn than are needed, it draws more rectangles.

If there are more rectangles drawn than are needed, it erases some rectangles.

So, the function boils down to: Compute the number of rectangles needed this time. Compare that to the number of rectangles shown (that is what the static variable lastV is holding). If not the same: If more rectangles are needed: Draw them from left to right. Else Erase the extras from right to left.

Drawing and erasing are done in a for loop. The starting index and the ending index are determined (or are determinable) each time the function is called. There is no need to remember the previous value, so the static variable move is not needed.

Can you see what code is determining the number of rectangles to draw? Can you see where the comparison to the number of rectangles currently draw happens? Can you see where the decision regarding whether to draw or erase rectangles happens?

If so, then, fixing the program to do what you want should be relatively simple.

If not, say so, and I'll point out the bits of code that do what I outlined above.

If you still find yourself having difficulties, do what I do. Write the comments first. If writing the comments does not clarify the needed code for you, at least you'll have something that you can show us that illustrates that you understand, or do not understand, what needs to be done.

If you do, we'll help you convert the comments to code. If not, we'll help with that step.

PaulS:

  1. Can you see what code is determining the number of rectangles to draw?
  2. Can you see where the comparison to the number of rectangles currently draw happens?
  3. Can you see where the decision regarding whether to draw or erase rectangles happens?
  1. int value ?
  2. if ( Val > lastV)
    3)for (move; move < Val; move++)draw, for (move; move > (lastV + 1); move–)erase

I appreciate what you wrote, but arguments like color, screen location width and height are clear for me.
things that arent clear for me are “value”, “low”, “high”.
Until i understand where those must be sourced I`m unable to understand further part of the code.

thanks

  1. int value ?
  int Val = map(value, low, high, h, w);

This is the code to determine the amount of the bar to cover with rectangles. However, it does not seem to make sense. The variable value is the reading from the pot. The low and high values are the range of values that value could take on. They define the “from” range. The “to” range is normally something like 0 to n, where n is the upper limit of a servo, or n is the maximum brightness value for an LED.

I would expect the mapping to be from the left edge of the bar area to the right edge. I can’t see how height and width are appropriate value for the “to” range. So, I think that this is the first thing that needs fixing.

  1. if ( Val > lastV)

Correct.

3)for (move; move < Val; move++)draw, for (move; move > (lastV + 1); move–)erase

No. That is where the decided upon action is implemented.

The correct answer is:

    if ( Val > lastV)
    {

Add some Serial.print() and Serial.println() statements to the code. What is input to the function? Is that reasonable?

What is the input mapped to? Is that reasonable?

How many rectangles are currently drawn? Is that reasonable?

Any time the answer is “No, that is not reasonable”, you have to look at how the unreasonable value is generated, and deal with that problem. Eventually, the code will do what you want, AND you’ll understand why it does/didn’t.

I found the error.

Try this.

void Bar(int value, int low, int high, int x, int y, int w, int h, int F_color, int B_color)
{
  static int lastV = -1, move = 0;
  int Val = map(value, low, high, 0, h-1);

  if (Val != lastV) // prevents it from constantly being redrawn on the screen
  {
    if ( Val > lastV)
    {
      for (move; move < Val; move++)
      {
        glcd.drawline(x, y - move, w + 1, y - move, F_color);
      }
      lastV = Val;
    }
    else
    {
      for (move; move > lastV; move--)
      {
        glcd.drawline(x, y - move, w + 1, y - move, B_color);
      }
      glcd.display();
    }
    lastV = Val;
  }
}

Hi. looks like something started to work. there is another thing I don`t understand.

Say you want to make this bar 0 whe pot=0 and 20 when pot=1024. The only way to get the bar progres moving 20px up was setting the height to 22. To get the width 6px "int w" must be 10. screen position x,y seams to be ok.

int value = pot, int low = 0, int high = 1024, int x = 6, int y = 50 , int w = 10, int h = 22, int F_color =  BLACK, int B_color = WHITE

And now. If i set int x=10, int y = 50, int w = 10, int h = 22 the bar turn to be 2px width only.

Also. The bar instead clearing to "nothing displayed" end with displaying 2px hight.

That can be done in the code. Right now for (move; move < Val; move++) will increment move until move is less than Val, but once move is equal or greater then it will exit the loop.

So if you want it to line up, then you need to subtract 1 from move when you get to the next for loop, for (move; move > lastV; move–). Everything should line up after that.

If not, then I will figure it out when I get home tonight.