Pages: [1]   Go Down
Author Topic: Configuring serLCD backpack for 20x4 LCD  (Read 1405 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I purchased a serLCD backpack from sparkfun (http://www.sparkfun.com/datasheets/LCD/SerLCD_V2_5.PDF), I soldered it to my LCD and it appears to default to 16 characters wide, not 20.  I cannot figure out how to program it to wrap after 20.  Would love some assistance.  The code below is for two optical sensors, each measures the velocity of a falling object in two different places in it's decent.  The velocity is to be displayed on the LCD screen.

Code:
#include <SoftwareSerial.h>

#define txPin 13

SoftwareSerial LCD = SoftwareSerial(0, txPin);
// since the LCD does not send data back to the Arduino, we should only define the txPin
const int LCDdelay=10;  // conservative, 2 actually works

boolean gate_hit = false; //gate is not hit
unsigned long time_1;
unsigned long time_2;
unsigned long time_3;
unsigned long time_4;
boolean time_1_stored = false; // time_1 is not yet stored
boolean time_2_stored = false; // time_2 is not yet stored
boolean time_3_stored = false; // time_2 is not yet stored
boolean time_4_stored = false; // time_2 is not yet stored

// wbp: goto with row & column
void lcdPosition(int row, int col) {
  LCD.write(0xFE);   //command flag
  LCD.write((col + row*64 + 128));    //position
  delay(LCDdelay);
}
void clearLCD(){
  LCD.write(0xFE);   //command flag
  LCD.write(0x01);   //clear command.
  delay(LCDdelay);
}
void backlightOn() {  //turns on the backlight
  LCD.write(0x7C);   //command flag for backlight stuff
  LCD.write(157);    //light level.
  delay(LCDdelay);
}
/*void backlightOff(){  //turns off the backlight
  LCD.write(0x7C);   //command flag for backlight stuff
  LCD.write(128);     //light level for off.
   delay(LCDdelay);
}*/
void serCommand(){   //a general function to call the command flag for issuing all other commands   
  LCD.write(0xFE);
}

void setup()
{
  int sensor_pin1 = 1; // yellow jumper connected to interrupt 1 on pin 3
  int sensor_pin0 = 0;
  pinMode(txPin, OUTPUT);
  LCD.begin(9600);
  clearLCD();
  //lcdPosition(0,0);
  LCD.print("Exit velocity m/s");
  attachInterrupt(sensor_pin1, gate_handler1, CHANGE); //pin=sensor_pin, function=gate_handler, state=CHANGE
  attachInterrupt(sensor_pin0, gate_handler0, CHANGE); //pin=sensor_pin, function=gate_handler, state=CHANGE
}
 
void gate_handler1 () //activates when interrupt pin 1 is changes state. aka, sensor is blocked or released.
{
  unsigned long time;
  time = millis(); // start to record time
  if (!gate_hit) // if gate is hit...
  {
      time_1 = time; // record time
      time_1_stored = true;
  }
      else    // if gate is released...
  {
      time_2 = time; // record time
      time_2_stored = true;
  }
  gate_hit = !gate_hit;  // resets gate_hit
}
void gate_handler0 () //activates when interrupt pin 1 is changes state. aka, sensor is blocked or released.
{
  unsigned long time;
  time = millis(); // start to record time
  if (!gate_hit) // if gate is hit...
  {
      time_3 = time; // record time
      time_3_stored = true;
  }
      else    // if gate is released...
  {
      time_4 = time; // record time
      time_4_stored = true;
  }
  gate_hit = !gate_hit;  // resets gate_hit
}

void loop ()
{
  //int DEBUG = true; 
  if (time_1_stored && time_2_stored)
  {
    long sensorDiff = 0;
    sensorDiff = time_2 - time_1; // measures the time druing between hit and release
    // set the cursor to column 0, line 1
    // (note: line 1 is the second row, since counting begins with 0):
    lcdPosition(0,21);
    // print the number of seconds since reset:
    LCD.print(2438.4/sensorDiff);
     
     /*Serial.print(time_1);
     Serial.print("  ");
     Serial.print(time_2);
     Serial.print("  ");
     Serial.println(sensorDiff);*/
     time_1_stored = false;  // resets time_1 and _2 back to false
     time_2_stored = false;
  }
  if ( time_3_stored && time_4_stored)
  {
    long sensorDiff = 0;
    sensorDiff = time_4 - time_3; // measures the time druing between hit and release
    // set the cursor to column 0, line 1
    // (note: line 1 is the second row, since counting begins with 0):
    lcdPosition(0,27);
    // print the number of seconds since reset:
    LCD.print(2438.4/sensorDiff);
     
     /*Serial.print(time_1);
     Serial.print("  ");
     Serial.print(time_2);
     Serial.print("  ");
     Serial.println(sensorDiff);*/
     time_3_stored = false;  // resets time_1 and _2 back to false
     time_4_stored = false;
  }

 }
 

Thank you!
Logged

Western New York, USA
Offline Offline
Faraday Member
**
Karma: 32
Posts: 4255
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I purchased a serLCD backpack from sparkfun (http://www.sparkfun.com/datasheets/LCD/SerLCD_V2_5.PDF), I soldered it to my LCD and it appears to default to 16 characters wide, not 20.


Check out the information in section 3.2 of your datasheet.  It covers your situation exactly.


Don
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes Don, perhaps I'm just having a dumb-blond couple of days, but I have read that many times and cannot figure out where to put the code or how to write it.  It specifically says "transmit the special command - 124 (0x7c). Follow this command with 3, 4, 5, or 6.  These commands set the LCD character width and number of lines.  These settings are used to correctly wrap the cursor to keep it within the view-able screen."  Sounds great!  Now how do I implement this new knowledge?

My problems with this:
1. Should I not include both 3 (for 20 characters wide) AND 5 (for 4 lines), not one or the other?
2. Where do I put this "special character 125 (0x7c)" and the following 3, 4, 5, or 6?

Thanks for your help.
Logged

Western New York, USA
Offline Offline
Faraday Member
**
Karma: 32
Posts: 4255
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Now how do I implement this new knowledge?
Well, I don't have one of those displays so I can only guess.

You have a bunch of functions like this one in your posted sketch. 
Code:
void backlightOn() {  //turns on the backlight
  LCD.write(0x7C);   //command flag for backlight stuff
  LCD.write(157);    //light level.
  delay(LCDdelay);
}
What you are trying to do looks correct to me but I don't know if the syntax is correct.  If they work then I would use the same technique.  If they don't work then you need someone who is fluent in 'C', which isn't me. 

I think you need two separate commands, one for the number of characters and another for the number of lines.

Code:
  LCD.write(0x7C);   //command flag
  LCD.write(3);      //20 characters

  LCD.write(0x7C);   //command flag
  LCD.write(5);      //4 lines

Don
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

While trouble-shooting the (0x7C) command to configure for 20x4, I got this error this morning:
avrdude: stk500_getsync(): not in sync: resp=0x00

Unplugged and plugged USB - error was gone!  But now nothing will show up on the LCD, not even the "Hello world" sample code from the playground, which was the first code I uploaded when I connected the serLCD backpack to the display, and it had, as well as my code, worked perfectly up until this point!

Could it be because I uploaded my code while the txPin was plugged in?  I am using software serial though...  This is what they say in the arduino playground for serLCD:

"As you can see I was just sending through the arduino's TX pin. This works fine in practice, but you may want to be sure to unplug it during code uploading, otherwise the LCD gets spammed with data, and it could potentially mess with it's configuration. (for instance, if it receives a 0x7C followed by some random integer.) Using the SoftwareSerial library is one way to avoid that..." 
- http://www.arduino.cc/playground/Learning/SparkFunSerLCD
Logged

Western New York, USA
Offline Offline
Faraday Member
**
Karma: 32
Posts: 4255
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You shouldn't have messed up the configuration if you used software serial when you uploaded stuff via the hardware serial port. 

In any case there is a way to regain control, it is spelled out in the very last paragraph on page 3 of your data sheet.  Their technique is designed for when you are communicating from a terminal (or from a PC running a terminal emulator program) connected directly to the LCD module.  You may have to write a short sketch to send the "<control>r" command.  If you are unfamiliar with ASCII this is 18 decimal or 12 hex.


Don
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you share your code that made the serLCD non-responsive? I hope you didn't include the LCD command in a loop and run it too many times, did you?
Logged


Pages: [1]   Go Up
Jump to: