Category: Arduino

Twitter + Processing + Arduino

The journey from sensor to Twitter is a little complex but we are providing you with examples and code so you only get to do the fun, conceptual part. The system works like this:

1- Sensor input is read by Arduino
2- Arduino talks to Processing through serial
3- Processing uses a library called Twitter4j to interface with the Twitter API (Application Programming Interface)
4- The Twitter API tweets on the selected account

And vice versa.
Please note that if Processing crashes you’ll probably need to restart and unplug the board. And you can’t have
We’ll break this down into simple steps.

Twitter
In order to do this project, you’ll need to have a Twitter account, and then create an authorized “App”. Basically, Twitter wants you to have fun, but they also want to keep track of what you’re doing, so that you don’t send lots of spam. (Computer programs are good at that.) You’ll need to get credentials by doing the following:

  • Get a Twitter account
  • Create a Twitter application
  • Fill out the description of your application (e.g. “Caroline’sEMS2App”)
  • Agree to the developer’s license, fill out the CAPTCHA, etc…
  • Change your app settings (Settings tab) to “Read and Write”. Don’t forget to SAVE the settings
  • Click on the button to request AccessToken and AccessTokenSecret. If you cange the “read and write” settings you have to create new tokens
  • Copy and paste your credentials: OAuthConsumerKey and OAuthConsumerSecret, AccessToken and AccessTokenSecret in the Processing sketch
  • Twitter4j will require those credentials in order for you to do stuff.

Tweeting from Processing

Here’s a simple demo that Tweets from Processing:
processing_to_twitter.zip

Important: You have to change OAuthConsumerKey, OAuthConsumerSecret, AccessToken, AccessTokenSecret or you will tweet with my credentials @ems2tweet.

The documentation for the library (included in the zip) is here:
http://twitter4j.org/en/javadoc/index.html

Arduino
If you successfully sent your first tweet on your account, then you should be able to integrate it with Arduino and Firmata.

Exercise
Try to send something with a switch (possibly de-bounced) or another sensor. Please make sure your application doesn’t send too many tweets (test it offline first) or you account may be suspended.

Reading tweets from processing
This is an example that reads new tweets from your account. In this way you can control a local system (Processing/Arduino) remotely from everywhere.
tweetReceiver.zip

Exercise
Turn an LED on and off by sending certain tweets.

Twitter + Arduino

Here’s a complete example of bi-directional communication between Twitter and Arduino via processing. Tweet anything with the word “sensor” in it and the script will read and tweet the value of the sensor and turn on an LED.
twitterArduino.zip

Various Snippets

Serial issues with Firmata
It’s rare but sometimes you need to manually change the constructor for the Arduino in processing

println(Arduino.list());  
arduino = new Arduino(this, Arduino.list()[0], 57600);

Read the output of the “println(Arduino.list())” and look for the slot corresponding to /dev/tty.usbmodem… That’s the number you have to put between the brackets instead of 0 in Arduino.list()[0].

Comparing strings
You can’t compare strings in processing using the symbol ==. You have to use “equals”:

String p = "potato";
if (p.equals("potato")) {
  println("Yes, the contents of p and potato are the same.");
}

Parsing
Here are some basic snippets you should use to parse a tweet.
Looking for the word “love” in the tweet:

println ("Tweet = " + mostRecentTweetString) ;

    String keyword = "love";
    //indexOf returns the char position OR -1 if no mathces are found
    int locationOfTestStringInTweet = mostRecentTweetString.indexOf(keyword);
    if (locationOfTestStringInTweet != -1) {
      println ("Test string exists! (At character index: " + locationOfTestStringInTweet + ")");
    }

Looking for a number and converting it to integer:

//first I split my string into single words in an array of strings
    //separating with " " space
    String tweetWords[] = split(mostRecentTweetString, " ");

    //check all the words
    for (int i=0; i<tweetWords.length; i++) {      
      //try to see if there is a number
      try {
        int valueFetchedFromTweet = Integer.parseInt(tweetWords[i]);
        //if the number is parseable do something
        println("number found = " + valueFetchedFromTweet);
      }
      catch(Exception e) {
        //if it's not a number it throws an error
        println("No numbers in "+tweetWords[i]);
      }
    }

Parsing a formatted “command” from a tweet. A command in this example is a string followed by a space and a number like “servo 180”

//splitting the tweet into words
    String arrayOfWords[] = split (mostRecentTweetString, " ");

    if (arrayOfWords[0].indexOf("servo") != -1)
    {
      //if the first word is "servo" I look for an angle in the second word
      int servoAngle = Integer.parseInt(tweetWords[1]);
      
      println("moving servo to " + servoAngle);
      //careful if the command is not formatted it will throw an error
      arduino.analogWrite(servoPin1, servoAngle);
    }

    if (arrayOfWords[0].indexOf("LED") != -1)
    {
      println("BLINK BLINK");
      //...and so on
    }

Controlling Servos from Processing (and Twitter)
Unfortunately the Arduino/Firmata library for processing doesn’t allow you to use servo. In order to do that you have you create your own Arduino to Processing communication interface. You can find some examples online:
Controlling a servo motor with processing

Single servo example

Reading tweets from specific users
The code above reads from your account. But if you change this line:

statuses = twitter.getUserTimeline(); 

With something like this:

statuses = twitter.getUserTimeline (17454769);

You’ll get the last tweet of the user associated to that number.
To find the id of a certain user insert the twitter handle here:
http://www.idfromuser.com/

For example putting together this code with the one we saw on monday I can make an application that
1. Reads from @foxnews (Id: 1367531)
2. Looks for the string “Occupy Wall Street”
3. Makes an led blink
4. And “responds” with a tweet such as “@foxnews bullshit!” (not that you need a parser to detect bullshit on fox news)

Reading tweets from all users
You can check when a word is posted from all the users on twitter. Twitter is huge so pick an uncommon word or an hashtag such as #art.
tweetChecker.zip

Uploading a webcam snapshot to Flickr
I haven’t tested this one recently.
http://frontiernerds.com/upload-to-flickr-from-processing

Uploading image + status on Twitpic
I haven’t tested this one recently.
The integration with Twitpic is better than Flickr so if you want to quickly upload images and make them visible on Twitter try this example:
processingTwitpic.zip

It uses another library (included in the “code” folder). It uploads an image and post a twitter update with just a line of code. In this case the image is randomly generated and is uploaded after a keystroke. Use your Twitter account username and password (no API developer keys are needed).

IF THIS THEN THAT

ITTT is a social network glue that allows you to link and trigger events across web 2.0 services
Some examples here

For this assignment you can use your dedicated twitter account connected to processing as intermediary for all the other networks. es:

Facebook -> Twitter -> Processing -> Arduino -> Circuit

So many things can go wrong here :-P

Assignment: Networked Object

Make an artifact that creates a meaningful relation between the Internet (Twitter) and the “physical” world.
It can be a tweeting sensor or an object that responds to the flows of information on Twitter. Or there can be a two way communication. Ideas are more important than technical complexity.

*The point is to connect the physical world with the digital world: Twitter to Processing or Processing to Twitter is not enough.

*If you are reading data from accounts that you are not controlling, first make sure that the whole system works and then try to run it with the actual data.

*Don’t think about Twitter as just a personal microblogging platform but as a scale model of the Internet, a model that is extremely easy to parse and analyze. Pretty much every institution, group or media has a Twitter account, the possibilities are endless.

*If you are not familiar with Twitter find out how #hashtags and @replies and mentions work.

*Recap of the interactions you can easily implement:
– Tweeting from Processing
– Looking for tweets from Processing by account name or basic parsing (somebody tweets a specific word).
– Read sensors and control actuators via Processing/Arduino
– Capture images from the webcam
– Post images on Flickr from processing
– Everything you already know about Processing
– External Processing libraries from the internet

Due dates
Ideas by Tuesday 27
Projects by Thursday 29

Assignment: Alternative Interface

Make a processing sketch (screen based work) that employs a non-standard Arduino-powered interface.

– Think about how these sensors can be integrated into an object (es. flex sensor to detect the opening of a box) and within a specific context (es. pressure sensor to detect somebody sitting in front of the screen).

– Think about a screen or sound output that is meaningful and related to the interaction. Abstract visuals are fine but go beyond randomish flickering shapes this time.

– More is not necessarily better but consider using more than one sensor.

Advanced: make a simple game (single player or multiplayer) built around the interface. Don’t spend too much time making graphics please.

Document interaction and screen output with a video and post the link here. Phone cameras are fine but screen recordings may work better.
Quicktime for Mac can record what happens on the screen and can be used in combination with a webcam feed from photo booth.

Arduino + Processing

The instructions to make arduino talk to Processing through Firmata are here.

1- Unzip the Processing library and copy the “arduino” folder into the “libraries” sub-folder of your Processing Sketchbook.
(You can find the location of your Sketchbook by opening the Processing Preferences. If you haven’t made a “libraries” sub-folder, create one.)

2- Run Arduino, open the Examples > Firmata > StandardFirmata sketch,
and upload it to the Arduino board.

3- Make this circuit:

4- Run the processing sketch below.

//import the libraries for serial communication
//and for arduino/firmata specifically
import processing.serial.*;
import cc.arduino.*;

//this object class Arduino
//represents guess what? Your board
Arduino arduino;

//like in an arduino sketch it's good to
//use variables for pin numbers 
int buttonPin = 13;
int sensorPin = 0;

void setup() {
  size(500, 500);
  smooth();
  noStroke();
  ellipseMode(CENTER);
  
  //the arduino object needs to be created at the beginning
  arduino = new Arduino(this, Arduino.list()[0], 57600);
  
  //once it's created I set the pin modes  
  arduino.pinMode(buttonPin, Arduino.INPUT);
  arduino.pinMode(sensorPin, Arduino.INPUT); 
}

//we are in processing so the main function is draw (not loop)
void draw()
{
  //read a digital value from the button pin
  int digitalValue = arduino.digitalRead(buttonPin);
  
  //change the background accordingly
  if (digitalValue == Arduino.HIGH)
    background(255);
  else
    background(0);
    
  //read an analog value from the sensor pin
  int analogValue =  arduino.analogRead(sensorPin);
  println(analogValue); //print it for testing purposes
  
  //draw a red circle ellipse the size of the analog value
  fill(255,0,0);
  ellipse(width/2, height/2, analogValue, analogValue);
}

Some exercises:
– Threshold switch, if the pressure sensor reaches a certain value a sound is triggered.

– Combine this example with your order and chaos processing sketch, replacing the pressure sensor with the slider.

Try with a photoresistor:

Or a magnet+Hall Effect

– Team up with a friend and use two knobs potentiometers to make a digital etch-a-sketch. One pot controls x, controls y, the red buttons clear the drawing.

Processing to arduino without Firmata

You can write your own serial communication protocol without using firmata in case you have issues with servo and stuff.
Here’s an example with two analog inputs to processing.

Arduino Code:


int sensorValue0;
int sensorValue1;
 
void setup() {
  // initialize the serial communication:
  Serial.begin(9600);
}
 
void loop() {
  sensorValue0 = analogRead(A0);
  sensorValue1 = analogRead(A1);
  Serial.print(sensorValue0);
  Serial.print(",");
  Serial.println(sensorValue1);
 
  delay(10);
}

Processing code:

import processing.serial.*;
 
int sensor0 = 0;
int sensor1 = 0;
 
Serial myPort;
 
void setup() {
  size(200, 200);
 
  // List all the available serial ports
  println(Serial.list());
  // I know that the first port in the serial list on my mac
  // is always my  Arduino, so I open Serial.list()[0].
  // Open whatever port is the one you're using.
  myPort = new Serial(this, Serial.list()[0], 9600);
  // don't generate a serialEvent() unless you get a newline character:
  myPort.bufferUntil('\n');
}
 
void draw() {
  // do your stuff here!
}
 
void serialEvent(Serial myPort) {
  // get the ASCII string:
  String inString = myPort.readStringUntil('\n');
 
  if (inString != null) {
    // trim off any whitespace:
    inString = trim(inString);
    // split the string on the commas and convert the
    // resulting substrings into an integer array:
    int[] sensors = int(split(inString, ","));
    // if the array has at least two elements, you know
    // you got the whole thing.  Put the numbers in the
    // sensor variables:
    if (sensors.length >=2) {
      sensor0 = sensors[0];
      sensor1 = sensors[1];
    }
  }
}

Assignment: robot(?) / interactive artwork

You have three options for this assignment on Actuators:

1. Make a “social robot”: not necessarily a proper robot (we don’t have the equipment for that) but a machine that produce a life-like behavior that exploit the Eliza Effect.
Remember the Furby guy:
– expressive character (eyes, face)
– response to the environment
– unpredictable/evolving behavior the illusion of life often comes from a non-direct mapping of the input. If the input is used as a switch and the behavior is purely deterministic the object will appear as smart as a blender. If you implement some kind of processing and memory you are more likely to produce the Eliza effect.

2. Make a stand-alone interactive artwork that responds to the environment.

For both the options the only requirement is an input-process-output cycle. The thing has to receive an input from a sensor and produce a meaningful output through motor and or servo and/or LEDs or piezo speaker.

*Let me know by the end of the class if you want to use my distance sensor or my 3axis accelerometer.

*Modifying existing toys is another way to go. Electronic toys may offer more possibilities but hacking miniaturized and undocumented printed circuits is hard.

*You can combine microphone input via Processing (first assignment) with actuators.

*For example this is how you play sound files in Processing through Minim

import ddf.minim.*;

Minim minim;
AudioPlayer song;

void setup()
{
  size(100, 100);

  minim = new Minim(this);

  // this loads mysong.wav from the data folder
  song = minim.loadFile("mysong.wav");
  song.play();
}

void draw()
{
  background(0);
}

void stop()
{
  song.close();
  minim.stop();

  super.stop();
}

“Advanced” students could try to use processing with other libraries.
For example with openCv you can track a colored object or a face fairly easily (and if you have an external webcam you can try to mount it on the servo an make it follow the shape)
http://ubaa.net/shared/processing/opencv/

Assignment: Automaton

We know how to use actuators (servos and DC motor), now let’s try to actuate something interesting.

Using the Automino kit as a starting point, make an automaton / kinetic sculpture / mechanical thingy with the following characteristics:

– produces a complex and meaningful movement(s)
– employs at least one laser cut piece of your own devise
– integrates non-laser-cut elements. Tip: try non-rigid, non structural materials (jelly, feathers, balloons, beetles, leaves, taxidermy…)
– You can work in groups of two
– If you don’t have a kit just use other materials
– I recommend using only the servos (if you are working in groups you can control 4 of them!)
– If you think your project is too ambitious, then it’s probably too ambitious.
– I mean technically ambitious not conceptually ambitious, there’s no limit to the latter
– Favor original, poetic and beautiful projects rather than technically complex ones.
– Can machines be expressive? Can you make me forget there’s a buzzing servo behind it?

Don’t make it interactive or responsive to the environment, that’s the next assignment. Focus on the “actuation”.

Automata


It all started with automata like the Digesting Duck

 

Or the praying monk, built in South Germany or Spain around 1560.

Hear the story in this episode of Radiolab.

Or the Ultimate machine, theorized by Claude Shannon, the father of information theory:

Modern automata of the Cabaret Mechanical Theater

Existentialist mechanical sculptures by Arthur Ganson

Sprawling modular mechanisms by SOA alumn Greg Witt

Wind powered “beach animals” by Theo Jansen
Strandbeest

Riotous robotic performances by the Survival Research Lab

Automino

Automino is a modular construction kit for prototyping small scale robotic/kinetic art projects. You can expand or modify the kit with a vector based design application like Illustrator or Inkscape and fabricate the pieces with a laser cutter.

Automino is specifically designed to be used with:

  • Machine screws 6-32. I strongly recommend 3/4” and 3/8” lenght
  • 0.12 inches thick hardboard
  • 0.11 inches diameter kabob sticks
  • The components from your extended Sparkfun Inventor Kit powered by Arduino

Downloads


Download sheet 1 (Illustrator eps file)


Download sheet 2 (Illustrator eps file)

Examples

Put Arduino in a box!
Other laser cutter friendly box solutions:
Boxmaker (PDF generation)
Boxotron (python script)

Snap-on joints! Better than Erector and with a slightly less embarrassing name.
Why always 90 degrees? Check this organic construction kit.

Weirdo screw-centric joints.
Based on this wooden construction kit

No more floating switches! No more out-of control motors! A way to integrate the DC motor

Gears that work (hopefully) and many basic mechanical elements!
That’s how you use the kabob sticks as axles. Use the star-shaped hole symbol to make jammable pieces.
Check this page for more mechanism.

The rack and pinion is used to convert between rotary and linear motion. You can use it with a servo 180 degrees Crank. Warning: I’m pretty sure it’s not possible to make a proper crank that doesn’t fall apart using these materials. This is meant to be used with the servo, it can’t do full rotation. Cam: rotary to linear motion. The two oval pieces rotate pushing up and down the two vertical bars.

Customizing the kit

The basic module for all the pieces is a square of 0.065. If you are not opening the .ai files set the grid to .13 inches with 2 subdivisions and “snap” to the grid every time you are drawing new parts.

Don’t worry if you are not familiar with Illustrator, you can do pretty much anything by using the pen, the arc, the free transform and the direct selection tools.

Remember
– use a stroke of 0.001 or less
– Get rid of the text layer with orange labels
– Export in eps format

Symbols

If you have Illustrator you can easily adapt the kit for other materials and other types of screws by modifying the symbol library (window > symbols). The symbols define the slots for snap-on joints and the holes for the screws. By changing a symbol, you’ll affect all of its instances in the document.

Laser cutting

The School of Art’s laser cutter is in Doherty Hall C316.
You can schedule an appointment with a monitor by emailing laser-art [at] andrew…
Monitor hours are on Friday and Saturdays 10.30-4.30.

Here’s a step by step guide:

  • Get the material first. Hardboard panels are sold at the art store (University Center underground floor). You can’t buy them in the lab.
  • Measure your panel and put it in the cutter as shown below. Close the glass panel.
    • I assume the monitor is there, in any case make sure the laser cutter is on and the gas valve is open.
    • Once you are logged in the laser cutter’s PC, download your eps file and open it with Corel Draw X5.
    • Make sure all the lines are set as hairline. Object properties > stroke > width

    • Make sure your design fits into the page dimension, if not change the document size. You can move and rearrange the design in Corel according to your hardboard size and design.
    • Click file > print…
    • Choose Epilog Engraver as device

    • Click on preferences
    • Make sure that the piece size corresponds to the page size
    • Change the vector settings to: speed 10, power 100%, frequency ~1000hz
      If you are using other materials check the reference table.

    • Click ok, check the preview image on the right and click print if everything looks fine.
    • Press the “Go” button on the cutter.
    • Stick around until is done but don’t stare at the laser!
    • Clean up the machine when you are done.

    More info about the laser cutter here.