An Arduino RSS Feed Display by fritterdonut
Featured

Step 4: The Arduino Code

// This code is for the Arduino RSS feed project, by Fritter
// Read the comment lines to figure out how it works

int startstring = 0;     // recognition of beginning of new string
int charcount = 0;     // keeps track of total chars on screen
       
#include  <LiquidCrystal.h>  // import the LiquidCrystal Library
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
         Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
         lcd.begin(16,2);            // Initialize the LCD size 16x2. Change if using a larger LCD
         lcd.setCursor(0,0);     // Set cursor position to top left corner
         pinMode(13, OUTPUT);
}

void loop() {
        char incomingByte = 0;   // for incoming serial data
       
         if (Serial.available() > 0) {        // Check for incoming Serial Data
                 digitalWrite(13, HIGH);
                 incomingByte = Serial.read();
                 if ((incomingByte == '~') && (startstring == 1)){            // Check for the closing '~' to end the printing of serial data      
                   startstring = 0;                                                                  // Set the printing to off
                   delay(5000);                                                                     // Wait 5 seconds
                   lcd.clear();                                                                         // Wipe the screen
                   charcount = 0;                                                                   // reset the character count to 0
                   lcd.setCursor(0,0);                                                          // reset the cursor to 0,0
                 }
                 if (startstring == 1){                                                             // check if the string has begun if first '~' has been read
                   if (charcount <= 30){                                                        // check if charcount is under or equal to 30
                     lcd.print(incomingByte);                                                // Print the current byte in the serial
                     charcount = charcount++;                                             // Increment the charcount by 1 yes I know it's awkward
                     }
                   }
                   if (charcount == 31){                                                         // if the charcount is equal to 31 aka the screen is full
                     delay(500);
                     lcd.clear();                                                                        // clear the screen
                     lcd.setCursor(0,0);                                                         // set cursor to 0,0
                     lcd.print(incomingByte);                                                // continue printing data
                     charcount = 1;                                                                 // set charcount back to 1
                   }
                  
                 if (incomingByte == '~'){                                                    // Check if byte is marker ~ to start the printing
                  
                   startstring = 1;                                                                  // start printing
                 }
         }
                 digitalWrite(13, LOW);
                 delay(10);                                                                            // 10ms delay for stability
             } 
 
Remove these adsRemove these ads by Signing Up
brndnlstr says: Jul 29, 2012. 10:11 AM
I could be wrong, but charcount=charcount++; won't do anything.

If you want to increment, you only need this:

charcount++;

That will increment by one.
fritterdonut (author) says: Jul 29, 2012. 10:05 PM
It will in fact add 1 to the total, but it is redundant.

the ++ modifier essentially acts as variable = variable + 1

So in this case it says variable = variable = variable + 1

It works, but should simply say charcount++ for the sake of simplicity and easier reading. Thanks for pointing it out.
clgonsal says: Jul 30, 2012. 9:19 AM
Technically, the behavior is "undefined" which means it might do what you want, or it might not. The actual behavior is up to your C compiler, and *any* behavior is permitted, including not compiling the code, or producing code with wildly unexpected behavior.

In case you care: the reason it's undefined is that the C language spec says modifying the same variable multiple times within the same expression without an intervening "sequence point" is undefined. There are no sequence points in that expression, and charcount is being modified twice. This exact topic used to be a common question on the C programming groups on USENET. About once a week someone would ask why one compiler would behave differently than another when compiling "i = i++" (often the question was "which one is right?").

Here's a Stack Overflow question on this topic: http://stackoverflow.com/questions/949433/could-anyone-explain-these-undefined-behaviors-i-i-i-i-i-etc
fritterdonut (author) says: Jul 31, 2012. 2:26 PM
Thanks for the definitive answer. So while I can confirm it would work properly for me, it might not work properly if you use a different compiler. Really is playing a game of chance with your code, I guess.
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!