Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Processing Code:
- //import all the twitter libraries
- import twitter4j.conf.*;
- import twitter4j.*;
- import twitter4j.auth.*;
- import twitter4j.api.*;
- import java.util.*;
- import processing.serial.*;
- //make a twitter object
- Twitter twitter;
- //lines from our text file
- String [] lines;
- Serial myPort;
- String val = null;
- //our search string
- String searchString = "#group22Yes";
- String searchString2 = "#group22No";
- //a list of type status from the API
- List<Status> tweets;
- List<Status> noTweets;
- int currentTweet;
- int listSize;
- int listSizeNo;
- void setup()
- {
- size(800,200);
- String portName = Serial.list()[1];
- String portName2 = Serial.list()[0];
- myPort = new Serial(this, portName, 9600);
- //setup an array to hold every line of the text file
- lines = loadStrings("secrets.txt");
- //sets up each line of the text file to the corresponding key
- ConfigurationBuilder cb = new ConfigurationBuilder();
- cb.setOAuthConsumerKey(lines[0]);
- cb.setOAuthConsumerSecret(lines[1]);
- cb.setOAuthAccessToken(lines[2]);
- cb.setOAuthAccessTokenSecret(lines[3]);
- TwitterFactory factory = new TwitterFactory(cb.build());
- twitter = factory.getInstance();
- }
- void draw()
- {
- getYesTweet();
- getNoTweet();
- calculateAngle();
- delay(60000);
- }
- void tweet(String val)
- {
- try
- {
- Status status = twitter.updateStatus(val + hour() + ":" + minute() +":" + second() + " via Arduino");
- System.out.println("Status updated to [" + status.getText() + "].");
- }
- catch (TwitterException te)
- {
- System.out.println("Error: "+ te.getMessage());
- }
- }
- //gets the new tweets
- void getYesTweet()
- {
- //try the search
- try
- {
- Query query = new Query(searchString);
- query.setCount(100);
- QueryResult result = twitter.search(query);
- tweets = result.getTweets();
- listSize = tweets.size();
- System.out.println("No of " + searchString + ": " + listSize);
- }
- //if there is an error then catch it and print it out
- catch (TwitterException te)
- {
- System.out.println("Failed to search tweets: " + te.getMessage());
- System.exit(-1);
- }
- }
- //gets the new tweets
- void getNoTweet()
- {
- //try the search
- try
- {
- Query query = new Query(searchString2);
- query.setCount(100);
- QueryResult result = twitter.search(query);
- noTweets = result.getTweets();
- listSizeNo = noTweets.size();
- System.out.println("No of " + searchString2 + ": " + listSizeNo);
- }
- //if there is an error then catch it and print it out
- catch (TwitterException te)
- {
- System.out.println("Failed to search tweets: " + te.getMessage());
- System.exit(-1);
- }
- }
- void calculateAngle()
- {
- float total;
- float angle;
- total = listSize + listSizeNo;
- angle = (listSizeNo / total) * 180;
- System.out.println(Math.round(angle));
- }
- Arduino code:
- #include <Servo.h>
- Servo myservo;
- const int tweetButtonPin = 2; // the number of the pushbutton pin
- const int searchButtonPin = 4; // the number of the pushbutton pin
- const int ledPin = 13; // the number of the LED pin
- int counter = 0;
- int ledState = HIGH;
- int tweetButtonState;
- int lastTweetButtonState = LOW;
- int searchButtonState;
- int lastSearchButtonState = LOW;
- // the following variables are long's because the time, measured in miliseconds,
- // will quickly become a bigger number than can be stored in an int.
- long lastTweetDebounceTime = 0;
- long tweetDebounceDelay = 50;
- long lastSearchDebounceTime = 0;
- long searchDebounceDelay = 50;
- int pos = 0;
- void setup()
- {
- Serial.begin(9600);
- myservo.attach(9);
- }
- void loop()
- {
- // read the state of the switch into a local variable:
- int tweetButtonReading = digitalRead(tweetButtonPin);
- int searchButtonReading = digitalRead(searchButtonPin);
- // check to see if you just pressed the button
- if (tweetButtonReading != lastTweetButtonState)
- {
- // reset the debouncing timer
- lastTweetDebounceTime = millis();
- }
- if (searchButtonReading != lastSearchButtonState)
- {
- // reset the debouncing timer
- lastSearchDebounceTime = millis();
- }
- if ((millis() - lastTweetDebounceTime) > tweetDebounceDelay)
- {
- // whatever the tweetButtonReading is at, it's been there for longer
- // than the debounce delay, so take it as the actual current state:
- // if the button state has changed:
- if (tweetButtonReading != tweetButtonState)
- {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement