Advertisement
aReford

Untitled

Mar 21st, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. Processing Code:
  2.  
  3. //import all the twitter libraries
  4. import twitter4j.conf.*;
  5. import twitter4j.*;
  6. import twitter4j.auth.*;
  7. import twitter4j.api.*;
  8. import java.util.*;
  9. import processing.serial.*;
  10.  
  11.  
  12. //make a twitter object
  13. Twitter twitter;
  14.  
  15.  
  16.  
  17. //lines from our text file
  18. String [] lines;
  19.  
  20. Serial myPort;
  21. String val = null;
  22.  
  23. //our search string
  24. String searchString = "#group22Yes";
  25. String searchString2 = "#group22No";
  26.  
  27. //a list of type status from the API
  28. List<Status> tweets;
  29. List<Status> noTweets;
  30.  
  31.  
  32. int currentTweet;
  33.  
  34. int listSize;
  35. int listSizeNo;
  36.  
  37. void setup()
  38. {
  39.  
  40.  
  41. size(800,200);
  42. String portName = Serial.list()[1];
  43. String portName2 = Serial.list()[0];
  44. myPort = new Serial(this, portName, 9600);
  45. //setup an array to hold every line of the text file
  46. lines = loadStrings("secrets.txt");
  47.  
  48. //sets up each line of the text file to the corresponding key
  49. ConfigurationBuilder cb = new ConfigurationBuilder();
  50. cb.setOAuthConsumerKey(lines[0]);
  51. cb.setOAuthConsumerSecret(lines[1]);
  52. cb.setOAuthAccessToken(lines[2]);
  53. cb.setOAuthAccessTokenSecret(lines[3]);
  54.  
  55. TwitterFactory factory = new TwitterFactory(cb.build());
  56.  
  57. twitter = factory.getInstance();
  58. }
  59.  
  60. void draw()
  61. {
  62. getYesTweet();
  63. getNoTweet();
  64.  
  65. calculateAngle();
  66.  
  67. delay(60000);
  68. }
  69.  
  70.  
  71. void tweet(String val)
  72. {
  73. try
  74. {
  75. Status status = twitter.updateStatus(val + hour() + ":" + minute() +":" + second() + " via Arduino");
  76. System.out.println("Status updated to [" + status.getText() + "].");
  77. }
  78. catch (TwitterException te)
  79. {
  80. System.out.println("Error: "+ te.getMessage());
  81. }
  82. }
  83.  
  84. //gets the new tweets
  85. void getYesTweet()
  86. {
  87.  
  88. //try the search
  89. try
  90. {
  91. Query query = new Query(searchString);
  92. query.setCount(100);
  93. QueryResult result = twitter.search(query);
  94.  
  95. tweets = result.getTweets();
  96.  
  97. listSize = tweets.size();
  98.  
  99. System.out.println("No of " + searchString + ": " + listSize);
  100.  
  101.  
  102.  
  103. }
  104. //if there is an error then catch it and print it out
  105. catch (TwitterException te)
  106. {
  107. System.out.println("Failed to search tweets: " + te.getMessage());
  108. System.exit(-1);
  109. }
  110. }
  111.  
  112. //gets the new tweets
  113. void getNoTweet()
  114. {
  115.  
  116.  
  117. //try the search
  118. try
  119. {
  120.  
  121.  
  122. Query query = new Query(searchString2);
  123. query.setCount(100);
  124. QueryResult result = twitter.search(query);
  125.  
  126. noTweets = result.getTweets();
  127.  
  128. listSizeNo = noTweets.size();
  129.  
  130. System.out.println("No of " + searchString2 + ": " + listSizeNo);
  131.  
  132.  
  133.  
  134.  
  135.  
  136. }
  137. //if there is an error then catch it and print it out
  138. catch (TwitterException te)
  139. {
  140. System.out.println("Failed to search tweets: " + te.getMessage());
  141. System.exit(-1);
  142. }
  143. }
  144.  
  145. void calculateAngle()
  146. {
  147. float total;
  148. float angle;
  149.  
  150. total = listSize + listSizeNo;
  151.  
  152. angle = (listSizeNo / total) * 180;
  153.  
  154. System.out.println(Math.round(angle));
  155. }
  156.  
  157.  
  158. Arduino code:
  159.  
  160. #include <Servo.h>
  161. Servo myservo;
  162.  
  163. const int tweetButtonPin = 2; // the number of the pushbutton pin
  164. const int searchButtonPin = 4; // the number of the pushbutton pin
  165. const int ledPin = 13; // the number of the LED pin
  166.  
  167. int counter = 0;
  168.  
  169. int ledState = HIGH;
  170. int tweetButtonState;
  171. int lastTweetButtonState = LOW;
  172.  
  173. int searchButtonState;
  174. int lastSearchButtonState = LOW;
  175.  
  176. // the following variables are long's because the time, measured in miliseconds,
  177. // will quickly become a bigger number than can be stored in an int.
  178. long lastTweetDebounceTime = 0;
  179. long tweetDebounceDelay = 50;
  180. long lastSearchDebounceTime = 0;
  181. long searchDebounceDelay = 50;
  182.  
  183.  
  184.  
  185. int pos = 0;
  186.  
  187. void setup()
  188. {
  189. Serial.begin(9600);
  190. myservo.attach(9);
  191. }
  192.  
  193. void loop()
  194. {
  195. // read the state of the switch into a local variable:
  196. int tweetButtonReading = digitalRead(tweetButtonPin);
  197. int searchButtonReading = digitalRead(searchButtonPin);
  198. // check to see if you just pressed the button
  199. if (tweetButtonReading != lastTweetButtonState)
  200. {
  201. // reset the debouncing timer
  202. lastTweetDebounceTime = millis();
  203. }
  204.  
  205. if (searchButtonReading != lastSearchButtonState)
  206. {
  207. // reset the debouncing timer
  208. lastSearchDebounceTime = millis();
  209. }
  210.  
  211. if ((millis() - lastTweetDebounceTime) > tweetDebounceDelay)
  212. {
  213. // whatever the tweetButtonReading is at, it's been there for longer
  214. // than the debounce delay, so take it as the actual current state:
  215. // if the button state has changed:
  216. if (tweetButtonReading != tweetButtonState)
  217. {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement