Go Down

Topic: Bargraph problem (Read 114 times) previous topic - next topic

Langoni

Dear all,
I have had some problem to understand a code from Github about a Bar-graph color bar.
When I use this code, the bars light up to the end and erase to the beginning in succession. This process repeats automatically. However I would like to use Bar-graph to read some variable, for example ultraviolet radiation level. How can I do this with this code. What changes need to be made?
I really thank you for any help.....

Here is the code:
Code: [Select]
#include <UTFT.h>
UTFT myGLCD(SSD1963_480,38,39,40,41); //myGLCD(RS,WR,CS,RST,ALE,mode);
// Declare which fonts we will be using
extern uint8_t SmallFont[];
extern uint8_t BigFont[];
byte rgbtable[34][3]{
  {0,0,255},
  {0,32,255},
  {0,64,255},
  {0,96,255},
  {0,128,255},
  {0,160,255},
  {0,192,255},
  {0,224,255},
  {0,255,255},
  {0,255,224},
  {0,255,192},
  {0,255,160},
  {0,255,128},
  {0,255,96},
  {0,255,64},
  {0,255,32},
  {0,255,0},
  {32,255,0},
  {64,255,0},
  {96,255,0},
  {128,255,0},
  {160,255,0},
  {192,255,0},
  {224,255,0},
  {255,255,0},
  {255,224,0},
  {255,192,0},
  {255,160,0},
  {255,128,0},
  {255,96,0},
  {255,64,0},
  {255,32,0},
  {255,0,0}
  };
void setup()
{
  myGLCD.InitLCD();
  myGLCD.clrScr();
  myGLCD.setFont(BigFont);
  myGLCD.setColor(VGA_WHITE);
  myGLCD.setBackColor(VGA_BLACK);
 
}
//END OF SETUP
//drawColorBar(value 0-32, x coordinate of left of graph, y coordinate of top left corner of graph, size);
void drawColorBar(int value,int spotx, int spoty, int pix){
  for(int i=0;i<33;i++){
    if(i<=value){
    myGLCD.setColor(rgbtable[i][0],rgbtable[i][1],rgbtable[i][2]);
    } else {myGLCD.setColor(VGA_GRAY);}
    myGLCD.fillRect(spotx+(i*pix),spoty,spotx+(i*pix)+pix/2,spoty+18);
  }
 myGLCD.setColor(VGA_BLACK);
 myGLCD.fillRect(spotx+(32*pix)+58,spoty,spotx+(32*pix)+90,spoty+17);
 myGLCD.setColor(rgbtable[value][0],rgbtable[value][1],rgbtable[value][2]);
 myGLCD.print("V: "+String(value)+"",spotx+(32*pix)+10,spoty+4);
}

//LOOP
void loop(){
  //LOOP DO
  myGLCD.setFont(BigFont);
      myGLCD.setColor(VGA_WHITE);
      myGLCD.print("ARDUINO COLOR BAR COLD-HOT",0,0);
      myGLCD.setColor(VGA_YELLOW);
      myGLCD.print("FREE CODE AVAILABLE!",0,16);
  for(int i=0; i<33; i++){
      drawColorBar(i,0,35,3);
      drawColorBar(i,0,65,4);
      drawColorBar(i,0,95,5);

      drawColorBar(i,0,125,6);
      drawColorBar(i,0,155,7);
      drawColorBar(i,0,185,8);
      drawColorBar(i,0,215,9);
      drawColorBar(i,0,245,10);
      delay(1);
}
myGLCD.setColor(VGA_RED);
myGLCD.print("SUBSCRIBE FOR MORE! ",0,16);
for(int i=32; i>=0; i--){
      drawColorBar(i,0,35,3);
      drawColorBar(i,0,65,4);
      drawColorBar(i,0,95,5);

      drawColorBar(i,0,125,6);
      drawColorBar(i,0,155,7);
      drawColorBar(i,0,185,8);
      drawColorBar(i,0,215,9);
      drawColorBar(i,0,245,10);
      delay(1);
}
}

Thanks,
Cesar

lesept

It's explained here:
Code: [Select]
drawColorBar(value 0-32, x coordinate of left of graph, y coordinate of top left corner of graph, size);The value you want to display (UV radiation level) should be translated in a scale from 0 to 32 (meaning the minimum value is translated to 0 and the maximum to 32). You can use the map function to do that.
Then you call the abovementionned drawColorBar function, with the arguments as explained.
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

Langoni

Hi Lesept,

At first, thanks for your help.

I understand clear about the map and the scale. However, it is not clear to me how introduce the variable in the process. What stop this counters...Is that counters just a test set? or they are necessary to increase the variable?
Do I need to remove the counters?

I'm just a beginner on arduino codes.

Thanks a lot for your patience.

Cesar

lesept

An arduino code is made of 3 parts :
  • Variables and libraries definition (head of the ino file)
  • The setup function, to define and do what you want done only once
  • The loop function which iterates forever.
In the setup, you do all the definitions and initializations. There you have the instructions for the initial variables values, all the parts of the display that won't change, etc.
In the loop, you do what is dynamic, such as sensor reading and display. So you must add some instructions in the loop to read the value from the sensor and call the drawColorBar to show the bar.

So your code will look like:
Code: [Select]
libraries declaration
constants declarations

void setup () {
  serial declaration and initialization
  initial values of global variables
  pin declarations
  display text or anything that won't change
}

void loop {
  read sensor value, put it in a variable (say X)
  map X to 0 - 32
  display the bar
  do whatever else you want
}
The loop will run forever, in a ... loop!

So forget the counters, they were in that piece of code for its own purpose. You just need to translate what I suggest above into C code.
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

Langoni

Hi Lesept,

Now I understand. Thank you a lot for explain me...

Cesar

Go Up