Hello,
First, please note that I have Googled this, and it is through Google and reading others' posts on the matter that i have managed to get this far as I am very new to programming & Arduino.
I am trying to write a small program that receives an RSS and displays it on a 16x2 screen using Arduino Uno. I have successfully achieved this step. This result split a word over 2 both lines if needed.
I then want it to print each 'word' rather than each character so that if there were not enough characters left on the line to display the word in full, then it would print the word on the next line, (0,1)
I have 90% succeeded in this but it is just not quite right -words still seems to run off a little. If the cursor is set to line 2 and there is run off then i would like a small pause, for the screen to clear and then print on a fresh screen at position (0,0)
Also, after each feed I want the lcd to clear and reset to (0,0) but this is proving problematic too (so I have removed that all together from the code... one problem at a time!!!)
Could someone please take a look at the code below and tell me where I have gone wrong.
Thank you
C++ Code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const byte numChars = 32;
char receivedChars[numChars]; // as array to store the recevied characters
boolean newData = false;
const int initialMarker = 1;
boolean line1 = true;
boolean line2 = false;
int charRemaining = 16;
void setup(){
delay (2000);
Serial.begin(9600);
lcd.begin(16,2);
lcd.setCursor(0,0);
pinMode(13, OUTPUT);
pinMode(10, OUTPUT);
Serial.println(initialMarker); // print startMarker once
}
void loop(){
recvWithEndMarker();
}
void newFeedCheck(char incomingByte){
if (incomingByte == '>') {
lcd.clear();
lcd.setCursor(0,0);
charRemaining = 16;
}
}
void recvWithEndMarker(){
static byte ndx = 0;
char endMarker = '~';
char rc;
while (Serial.available() > 0 && newData == false){
rc = Serial.read();
if (rc != endMarker) {
if ((rc != '<') && (rc != '>')) {
receivedChars[ndx] = rc;
ndx++;
}
if (ndx >= numChars){
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminates the string
ndx = 0;
newData = true;
}
}
workOutLines();
}
int strLength(char*buffer){
int i = 0, length = 0;
while (buffer != '\0'){
if (buffer[i+1] == '\0'){
length = i+1;
return length;
}
i++;
}
}
void workOutLines(){
int wordSize;
if (newData == true){
wordSize = strLength(receivedChars);
newData = false;
showData(wordSize);
}
}
void showData(int receivedWordSize){
if (charRemaining < 0 ) {
if (line1 == true && line2 == false) {
line1 = false;
line2 = true;
charRemaining = 16;
lcd.setCursor(0,1);
lcd.print (receivedChars);
lcd.print (" ");
charRemaining = charRemaining-receivedWordSize;
charRemaining --;
}
else if (line1 == false && line2 == true) {
line1 = true;
line2 = false;
charRemaining = 16;
lcd.clear ();
lcd.setCursor(0,0);
lcd.print (receivedChars);
lcd.print (" ");
charRemaining = charRemaining-receivedWordSize;
charRemaining --;
}
}
else{
lcd.print (receivedChars);
lcd.print (" ");
charRemaining = charRemaining-receivedWordSize;
charRemaining --;
}
}
Python RSS Script
#
# from http://www.pythonforbeginners.com/feedparser/using-feedparser-in-python
# installed feedparser with `pip`, after installing pip with apt-get
#
import serial
import feedparser
import time
ser = serial.Serial("\\.\COM3", 9600)
i = 1
y = 0
time.sleep(3)
while i == 1:
y = int(ser.read())
while y == 1:
sportFeed = feedparser.parse('http://feeds.bbci.co.uk/sport/0/football/rss.xml')
posts = []
for z in range(0,len(sportFeed)):
topic = sportFeed['entries'][z].title.encode('utf-8')
newTopic = topic.replace(" ","~")
print newTopic
ser.write (str("<"))
ser.write (str(newTopic))
ser.write (str('~>'))
time.sleep(6)
time.sleep(30)