All together now - JuliaElizabethLittle/peeMarksmanship GitHub Wiki
So now the time to put everything together has arrived: we'll combine Score, Highscore and LCD with Twitter.
As you may have seen from the Twitter code it was a bit hectic so we also had to clean it up a bit. We first simply put it all together, the setup with the setup, the loop with the loop and the stuff outside outside, but it was all a bit of a mess, because just to send a tweet to the account there was a ton of code. So we look it all and placed it out of the loop in a special function created just for it, like this:
void sendTweet(String tweetText){
TembooChoreo StatusesUpdateChoreo;
// invoke the Temboo client
// NOTE that the client must be reinvoked, and repopulated with
// appropriate arguments, each time its run() method is called.
StatusesUpdateChoreo.begin();
// set Temboo account credentials
StatusesUpdateChoreo.setAccountName(TEMBOO_ACCOUNT);
StatusesUpdateChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
StatusesUpdateChoreo.setAppKey(TEMBOO_APP_KEY);
// identify the Temboo Library choreo to run (Twitter > Tweets > StatusesUpdate)
StatusesUpdateChoreo.setChoreo("/Library/Twitter/Tweets/StatusesUpdate");
// set the required choreo inputs
// see https://www.temboo.com/library/Library/Twitter/Tweets/StatusesUpdate/
// for complete details about the inputs for this Choreo
// add the Twitter account information
StatusesUpdateChoreo.addInput("AccessToken", TWITTER_ACCESS_TOKEN);
StatusesUpdateChoreo.addInput("AccessTokenSecret", TWITTER_ACCESS_TOKEN_SECRET);
StatusesUpdateChoreo.addInput("ConsumerKey", TWITTER_API_KEY);
StatusesUpdateChoreo.addInput("ConsumerSecret", TWITTER_API_SECRET);
// and the tweet we want to send
StatusesUpdateChoreo.addInput("StatusUpdate", tweetText);
// tell the Process to run and wait for the results. The
// return code (returnCode) will tell us whether the Temboo client
// was able to send our request to the Temboo servers
unsigned int returnCode = StatusesUpdateChoreo.run();
// a return code of zero (0) means everything worked
if (returnCode == 0) {
Serial.println("Success! Tweet sent!");
} else {
// a non-zero return code means there was an error
// read and print the error message
while (StatusesUpdateChoreo.available()) {
char c = StatusesUpdateChoreo.read();
Serial.print(c);
}
}
StatusesUpdateChoreo.close();
}
Once That was out of the way, our code looked much neater!
void setup() {
lcd.begin(16, 2);
pinMode(button, INPUT_PULLUP);
Serial.begin(9600);
delay(4000);
//while(!Serial);
Bridge.begin();
}
void loop() {
sensors[0] = map(analogRead(A0), 0, 1023, 0, 10);
score = sensors[0] * 20;
sensors[1] = map(analogRead (A1), 0, 1023, 0, 10);
score = score + sensors[1] * 10;
sensors[2] = map(analogRead (A2), 0, 1023, 0, 10);
score = score + sensors[2] * 5;
chain = digitalRead(button);
if (chain == LOW) {
counter = 0;
lcd.clear();
lcd.print(score);
if(highscore<score){
highscore = score;
Serial.println("Running SendATweet");
// define the text of the tweet we want to send
String tweetText("There's a new highscore: " + String(highscore));
sendTweet(tweetText);
}
delay(1000);
}
//Every five seconds it changes the screen from current score to highscore
if (score==lastscore){
counter+=1;
if (counter>=50) {
lcd.clear();
lcd.print("highscore: ");
lcd.print(highscore);
}
}
lastscore=score;
delay(100);
}
As we can see where you define the text of the tweet you want to send we call the function String tweetText
and it will directly run the whole thing from the top.