Go Down

Topic: Loops and Strings (Read 1 time) previous topic - next topic

zach0731

I am using a loop and have a string to save the text and check it against an xml file the next time the loop runs, but the string keeps reseting every time the loop runs. How can I keep it from removing the value it has stored?

Code: [Select]

void loop()
   

if (client.connected()) {
     // get the last tweet by simply parsing the item and title tags
     if((finder.find("<item>")&&(finder.getString("<title>","</title>",tweetp,140)!=0)))
    {
     
  String twenew(tweetp);
  Serial.println("tweetp");
  Serial.println(tweetp);
  Serial.println("twenew");
  Serial.println(twenew);
  Serial.println("tweold");
  Serial.println(tweold); 
     
  if (twenew != tweold){
       Serial.println(tweold);
       Serial.println("Favorite coming");
       Serial.println(twenew);
          delay(100);         
          Thermal.print(tweetp);
          delay(800);
        Thermal.print(10, BYTE);
        Thermal.print(10, BYTE);
      String tweold = twenew;
      Serial.println("old");
       Serial.println(tweold);
         delay(500);
       }
     else{

delay(500);


         
     }


         
     
  delay(6000); // delay 0.7 second
                        }}}

zach0731

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetDHCP.h>
#include <TextFinder.h>
#include <SoftwareSerial.h>
#include <EthernetDNS.h>
#include <Twitter.h>
#include <NewSoftSerial.h>
#include "SparkSoftLCD.h"

  // LCD transmit pin
#define LCD_TX 3

SparkSoftLCD lcd = SparkSoftLCD(LCD_TX);

NewSoftSerial Thermal(4, 5); //Soft RX from printer on D2, soft TX out to printer on D3

#define FALSE  0
#define TRUE  1
int printOnBlack = FALSE;
int printUpSideDown = FALSE;

int ledPin = 13;
int heatTime = 255; //80 is default from page 23 of datasheet. Controls speed of printing and darkness
int heatInterval = 255; //2 is default from page 23 of datasheet. Controls speed of printing and darkness
char printDensity = 15; //Not sure what the defaut is. Testing shows the max helps darken text. From page 23.
char printBreakTime = 15; //Not sure what the defaut is. Testing shows the max helps darken text. From page 23.



//Setup of pins as digital outputs (in this case with LED's attached to them, but intention is to use them to control things with)

#define LED3Pin 6
#define LED4Pin 7
#define DelayFlashPin 9  //this pin flashes while arduino is waiting 1 minute before looking for next tweet

//byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };//default ethernet shield MAC
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x20, 0x3B };//my particular ethernet shield MAC (this number is WRITTEN on a label on the Ethernet Shield)
//byte server[] = {128,242,240,20}; // Twitter...I found this did NOT work so googled to find current Twitter server IP
byte server[] = {199,59,148,10}; // Twitter server IP address (checked 29/3/11)

char myStringBuffer[80];

char tweet[140];

char tweetp[140];


char tweetpo[140];
const String tweold;
String twenew;


Client client(server, 80);

TextFinder  finder( client );


char line1[] = "    01234567890ABCDE";
#define line1Length (sizeof(line1)-2)

int x = 15; // for scroll
int y = 0; // for scroll



//SETUP SOFTWARE SERIAL FOR LCD DISPLAY
#define rxlcdprintPin 2  //sets new software rx as pin2, from LCD serial-LCD display usnit
#define txlcdprintPin 8 //sets new software tx pin as pin 3 to LCD serial-LCD display usnit
SoftwareSerial lcdSerial = SoftwareSerial (rxlcdprintPin, txlcdprintPin); //define lcdSerial as rxlcdprintPin (2) and txlcdprintPin (3)
//END OF SOFTWARE SERIAL LCD DISPLAY SETUP

void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(LCD_TX, OUTPUT);
lcd.begin(9600);
lcd.clear();

  Serial.begin(9600); //Use hardware serial for debugging
  Thermal.begin(19200); //Setup soft serial for ThermalPrinter control

  printOnBlack = FALSE;
  printUpSideDown = FALSE;

  //Modify the print speed and heat
  Thermal.print(27, BYTE);
  Thermal.print(55, BYTE);
  Thermal.print(7, BYTE); //Default 64 dots = 8*('7'+1)
  Thermal.print(heatTime, BYTE); //Default 80 or 800us
  Thermal.print(heatInterval, BYTE); //Default 2 or 20us

  //Modify the print density and timeout
  Thermal.print(18, BYTE);
  Thermal.print(35, BYTE);
  int printSetting = (printDensity<<4) | printBreakTime;
  Thermal.print(printSetting, BYTE); //Combination of printDensity and printBreakTime

  Serial.println();
  Serial.println("Parameters set");
 
  Serial.begin(9600);
  delay(100);
 
      //FOR LCD DISPLAY
  pinMode (rxlcdprintPin, INPUT);
  pinMode (txlcdprintPin, OUTPUT);
  lcdSerial.begin(9600);// LCD serial out
  //end of LCD setup
 
 
   //For digital output pins (with LEDS on them, pins 4 5 6 7, also 13 which flashes as the one minute counter counts down)

pinMode(LED3Pin,OUTPUT);
pinMode(LED4Pin,OUTPUT);
pinMode(DelayFlashPin,OUTPUT);
 
  Serial.println("Twitter2LCD");
  delay(50);
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
lcdSerial.print(19, BYTE); //backlight on
delay(50);
lcdSerial.print(12, BYTE); //clears screen
delay(50);
lcdSerial.print("Twitter to LCD"); //lcd text
lcdSerial.print(13, BYTE); //newline
lcdSerial.print("and 4 control LED's"); //lcd text
delay(50);
lcdSerial.print(13, BYTE); //newline
lcdSerial.print("J.Dingley   V1.1"); //lcd text
//lcdSerial.print(13, BYTE); //newline
delay(500);
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX



EthernetDHCP.begin(mac);
}


void loop()
{
  //lcd.clear();
  if (client.connect()) {

    //XXXXXXXXXXXXXXX CHANGE THE 9 DIGITS IN STATEMENT BELOW TO THOSE OF YOUR OWN TWITTER RSS feed xxxxxxxxxxxxxxxxxxxxxxx
    //XXXXXXXXXXXXXXX My instructable tells you how to get your twitter RSS feed number XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    client.println("GET http://www.twitter.com/statuses/user_timeline/20557892.rss HTTP/1.0");  // twitter rss for jddat2
    client.println();
  }
  else {
   
    Serial.println("Connection failed");
    delay(200);
//XXXXXXXXXXX Display XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
lcdSerial.print(12, BYTE); //clears screen
delay(50);
lcdSerial.print("Connection"); //lcd text
lcdSerial.print(13, BYTE); //newline
lcdSerial.print("FAILED"); //lcd text

delay(500);
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  }
  if (client.connected()) {
     // get the last tweet by simply parsing the item and title tags
     if((finder.find("<item>")&&(finder.getString("<title>wootoff: ","</title>",tweet,33)!=0)))
     { 
         Serial.println(tweet);
         delay(100);
         //XXXXXXXXXXXX Display XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
          lcdSerial.print(12, BYTE); //clears screen
          delay(200);
          lcd.print("tweet coming"); //lcd tweet text
         //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

delay(500);
         
         
     }
    else  {
      Serial.println("Could not find item field");
      delay(100);
     //XXXXXXXXXXXXX Display XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
          lcdSerial.print(12, BYTE); //clears screen
          delay(50);
          lcdSerial.print("Could not find"); //lcd text
          lcdSerial.print(13, BYTE); //newline
          lcdSerial.print("item field"); //lcd text
     //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 
           }   
  }
  else {
    Serial.println("Disconnected");
    delay(100);
    lcdSerial.print(12, BYTE); //clears screen
    delay(50);
    lcdSerial.print("Disconnected"); //lcd text
  }
  client.stop();
  //client.flush();
 
 
 
 






  //XXXXXXXXXXXXXXXXput text tweet onto LCD XXXXXXXXXXXXXXXXXXXXXXXXX
          lcd.clear(); //clears screen
          Serial.println("TEXT TWEET COMING");
          lcd.print("TEXT TWEET COMING"); //lcd tweet text
          delay(2000);
          lcd.clear();
          delay(200);
          Serial.print(tweet); //Serial viewer
          lcd.print(tweet);
       
         
         //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
         
         

if (client.connect()) {

    //XXXXXXXXXXXXXXX CHANGE THE 9 DIGITS IN STATEMENT BELOW TO THOSE OF YOUR OWN TWITTER RSS feed xxxxxxxxxxxxxxxxxxxxxxx
    //XXXXXXXXXXXXXXX My instructable tells you how to get your twitter RSS feed number XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    client.println("GET http://www.twitter.com/favorites/698823.rss HTTP/1.0");  // twitter rss for wootchatter 216522378
    client.println();
}
   

if (client.connected()) {
     // get the last tweet by simply parsing the item and title tags
     if((finder.find("<item>")&&(finder.getString("<title>","</title>",tweetp,140)!=0)))
    {
     
  String twenew(tweetp);
  Serial.println("tweetp");
  Serial.println(tweetp);
  Serial.println("twenew");
  Serial.println(twenew);
  Serial.println("tweold");
  Serial.println(tweold); 
     
  if (twenew != tweold){
       Serial.println(tweold);
       Serial.println("Favorite coming");
       Serial.println(twenew);
          delay(100);         
          Thermal.print(tweetp);
          delay(800);
        Thermal.print(10, BYTE);
        Thermal.print(10, BYTE);
      String tweold = twenew;
      Serial.println("old");
       Serial.println(tweold);
         delay(500);
       }
     else{

delay(500);


         
     }


         
     
  delay(6000); // delay 0.7 second
                        }}}


PaulS

Code: [Select]
  delay(6000); // delay 0.7 second
                        }}}

Making the comments match the code, when the code is not perfectly obvious, is usually a good idea.

Putting each { and } on it's own line, properly indenting the code between the { and the } makes the code much easier to read. Eliminating the huge amounts of white space means less scrolling, too.

Finally:
Quote
I am using a loop and have a string to save the text and check it against an xml file the next time the loop runs, but the string keeps reseting every time the loop runs. How can I keep it from removing the value it has stored?

Which string are you saving the text in? Giving us a clue WHICH variable is not retaining its value would make troubleshooting a lot easier.

johnwasser

The String objects 'twenew' and 'tweold' are declared inside loop().  When you hit the end of loop() they go 'out of scope' and are therefore destroyed.  The next time through loop() you create them again.

For them to persist across invocations of loop() you have to make them global or static:

Code: [Select]

String twenew, tweold;
void loop()
  {...}


or

Code: [Select]

void loop()
  {
  static String twenew, tweold;
  ...
  }
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Go Up