Pages: [1]   Go Down
Author Topic: sketch issues and recomendations  (Read 901 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've only been at this for 2 days but I starting to get the hang of this.

I'm setting up a display that shows on a bar graph the pressure of 4 sensors. The sensor are .5v = 0 psi and 4.5v = 100 psi I have three issues with my code.

  • When I run this sketch the bar graph is on top of 'Oil Pressure' text I need to move the bar graph down to the second line and 'oil pressure' on the first
  • are there any tutorials or sample code on how to scale the analog to the .5v = 0 psi and 4.5v = 100 reference of the sensor I guess I'm not searching for the right thing
  • I have 4 sensors that all use this scale how can I interface a push button switch to toggle between the sensors / analog ports / sketches?

Here is my code so far. It's ugly but the POC is there.

Code:
#include <LiquidCrystal.h> // include the LCD library
#include <LcdBarGraph.h> // include the LCDBarGraph library
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

byte lcdNumCols = 16; // -- number of columns in the LCD
byte sensorPin = 0; // -- value for this example

LcdBarGraph lbg(&lcd, lcdNumCols);  // -- creating

int potPin = A0; //Potentiometer input pin
int potValue1 = 0;
int potValue2 = 0; // final display variable
void setup() {
    lcd.begin(16, 2); // lcd rows and columns
    lcd.print("Oil Pressure"); // title of sorts
}

void loop() {
// read then divide the input(max 1020 in this case) by 10
    potValue1 = analogRead(potPin) / 10;
// divide by 1.02 to get percentage
    potValue2 = potValue1 / 1.02;
// set cursor to second row, first column
    lcd.setCursor(0, 1);
//display final percentage
    lcd.print(potValue2);
//print the percent symbol at the end
    lcd.print("%");
//wait 0.1 seconds
    delay(100);
//wipe the extra characters
    lcd.print(" ");
    delay(1);
// -- draw bar graph from the analog value readed
    lbg.drawValue( analogRead(sensorPin), 1018);
// -- do some delay: frequent draw may cause broken visualization
    delay(100);
}

Thanks
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13739
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


// set cursor to second row, first column
    lcd.setCursor(0, 1);

shouldn't that be lcd.setCursor(1,1)  ??

did some code cleanup
Code:
#include <LiquidCrystal.h> // include the LCD library
#include <LcdBarGraph.h> // include the LCDBarGraph library

// DISPLAY VARS
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
byte lcdNumCols = 16; // -- number of columns in the LCD
LcdBarGraph lbg(&lcd, lcdNumCols);  // -- creating

// SENSOR VARS
int sensorPin = 0; // -- value for this example
int potPin = A0;   //Potentiometer input pin

float potVal = 0;
int sensorVal = 0;

void setup()
{
  lcd.begin(16, 2); // lcd rows and columns
  lcd.print("Oil Pressure"); // title of sorts
  delay(1000); // time to display title
}


void loop()
{
  // MAKE MEASUREMENTS
  potVal  = analogRead(potPin) / 10.2;       // potvalue 2 should be declared float ??
  sensorVal = analogRead(sensorPin);

  // DISPLAY RESULTS
  lcd.setCursor(1, 1);      // set cursor to second row, first column
  lcd.print(potVal, 1);     // float with one decimal
  lcd.print("%  ");         //  %  + wipe

  lbg.drawValue( sensorValue, 1018);
  delay(500);          // ~ 2Hz display is fast enough
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you very much for the cleanup I see that you you put in

float potVal = 0;
int sensorVal = 0;

What are they and how do they work?



BTW what part of the Netherlands are you from? I spent about 18 months in the Nijmegen Boxmeer area back in 2009 / 2010
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13739
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


potval is a float so it can hold a value like 10.1 that looks good for %%
sensor val is used for the bargraph and as the analoRead range  0..1023 will be mapped upon 0..16 an int is the appropiate datatype.

BTW,  I copied the names from your sketch smiley
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Sydney, AUS
Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your original code was correct:

// set cursor to second row, first column
    lcd.setCursor(0, 1);

the digits are for the column (starting from 0) followed by the row (again, starting from 0), so the 6th character on the second row would be:

// set cursor to second row, sixth column
    lcd.setCursor(5, 1);

Also, I believe you should set the lcdNumCol as an int instead of byte, since that's what the lcdbargraph library is expecting.  The same goes for pin numbers and so on.  Byte is more useful when you need to set a number using a binary stream..
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok I want to learn this along with other things but this is thumping my brain

So I looked at the LcdBarGraph library and it says.

"You also need to specify the number of character columns in the LCD which is unfortunately not available from the official LiquidCrystal library. "

Fine, its already there with the lcd.setCursor(0, 0) for the oil pressure print and lcd.setCursor(0, 1) for the bar graph (I think) I should be able to swap the rows and be done but the bar graph doesn't want to move.

what gives?




Code:
#include <LiquidCrystal.h> // include the LCD library
#include <LcdBarGraph.h> // include the LCDBarGraph library

// DISPLAY VARS
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
int lcdNumCols = 16; // -- number of columns in the LCD
LcdBarGraph lbg(&lcd, lcdNumCols);  // -- creating

// SENSOR VARS
int sensorPin = 0; // -- value for this example
int potPin = A0;   //Potentiometer input pin

float potVal = 0;
int sensorVal = 0;

void setup()
{
  lcd.begin(16, 2); // lcd rows and columns
  lcd.setCursor(0,1); // LCD format is Col,Row for 16 columns and 2 rows
  lcd.print("Oil Pressure"); // title of sorts
  delay(1000); // time to display title
}


void loop()
{
  // MAKE MEASUREMENTS
  potVal  = analogRead(potPin) / 10.2;       // potvalue 2 should be declared float ??
  sensorVal = analogRead(sensorPin);

  // DISPLAY RESULTS
  lcd.setCursor(0, 0);      // set cursor to second row, first column
  lbg.drawValue( analogRead(sensorPin), 1018);
  delay(500);          // ~ 2Hz display is fast enough
}
Logged

Sydney, AUS
Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#include <LiquidCrystal.h> // include the LCD library
#include <LcdBarGraph.h> // include the LCDBarGraph library

// DISPLAY VARS
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
int lcdNumCols = 16; // -- number of columns in the LCD
LcdBarGraph lbg(&lcd, lcdNumCols);  // -- creating

// SENSOR VARS
int sensorPin = 0; // -- value for this example
int potPin = A0;   //Potentiometer input pin

float potVal = 0;
int sensorVal = 0;

void setup()
{
  lcd.begin(16, 2); // lcd rows and columns
  lcd.setCursor(0,0); // LCD format is Col,Row for 16 columns and 2 rows
  lcd.print("Oil Pressure"); // title of sorts
  delay(1000); // time to display title
}


void loop()
{
  // MAKE MEASUREMENTS
  potVal  = analogRead(potPin) / 10.2;       // potvalue 2 should be declared float ??
  sensorVal = analogRead(sensorPin);

  // DISPLAY RESULTS
  lcd.setCursor(0,1);      // set cursor to second row, first column
  lbg.drawValue(analogRead(sensorPin), 1018);
  delay(500);          // ~ 2Hz display is fast enough
}


your setCursors weren't correct for what you wanted to do - fixed in the sketch above.  Hopefully it produces the desired result.  Also, is 1018 the maximum value that your sensor will give?  Why not use 1024 for the full range of the sensor pin?  Yes, it's only 6 more, but you might as well use the whole range the pin can provide, unless of course you want to narrow it down to a smaller range.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I did the changes you made and I get the same result the bargraph stays at first row. Weird. I've just given up and decided to just put the title under the bargraph.

Now I'm on to doing the same for 3 more analogs and trying to figure out how to toggle between all 4 analogs and show them on the LCD. I have a feeling its going to take me a while because I'm not finding any examples.  smiley-cry 
Logged

Sydney, AUS
Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So I had a dig through the code for the LCDBarGraph library, and it looks like it forces the output to be on the first line (line 0).

Luckily, it's also easy to modify it to accept an integer value for the line to output on and then use that value to select the line.  I've done the changes to the attached version of the library.. Hopefully it all works smiley

The new format for calling the library is drawValue(int Value, int maxValue, int LCDRow)

So to output 637 out of 1024 on the second row of your screen, you'd call drawValue(637, 1024, 1)



**EDIT - oops, of course, I just realised that since you get 1024 values on analog pins, the maxvalue for the graph would be 1023 since it counts from 0 smiley-razz  so drawValue(637, 1023, 1) instead.

* LcdBarGraph-v1.1.zip (15.97 KB - downloaded 9 times.)
« Last Edit: April 01, 2011, 02:24:30 pm by Daneel » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 42
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So I've racked my brain, going blind looking a other code sources to find that the original library cant change the line and the Noob is the first find it.

Great, ok


I'll try the new library this weekend.
Logged

Pages: [1]   Go Up
Jump to: