SoftwareSerial library with LCD

Hi,

I am using the SoftwareSerial library to send message over Serial to a 2X16 LCD display. In the below code, I send over 32 characters. The 1 - 16 characters go on the first line and 17-32 go on the 2nd line. This works fine when using the Arduino IDE serial monitor. I can send over server messages and the LCD is updated. My issue is when I send the data via usb using python or C# (.net or Raspberry Pi). The first 32 characters update the LCD, but then it stops updating. In both cases I am using while loops to send over the messages. Any help would be great

The bellow code is flashed on the an Atty84

/*
  LiquidCrystal Library - Serial Input 
*/
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>

#define rxPin 2 // receive
#define txPin 3 // transmit

SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(4, 5, 6, 7, 8, 9);
char lineOne[16];
char lineTwo[16];

int maxChar = 32;
char inLineData[32]; // Allocate some space for the string - only 32 chars / 16 allowed per line
char inLineChar=-1; // Where to store the character read


void setup(){
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
   // define pin modes for tx, rx, led pins:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  
  mySerial.begin(9600);
  mySerial.print("LCD Display...");
  lcd.print("Initializing...");
  delay(800);
  lcd.clear();
  lcd.print("Ready");
}

void loop()
{
  // when characters arrive over the serial port...
  if (mySerial.available()) {
    // wait a bit for the entire message to arrive
    delay(1000);      
    
    // read all the available characters
    byte index = 0;
    int readData;
    while (mySerial.available() > 0) {
      
      readData = mySerial.read(); // Read a character
                                
      inLineData[index] = readData; // Store it   
      // display each character to the LCD         
      if(index < 16){              
         lcd.setCursor(index, 0);   
         lcd.write(inLineData[index]);         
      }else{                             
          lcd.setCursor(index, 1);
          lcd.write(inLineData[index]);         
      }
       delay(100);
       
       if(index>15){
         
         delay(100);         
         lcd.setCursor(index - 16, 1);
         lcd.write(inLineData[index]);
       
       }   
     
      index++;
    }
    delay(2000);
    index = 0;
   
  } 
  
}

Example loop in python

while 1:
     // output serial message
sleep(2)

try setting your index back to 1 once it hits 32 to see if it will start back on line one. you might want to send a starting character and a ending character kinda like they do with rfid so that you know when to start a new line.

jasit.

are you sure the first character on line 2 is "position 17"? I was working with a pair of LCDs here recently, one is a 16x2 and the other iirc is a 20x4. I thought one or both had "dead space" off to the right of each line, that you had to account for when cursoring over, to get to the next line. (maybe it was the 16x2 was physically addressed as a 20x2, because it was using the same multiplexing as the bigger display, so you had to skip 4 characters to get to the next line? sorry I don't recall clearly)

Thanks. Setting the index back to 0 did the trick. I also re-wrote most of the code, making it simpler.

#include <LiquidCrystal.h>
#include <SoftwareSerial.h>

#define rxPin 2 // receive
#define txPin 3 // transmit

SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(4, 5, 6, 7, 8, 9);


void setup(){
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
   // define pin modes for tx, rx, led pins:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  
  mySerial.begin(9600);
  mySerial.print("LCD Display...");
  lcd.print("Initializing...");
  delay(800);
  lcd.clear();
  lcd.print("Ready");
}

void loop()
{
  // when characters arrive over the serial port...
  if (mySerial.available()) {
    // wait a bit for the entire message to arrive
    delay(100);
       
    // clear the screen
    lcd.clear();
    // read all the available characters
    int index = 0;
 
    while (mySerial.available() > 0) {

      // display each character to the LCD
      if(index < 16){
      
          lcd.setCursor(index, 0);   
      
      }else{      
         if(index > 34){
           index = 0;
           
           lcd.setCursor(index, 0);
         }else{
           lcd.setCursor(index-16, 1);
         } 
      }
     
      lcd.write(mySerial.read());
      delay(100);
      index++;
    }
    
   }
}

it would be nice if that didn't require six pins….

I am using an Attiny84 which leaves me 3 pins left. For what I am working on, this is great. If you want, you could buy an LCD with i2c interface backpack. Only uses 2 pins. http://arduino-info.wikispaces.com/LCD-Blue-I2C#v1