Sunday, June 1, 2014

Sain Smart LCD + Arduino Uno for voltage readout


Here is the code:

#include <LiquidCrystal.h> // include the LCD library
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 );

int VinPin = A5; //Voltage input pin
int VinValue1 = 0;
int VinValue2 = 0;
int TimeValue1 = 0;
#define NUM_SAMPLES 10

int sum = 0;                    // sum of samples taken
unsigned char sample_count = 0; // current sample number
float voltage = 0.0;            // calculated voltage

void setup() {
    lcd.begin(16, 2); // lcd rows and columns
    lcd.print("Volts  %   Hours"); // title of sorts
}

void loop() {
     while (sample_count < NUM_SAMPLES) {
        sum += analogRead(A5);
        sample_count++;
        delay(10);
    }
    voltage = ((float)sum / (float)NUM_SAMPLES * 5.015) / 1024.0;
    VinValue1 = analogRead(VinPin) / 10;
    VinValue2 = VinValue1 / 1.02;
    lcd.setCursor(0, 1);
    lcd.print(voltage);
    lcd.setCursor(7, 1);
    lcd.print(VinValue2);
    delay(100);
    lcd.setCursor(13, 1);
    TimeValue1 = VinValue2 / 12;
    lcd.print(TimeValue1);
    sample_count = 0;
    sum = 0;
    delay(1);
}