Coders:
I'm trying to figure out how to get the LCD display the Arduino calculated variables without using the PCD8544.h libraries. I've tried to use different ones from different sites, saved under different names so I know which one I'm using, but none of them seem to work. So, I found some code that will make LCD display in bitmap, which is fine for what I want to use it for.
It works until I try to make the LCD display a variable using such code as:
LCDString("%i V\n",batteryVoltage);
The compiler generates output errors stating it does not like this code:
void LCDString(char *characters)
{
while (*characters);
LCDCharacter(*characters++);
}
here is the full code. It is pieced together from other code.
#define PIN_SCE 7 //Pin 3 on LCD
#define PIN_RESET 6 //Pin 4 on LCD
#define PIN_DC 5 //Pin 5 on LCD
#define PIN_SDIN 4 //Pin 6 on LCD
#define PIN_SCLK 3 //Pin 7 on LCD
#define LCD_COMMAND 0
#define LCD_DATA 1
#define LCD_X 84
#define LCD_Y 48
int batMonPin = A4;
int batVal = 0; // variable for the A/D value
float pinVoltage = 0; // variable to hold the calculated voltage
float batteryVoltage = 0;
float ratio = 2.4; // Change this to match the MEASURED ration of the circuit, 12k R1 and 5k R2
int analogInPin = A0; // Analog input pin that the carrier board OUT is connected to
int sensorValue = 0; // value read from the carrier board
int outputValue = 0; // output in milliamps
unsigned long msec = 0;
float time = 0.0;
int sample = 0;
float totalCharge = 0.0;
float averageAmps = 0.0;
float ampSeconds = 0.0;
float ampHours = 0.0;
float wattHours = 0.0;
float amps = 0.0;
//This table contains the hex values that represent pixels
//for a font that is 5 pixels wide and 8 pixels high
static const byte ASCII[][5] = {
*** code snipped***
void setup()
{
Serial.begin(9600);
LCDInit();
}
void gotoXY(int x, int y)
{
LCDWrite(0, 0x80 | x); // Column.
LCDWrite(0, 0x40 | y); // Row. ?
}
void LCDBitmap(char my_array[])
{
for (int index = 0 ; index < (LCD_X * LCD_Y / 8) ; index++)
LCDWrite(LCD_DATA, my_array[index]);
}
void LCDCharacter(char character)
{
LCDWrite(LCD_DATA, 0x00); //Blank vertical line padding
for (int index = 0 ; index < 5 ; index++)
LCDWrite(LCD_DATA, ASCII[character - 0x20][index]);
LCDWrite(LCD_DATA, 0x00); //Blank vertical line padding
}
//Given a string of characters, one by one is passed to the LCD
void LCDString(char *characters)
{
while (*characters)
LCDCharacter(*characters++);
}
//Clears the LCD by writing zeros to the entire screen
void LCDClear(void)
{
for (int index = 0 ; index < (LCD_X * LCD_Y / 8) ; index++)
LCDWrite(LCD_DATA, 0x00);
gotoXY(0, 0); //After we clear the display, return to the home position
}
//This sends the magical commands to the PCD8544
void LCDInit(void)
{
//Configure control pins
pinMode(PIN_SCE, OUTPUT);
pinMode(PIN_RESET, OUTPUT);
pinMode(PIN_DC, OUTPUT);
pinMode(PIN_SDIN, OUTPUT);
pinMode(PIN_SCLK, OUTPUT);
//Reset the LCD to a known state
digitalWrite(PIN_RESET, LOW);
digitalWrite(PIN_RESET, HIGH);
LCDWrite(LCD_COMMAND, 0x21); //Tell LCD that extended commands follow
LCDWrite(LCD_COMMAND, 0xB9); //Set LCD Vop (Contrast): Try 0xB1(good @ 3.3V) or 0xBF if your display is too dark
LCDWrite(LCD_COMMAND, 0x04); //Set Temp coefficent
LCDWrite(LCD_COMMAND, 0x14); //LCD bias mode 1:48: Try 0x13 or 0x14
LCDWrite(LCD_COMMAND, 0x20); //We must send 0x20 before modifying the display control mode
LCDWrite(LCD_COMMAND, 0x0C); //Set display control, normal mode. 0x0D for inverse
}
//There are two memory banks in the LCD, data/RAM and commands. This
//function sets the DC pin high or low depending, and then sends
//the data byte
void LCDWrite(byte data_or_command, byte data)
{
digitalWrite(PIN_DC, data_or_command); //Tell the LCD that we are writing either to data or a command
//Send the data
digitalWrite(PIN_SCE, LOW);
shiftOut(PIN_SDIN, PIN_SCLK, MSBFIRST, data);
digitalWrite(PIN_SCE, HIGH);
}
void loop()
{
// read the analog in value:
sensorValue = analogRead(analogInPin);
// convert to milli amps
outputValue = (((long)sensorValue * 5000 / 1024) - 500 ) * 1000 / 133;
/* sensor outputs about 100 at rest.
Analog read produces a value of 0-1023, equating to 0v to 5v.
"((long)sensorValue * 5000 / 1024)" is the voltage on the sensor's output in millivolts.
There's a 500mv offset to subtract.
The unit produces 133mv per amp of current, so
divide by 0.133 to convert mv to ma
*/
batVal = analogRead(batMonPin); // read the voltage on the divider
pinVoltage = batVal * 0.00488; // Calculate the voltage on the A/D pin
// A reading of 1 for the A/D = 0.0048mV
// if we multiply the A/D reading by 0.00488 then
// we get the voltage on the pin.
batteryVoltage = pinVoltage * ratio; // Use the ratio calculated for the voltage divider
// to calculate the battery voltage
amps = (float) outputValue / 1000;
float watts = amps * batteryVoltage;
Serial.print("Volts = " );
Serial.print(batteryVoltage);
Serial.print("\t Current (amps) = ");
Serial.print(amps);
Serial.print("\t Power (Watts) = ");
Serial.print(watts);
sample = sample + 1;
msec = millis();
time = (float) msec / 1000.0;
totalCharge = totalCharge + amps;
averageAmps = totalCharge / sample;
ampSeconds = averageAmps*time;
ampHours = ampSeconds/3600;
wattHours = batteryVoltage * ampHours;
Serial.print("\t Time (hours) = ");
Serial.print(time/3600);
Serial.print("\t Amp Hours (ah) = ");
Serial.print(ampHours);
Serial.print("\t Watt Hours (wh) = ");
Serial.println(wattHours);
// LCD readout
// LCDClear();
gotoXY(0,0);
LCDString("12.13V 3.48A"); // should be displaying batteryVoltage and amps
gotoXY(0,1);
LCDString("42.2W 0.5H"); // should be batteryVoltage*amps and time
gotoXY(0,3);
LCDString("21.1WH 1.74AH"); //should be displaying wattHours and ampHours
delay(10);
}