I 'll start by explaining what I am trying to accomplish.
I have a Uno connected to an i2c 4x20 lcd display. It has 4 Freescale MPX series pressure sensors connected to analog inputs A0 to A3.
What I would like to do is have the four pressure sensors control 4 bars on the lcd.
I found a sketch in the exhibition section that will do exactly what I need. I managed to get it to display four bars numbered from 1 to 4.
What I need help with is attaching the four analog inputs to each individual bar. The original author has A0 controlling all four bars for demo purposes.
As you have heard many times I am a novice to coding so you will probably have to walk me through this step by step, which I would prefer so I can learn something.
Just for those that are wondering, I am building a unit to check the syncronization on motercyle carbs.
Here is the code
[code]
*/
// include the library code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h> // fmalpartida version
// download here: https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
// remove the standard Arduino liquidcrystal from ...programs/Arduino/libraries
#define lcdlength 20 // DEFINES YOUR display length in columns
#define lcdheigth 4 // DEFINES YOUR display heigth in rows
unsigned char b, b2;
double a, a2;
unsigned int segment, segment2;
double perc=100;
boolean logar;
byte bn; // bn BAR NUMBER every bar have to be numbered from 1 to 40
unsigned int xpos, ypos, blen;
double pkval[41]; // to store the last peak value for each bar
int pkcyc[41][2]; // set the num. of printbar recall TO DO [1] and DONE [2] for each bar before the peak decays,
// if pkcyc[bn][1] == 0 no peaks wanted
// it's a workaround to avoid to waste time in delay functions (internal or external)
// that may interfere your application performances
// Bar characters definitions (8 maximum allowed)
byte block[8][8]=
{
{ 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10 }, // define characters for bar
{ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18 },
{ 0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C },
{ 0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E },
{ 0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08 }, // define characters for peak
{ 0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04 },
{ 0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02 },
{ 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01 },
};
// #define del 100 // REMOVE IT, delay for testing purposes only
int potalim = 12; // REMOVE IT, to supply 5V al to testing potentiometer
// Set the pins on the I2C chip used for LCD connections:
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
// A0 A1 A2 in I2C unsoldered means 0x27 address
//==============
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(20,4); // initialize the lcd for 20 chars 4 lines and turn on backlight
pinMode(potalim, OUTPUT); // REMOVE IT, test purposes, defines pin 12 to power signal potentiometer
for( int i=0 ; i<8 ; i++ )
lcd.createChar( i,block[i] );
// lcd.backlight(); // LCD backlight on (default when init)
// lcd.noBacklight(); // LCD backlight off
}
void loop()
{
digitalWrite(potalim, HIGH); // REMOVE IT, TEST purposes, activates 5V on pin 12 for the signal potentiometer
// delay(del); // REMOVE IT, to slow down display without a real application running
// Assign value (in this case from Analog Input 0)
unsigned int value = analogRead(0);
perc = value/1023.0*100; // perc is the value from 0 to 100 for the length of the bargraph of the meter
// Setting parameters for bars ( you can put directly values inside the function)
// xpos=0;
// ypos=1;
// blen= 20;
// remember that bar starts from lower point raising of blen positions
// LEFT UPPER ANGLE IS xpos = 0 AND ypos = 0
logar = false;
// bn = 1; // BAR NUMBER every bar have to be numbered from 1 to 40
for (int i=1;i<41;i++) // setting 15 cycles for all bars... YOUR CHOICE !!
{ pkcyc[i][1]= 15;}
//==============================================================
// EXAMPLE OF PRINTING ON DISPLAY
// (I use same input for all bars for demo purposes)
// printbar (bn, perc, xpos, ypos, blen, logar);
lcd.setCursor(0,0); lcd.print ("1");
printbar (1,perc,1,0,19, true);
lcd.setCursor(0,1); lcd.print ("2");
printbar (2,perc,1,1,19, true);
lcd.setCursor(0,2); lcd.print ("3");
printbar (3,perc,1,2,19, true);
lcd.setCursor(0,3); lcd.print ("4");
printbar (4,perc,1,3,19, true);
// END OF EXAMPLE
//==============================================================
}
// METER BARS PRINTING FUNCTION
// passing percentage, x and y position, positions bar length, linear/audio logaritmic bar
// barnum is used to manage times of decayn of each peak separately (see definitions)
void printbar (byte bn, double perc, int xpos, int ypos, int blen, boolean logar)
{
if ((logar == true) && (perc > 0)) // logaritmic bar
{
perc = ( log10(perc ) )*50; // 10 * log10 (value) linear to logaritmic for AUDIO conversion
if ( perc < 0 ) { perc = 0; } // avoid negative values
}
a=blen/99.5*perc; // calculate length of bar
b = 0;
if ( pkcyc[bn][1] > 0 ) // if PEAK is activated
{
if ( (a > (pkval[bn]-0.01)) || (pkcyc[bn][2] > pkcyc[bn][1]) ) // new peak (w little histeresys) or expiration of peak
{
pkval[bn] = a;
pkcyc[bn][2] = 0; // reset cycles
}
pkcyc[bn][2]++;
}
// drawing filled rectangles
if (a>=1) {
for (int i=1;i<a;i++) {
lcd.setCursor(xpos-1+i,ypos);
lcd.write(255);
b=i;
}
a=a-b;
}
segment= a*5;
// drawing final part of the bar
if (b < blen)
{
lcd.setCursor(xpos+b,ypos);
switch (segment) {
case 0:
lcd.print(" ");
break;
case 1:
lcd.write((byte)0);
break;
case 2:
lcd.write(1);
break;
case 3:
lcd.write(2);
break;
case 4:
lcd.write(3);
break;
}
}
// cleaning rest of line
for (int i =0;i<(blen-b-1);i++)
{
lcd.setCursor(xpos+ b+ 1+ i, ypos);
lcd.print(" ");
}
b2= (int) pkval[bn];
a2= pkval[bn]-b2;
segment2= a2*5;
if ( (pkcyc[bn][1] > 0) && ( // DRAWING PEAK
((b + segment) == 0) // if bar empty
|| (b2 > b) // or different box position
|| ( (b2 == b) && segment == 0 && segment2 >0 ) // special case, too long to explain :-)
))
{
lcd.setCursor(xpos + b2, ypos);
switch (segment2) {
case 0:
if ( (b2 > 0) || (b2 > b+1))
{
lcd.setCursor(xpos + b2-1, ypos);
lcd.write(7);
};
break;
case 1:
lcd.write(byte(0));
break;
case 2:
lcd.write(4);
break;
case 3:
lcd.write(5);
break;
case 4:
lcd.write(6);
break;
}
}
}
[/code]