Go Down

Topic: Help with Parallax 2x16 Serial LCD? (Read 1 time) previous topic - next topic

ernieb

Don, point well taken.

Here is a program which uses the serial display and a simple pot fro gnd to +5 and it works as a voltmeter.
Hope it is helpful to others.

Ernie

Code: [Select]

/*
  LiquidCrystal Serial

Demonstrates the use a Paralax 2X16 Serial LCD display. 

This sketch prints "Hello, world..." to the first line LCD 
and "From Ernie" in the second line. It then loops with a
counter running and  plays with the backlight and display.

The circuit:
* LCD Serial Pin to digital pin 7

By Ernie Bose
Created 2 May 2012
by Ernie Bose

  This example code is in the public domain.
  display information can be found here:
    http://www.parallax.com/Portals/0/Downloads/docs/prod/audiovis/27976-7-9-ParallaxSerialLCD-v3.0.pdf
    The command set describes what each command does to the display.
*/


//============================================================================
//  Constants used so all the setup stuff is in one place

//============================================================================

const int TxPin = 7;               // serial output to display
const int VinPin = A0;              // analog input for measurement
const int Baud = 9600;


#include <SoftwareSerial.h>
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);

//============================================================================
// The int2voltstr subroutine converts an integer value in the range 0 to 1024
// and converts it to a string of the format 00.00. The value is scaled by the
// map function so that the range is scaled to represent 0 to 500 for 5 volts.
//
// The string is returned.
//============================================================================

String int2voltstr (int v)
{

  int vv = v;
  char voltageString[6];

  // Do these two first since they don't change:
  voltageString[5] = 0;    // Null termination for the string
  voltageString[2] = '.';  // Decimal separator can be changed to comma


  // Make the value be in between 0 and 500
  int mappedValue = map( vv, 0, 1023, 0, 500 );

  // Working backwards, take the bottom digit and shift
  voltageString[4] = '0' + mappedValue % 10; // Take the bottom digit
  mappedValue /= 10;    // Shift by one digit
  voltageString[3] = '0' + mappedValue % 10; // Take the bottom digit
  mappedValue /= 10;    // Shift by one digit

  // Decimal was already written above, skip voltageString[2]
  voltageString[1] = '0' + mappedValue % 10; // Take the bottom digit
  mappedValue /= 10;    // Shift by one digit
  voltageString[0] = '0' + mappedValue % 10; // Take the bottom digit

  return voltageString;
}


void setup() {
   
  pinMode(TxPin, OUTPUT);
  digitalWrite(TxPin, HIGH);
  pinMode(VinPin, INPUT);
  mySerial.begin(Baud);               // Set serial Baud rate
  delay(100);                         // wait for the display 
  mySerial.write(12);                 // Clear             
  mySerial.write(17);                 // Turn backlight on
  delay(5);                           // Required delay
  // The next lines are for debug purposes and not needed in the final
  //mySerial.print("Hello, world...");  // First line
  //mySerial.write(13);                 // Form feed
  //mySerial.print("from Ernie!");      // Second line
  //mySerial.write(212);                // Quarter note
  //mySerial.write(220);                // A tone

}

void loop() {

  String strVolts;                    // String to store the results

  int volts = analogRead(VinPin);    // Get the value from the input pin
  mySerial.write(12);                // write form feed to the display to clear
  mySerial.write(148);               // put the cursor at the beginning of line 2
 
  mySerial.print(volts);             // print the raw analog pin data
  strVolts = int2voltstr (volts);    // convert the raw data into a scaled string
 
  mySerial.write(128);                // put the cursor on the top line
  mySerial.print(" Volts = ");
  mySerial.print(strVolts);           // print Volts = xx.xx
  delay(200);                         // let the display catch up and do it again
 
}


Go Up