Hello,
I am working on a little project that takes tweets and displays them on an LCD (currently 16x2), using an Arduino Diecimilia (ATmega168) and the LiquidCrystal library. I've got it to the point where the first time I send a tweet the message displays, but any subsequent attempts fail until the next time I start the application. The error appears to be on the Arduino side. I believe it is happening in my heldTextToLCD() method.
Here is my Arduino code:
#include <LiquidCrystal.h>
#include <WString.h>
#define CHARACTERSPERLINE 16
#define LCDLINES 2
#define MAXLINES 10
String heldText = String(CHARACTERSPERLINE*MAXLINES);
int received = 0;
LiquidCrystal lcd(7,8,9,10,11,12);
int buzzerPin = 3;
void setup()
{
// start serial port at 9600 bps:
Serial.begin(9600);
//pinMode(2, INPUT); // digital sensor is on digital pin 2
lcd.begin(16,2);
//lcd.autoscroll();
establishContact(); // send a byte to establish contact until receiver responds
delay(1000);
//heldText = "returns a new string that is a part of the original string. When using the endIndex parameter, the string between beginIndex and endIndex -1 is returned.";
//heldTextToLCD();
}
void loop()
{
// if we get a valid byte, read analog ins:
if (Serial.available() > 0)
{
// get incoming byte:
handleIncomingChars();
// delay 10ms to let the ADC recover:
delay(10);
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.println("0,0,0"); // send an initial string
delay(300);
}
}
void handleIncomingChars() {
// read the incoming data as a char:
char inChar = Serial.read();
if(inChar=='\n' || inChar == '\r')
{
lcd.clear();
lcd.home();
heldTextToLCD();
heldText= String(CHARACTERSPERLINE*MAXLINES);
}
else
{
// if you're not at the end of the string, append
// the incoming character:
if (heldText.length() < (CHARACTERSPERLINE*MAXLINES))
{
heldText.append(inChar);
}
else
{
// empty the string by setting it equal to the inoming char:
heldText = inChar;
}
}
}
void heldTextToLCD()
{
Serial.println(heldText);
int numLines = heldText.length() / CHARACTERSPERLINE;
//String lastFragment = "";
int remainingLength = heldText.length();
lcd.clear();
int currentIndex = 0;
int i=0;
while(remainingLength>0 || i > MAXLINES)
{
if(i%LCDLINES==0)
{
lcd.clear();
}
else
{
lcd.setCursor(0,i%LCDLINES);
}
String line = String(32);//lastFragment;
int lengthToTake = CHARACTERSPERLINE;
if(remainingLength < CHARACTERSPERLINE)
{
lengthToTake = remainingLength;
}
line.append(heldText.substring(currentIndex,currentIndex+lengthToTake));
delay(10);
currentIndex += lengthToTake;
delay(100);
lcd.print(line);
delay(2000);
i++;
remainingLength = heldText.length() - currentIndex;
}
Serial.println("done");
}
Here is my processing code (I am working in Eclipse):
import processing.core.PApplet;
import processing.serial.Serial;
import twitter.TwitterEngine;
import twitter4j.Tweet;
public class TwitterLCD extends PApplet
{
private static final long serialVersionUID = 1L;
private TwitterEngine twitter;
private Serial port;
private int stepper= 0;
public void setup()
{
size(500,500);
twitter = new TwitterEngine();
twitter.query("@hapticdata");
println(Serial.list());
port = new Serial(this,Serial.list()[1],9600);
port.bufferUntil('\n');
}
public void draw()
{
}
public void serialEvent(Serial myPort)
{
String receivedString = myPort.readStringUntil('\n');
println("received: "+receivedString);
}
public void mousePressed()
{
Tweet t = twitter.getTweet(stepper);
stepper++;
String tweetText = t.getText();
char[] characters = tweetText.toCharArray();
for(int i=0;i<characters.length;i++)
{
port.write(characters[i]);
delay(10);
}
port.write('\r');
}
}
I was wondering if maybe it has to do with memory management? I really don't know anything about garbage collection or releasing variables in C.
I really appreciate any help you can offer, thank you!