Go Down

Topic: combining content of two character arrays (Read 2366 times) previous topic - next topic



I'm developing a sketch to randomly select from a pool of messages and then send it as a tweet.

I've the basics all working (ethernet shield, saving messages to PROGMEM, randomly retrieving one of them, tweeting it), but in order to avoid Twitter rejecting duplicate messages I'd like to add an incrementor to the beginning of the message that gets tweeted.

I've managed to get the incrementor incrementing and then put the resulting int into a character array - which then gets successfully tweeted - but I'm struggling to get the bit where I need to add the message into the tweet content working.

Here's a stripped down version of the code I'm currently arrived at:

Code: [Select]

#include <SPI.h> // needed in Arduino 0019 or later
#include <Ethernet.h>
#include <Twitter.h>
#include <avr/pgmspace.h>
#include <SoftwareSerial.h>

int copiercount = 0;
//[message contents saved to PROGMEM here]
//[ lookup table here]

char buffer[140];    // make sure this is large enough for the largest string it must hold

//[ethernet settings etc]

// Message to post
char msg[145] ;

void setup()
//[communications set up etc ]

   void sendtweet(){
// [code to send tweet here  ]

void loop()
    copiercount = copiercount + 1;
    Serial.println("Select from copier pool ");

    strcpy_P(buffer, (char*)pgm_read_word(&(copier_table[randNumber]))); // Necessary casts and dereferencing, just copy.
    Serial.println( buffer );
Serial.print("copier count: ");
Serial.println( copiercount );

String stringOne = "> ";
stringOne += copiercount;
char counter[5];
stringOne.toCharArray(counter, 5) ;

    msg[0] = counter[0];
    msg[1] = counter[1];
    msg[2] = counter[2];
    msg[3] = counter[3];
    msg[4] = counter[4];

    for (int j = 5; j < 143; j++){
    msg[j] = buffer[j-5];}
    Serial.println( msg );

} //end loop

This is outputting a series of tweets with the content "> 1", "> 2" etc, but not adding in the main body of the message afterwards.

I'd be grateful if someone could explain to me where I'm going wrong and/or the correct way to combine the two segments of the tweet content. My programming knowledge is limited, so please don't assume much in the way of prior knowledge!



First, avoid the String class like the plague.

You are doing things there that, while fine on a computer with plenty of memory, on a microcontroller are the spawn of satan and will cause untold problems with memory fragmentation.

You should be using char arrays for all your text.

Then, investigate the "sprintf()" function - that will allow you to format a complete message, including integer inclusions, etc, into a char array.


A belated thanks for your reply, majenko.

sprintf() investigated and I think I've got a feel for how it works now.
...unfortunately I'm now in a different city to the Ethernet Shield, but hopefully I'll get a chance to rework and test the code soon!


Go Up