Sunday, April 22, 2012

Displaying A Read Input (or, On My Way To A 1.5V Battery Monitor/Charger)

The reason I need access to those four pins when the LCD shield is on-board (see previous post...) is for the battery monitor/trickle charger I'm working on.  At this point I have the code done for reading analog pins 0~3, converting the data to voltage, and displaying the voltage on the LCD shield.

I'm glad I used the 2-row sockets.  The LCD shield covers the first row!
The readings on the display are the 'floating values' of those pins.  Without the pins tied to something, a voltage or ground, they have a random level.  This level will actually fluctuate with what is in proximity to the pins, even you finger.

The code is simple, but one step at a time...

/*********************
Alkaline Battery Trickle Charger
LCD sections based on example code for the Adafruit RGB Character LCD Shield and Library
**********************/

//This section from Adafruit
// include the library code:
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>

// The shield uses the I2C SCL and SDA pins. On classic Arduinos
// this is Analog 4 and 5 so you can't use those for analogRead() anymore
// However, you can connect other I2C sensors to the I2C bus and share
// the I2C bus.
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();

// These #defines make it easy to set the backlight color
#define RED 0x1
#define YELLOW 0x3
#define GREEN 0x2
#define TEAL 0x6
#define BLUE 0x4
#define VIOLET 0x5
#define WHITE 0x7

//This Section from Clark
// Define pin positions
int Battery_1 = 0;
int Battery_2 = 1;
int Battery_3 = 2;
int Battery_4 = 3;

// Variables to hold read pin value
float B1_Percent_of_5V;
float B2_Percent_of_5V;
float B3_Percent_of_5V;
float B4_Percent_of_5V;

void setup()

  // set up the LCD's number of rows and columns:
  lcd.begin(16, 2);
  // Prints the template to the screen
  lcd.setBacklight(VIOLET);
  lcd.print("B1    V  B2    V");
  lcd.setCursor(0, 1);
  lcd.print("B3    V  B4    V");
}

void loop()
{
  //Read analog input pins (% of 5V reference)
  B1_Percent_of_5V = analogRead(Battery_1);
  B2_Percent_of_5V = analogRead(Battery_2);
  B3_Percent_of_5V = analogRead(Battery_3);
  B4_Percent_of_5V = analogRead(Battery_4);
 
  // Set cursor position within the template,
  // convert % to voltage, display voltage
  lcd.setCursor(3, 0);
  lcd.print((B1_Percent_of_5V * 0.0049),1);
  lcd.setCursor(12, 0);
  lcd.print((B2_Percent_of_5V * 0.0049),1);
  lcd.setCursor(3, 1);
  lcd.print((B3_Percent_of_5V * 0.0049),1);
  lcd.setCursor(12, 1);
  lcd.print((B4_Percent_of_5V * 0.0049),1);
}

No comments: