Skip to content

Instantly share code, notes, and snippets.

@JosiahThobejane
Last active April 17, 2019 18:45
Show Gist options
  • Save JosiahThobejane/7715e0007c3f5bbe955c66f9221de787 to your computer and use it in GitHub Desktop.
Save JosiahThobejane/7715e0007c3f5bbe955c66f9221de787 to your computer and use it in GitHub Desktop.
Controlling a Arduino using Twitter. You will need to download the Twitter4J library on http://twitter4j.org then import all the .jar files in the processing IDE.
//This code has to be opened in the Processing IDE, put it in the same folder as the JosKartTwitter.pde
//We are gonnna use the Twitter4J library.
import twitter4j.FilterQuery;
import twitter4j.StallWarning;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.TwitterStream;
import twitter4j.conf.ConfigurationBuilder;
import twitter4j.StatusDeletionNotice;
import twitter4j.StatusListener;
import twitter4j.TwitterStreamFactory;
public class JosKartEngine {
Twitter twitter;
TwitterFactory twitterFactory;
TwitterStream twitterStream;
String[] directions = {"MOVEFORWARD", "MOVELEFT", "MOVERIGHT", "REVERSE"};
String direction = " ";
double distance = 0;
double calculatedDistance;
int secondsPerCM = 1507;
public void initConfig() {
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.setDebugEnabled(true)
.setOAuthConsumerKey(" ") //you will need to use your own Twitter Dev credentials.
.setOAuthConsumerSecret(" ")
.setOAuthAccessToken(" ")
.setOAuthAccessTokenSecret(" ");
twitterStream = new TwitterStreamFactory(configurationBuilder.build()).getInstance();
println("CONFIGS ARE DONE");
}
//get a number from the tweet
public String getDistanceFrom (String theTweet)
{
if(theTweet == null || theTweet.isEmpty()) {return "0";}
StringBuilder stringBuilder = new StringBuilder();
boolean foundDistance = false;
for(char c : theTweet.toCharArray()){
if(Character.isDigit(c)){
stringBuilder.append(c);
foundDistance = true;
} else if (foundDistance) {break;}
}
return stringBuilder.toString();
}
public void stopMovementAfter(int actualDistance)
{
delay(actualDistance);
serial.write(stopMoving());
println("Distance covered...skirr");//I shouldn't have this here
}
public void listenOnTweets()
{
StatusListener statusListener = new StatusListener(){
@Override
public void onException(Exception ex) {
System.out.println("ERRRORR: " + ex);
}
@Override
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
System.out.println("LIMIT: " + numberOfLimitedStatuses);
}
@Override
public void onStatus(Status status) {
String theStatus = status.getText().toUpperCase();
//the distance
try { distance = Integer.parseInt(getDistanceFrom(theStatus));
} catch (NumberFormatException ex) {
System.out.println("OOPSS...numbers game: " + ex);
}
// the most magic happens on line 84 and 85
calculatedDistance = (distance/28.571) * 1000; //28.. is the estimated speed of the motors on full throttle
Integer timeLapse = Integer.valueOf((int) Math.round(calculatedDistance));//T = D/V * 1000 seconds
System.out.println("\n\n-------------NEW TWEET-----------------");
System.out.println("\nTWEET: " + status.getText() + " BY: " + status.getUser().getScreenName());
if (theStatus.contains(directions[0]))
{
println("\n\n--------Extracted Data-----------");
println("Direction: " + directions[0] + "\nDistance: " + distance + "\nTravel Time: " + timeLapse + " milliseconds");
serial.write(moveForward());
stopMovementAfter(timeLapse);
} else if(theStatus.contains(directions[1]))
{
println("Direction: " + directions[0] + "\nDistance: " + distance + "\nTravel Time: " + timeLapse + " milliseconds");
serial.write(moveLeft());
stopMovementAfter(timeLapse);
} else if(theStatus.contains(directions[2]))
{
println("Direction: " + directions[0] + "\nDistance: " + distance + "\nTravel Time: " + timeLapse + " milliseconds");
serial.write(moveRight());
stopMovementAfter(timeLapse);
} else if(theStatus.contains(directions[3]))
{
println("Direction: " + directions[0] + "\nDistance: " + distance + "\nTravel Time: " + timeLapse + " milliseconds");
serial.write(moveBackward());
stopMovementAfter(timeLapse);
} else {
System.out.println("CAN'T READ DIRECTION & Distance (maybe)");
}
System.out.println("\n-------------END TWEET-----------------");
}
@Override
public void onStallWarning(StallWarning warning) {
System.out.println("STALLING: " + warning);
}
@Override
public void onScrubGeo(long userId, long upToStatusId) {
System.out.println("USER: " + userId + " : " + upToStatusId);
}
@Override
public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
System.out.println("DELETING STATUS: " + statusDeletionNotice);
}
};
//the magic is here..a bit of it
FilterQuery filterQuery = new FilterQuery();
String keyWord = "#redbullbasement"; //you can use any keyword of choice.
filterQuery.track(keyWord);
twitterStream.addListener(statusListener);
twitterStream.filter(filterQuery);
}
}
//This code has to be opened in the Processing IDE, put it in the same folder as the JosKartEngine.pde
import processing.serial.*;
Serial serial;
JosKartEngine josKartEngine = new JosKartEngine();
void setup()
{
josKartEngine.initConfig(); //start the engine
size(400, 200);
serial = new Serial(this, "COM14", 9600);
josKartEngine.listenOnTweets(); // I dont think this will work...lol
}
void draw()
{
background(0);
//textAlign(CENTER);
//drawType(width * 0.5);
}
int moveForward() { return 1; }
int moveBackward() { return 2; }
int moveLeft() { return 3; }
int moveRight() { return 4; }
int stopMoving() {return 0;}
void drawType(float x) {
line(x, 0, x, 65);
line(x, 220, x, (height * 0.5));
fill(255);
text(directionText, x, 95);
}
//This code has to be uploaded to the Arduino
const int motorAForward = 11;
const int motorABackward = 10;
//motor B stuff
const int motorBForward = 9;
const int motorBBackward = 8;
int directionData;
void setup() {
// put your setup code here, to run once:
pinMode(motorAForward, OUTPUT); //set pin to output
pinMode(motorBForward, OUTPUT); //set pin to output
pinMode(motorABackward, OUTPUT);
pinMode(motorBBackward, OUTPUT);
Serial.begin(9600);
}
void loop() {
if(Serial.available() > 0)
{
directionData = Serial.read();
//move forward
if(directionData == 1)
{
moveForward();
}
//movebackward
if(directionData == 2) {
moveBackward();
}
//move left
if(directionData == 3) {
moveForwardLeft();
}
//move right
if(directionData == 4) {
moveForwardRight();
}
if(directionData == 0){
dontMove(); //this should work
}
}
}
void moveForward()
{
analogWrite(motorAForward, 255);
analogWrite(motorBForward, 255);
}
void moveBackward()
{
analogWrite(motorABackward, 255);
analogWrite(motorBBackward, 255);
}
void moveForwardLeft()
{
analogWrite(motorAForward, 0);
analogWrite(motorBForward, 255);
}
void moveForwardRight()
{
analogWrite(motorAForward, 255);
analogWrite(motorBForward, 0);
}
void dontMove()
{
analogWrite(motorAForward, 0);
analogWrite(motorBForward, 0);
analogWrite(motorABackward, 0);
analogWrite(motorBBackward, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment