How to make a remote-control sentient web-puppet by hacking Twitter, Google, Skype, Arduino and Processing! by rosemarybeetle
Featured

Step 3: Using Arduino to control physical actions based on the data received (with code)

ArduinoUnoSmd.jpg
Once data had been extracted from the web with the Processing sketch, an Arduino microcontroller used it to activate Twitr_janus's head. The data was converted into control signals for servos inside Twitr_janus' head, which moved its eyes and jaw. This is explained below...


Controlling an Arduino from a PC

The Arduino is doing several things:
  • It is attached to the master computer - digitally via a USB connection and also with analogue input from the audio output of the computer
     
  • It maintains a serial port connection with the master computer, over which it is constantly checks for new control digital data that has been sent over the web to the master computer. 
    >>>
    This line is Arduino making the serial connection...
    Serial.begin(115200);

    This line is calling a routine that will check the connection fo rdate...
    checkSerial ();
    <<<
     
  • It checks every 10-30 seconds and compares incoming data to the last received action and only acts on it if different.
    >>> void checkSerial ()
    {
    It checks if there is a connection...
    if (Serial.available() > 0) {

    Reads the data...
    incomingByte = Serial.read();
    }

    If the data is 30, it will trigger the Twitter routine (twitterCheck()...
    if (incomingByte==30 ) //
    {

    twitterCheck();
    }

    If the data is between 0 and 25, it's Google data, so call the Google checking function - googleCheck()
    if ((incomingByte<=25) && (incomingByte >0) ) // google data is data coded as an integer  between 0 and 25
    {
    googleCheck();
    else {// No point calling check functions if not serial data received. This is error handling clause
    Serial.println("I received nothing ");
    }
    }// enf of checkSerial
    <<<
  • If new data is received, it will light up indicator warts on its forehead. These are lit by LEDs. 

    An orange wart lights when Google data (integer between 0 and 25) has been received, the data is used to reposition the servos and the blue wart is deactivated
    >>>
    void googleCheck()
    {
    digitalWrite(twitterFlagPin, LOW);
    eyeLeftRight= 2*(incomingByte-1);
    eyeUpDown = (2*incomingByte)-1;
      if (incomingByte<=25 )
      {
         digitalWrite(googleFlagPin, HIGH);
        servoLeftRight.write(eyePos[eyeLeftRight]);
    servoUpDown.write(eyePos[eyeUpDown]);
       }
      }

    <<<

    A blue wart lights when Twitter data has been received (integer 30) and the orange wart is deactivated  
    >>>
    void twitterCheck ()
    {
      // this function
    digitalWrite(twitterFlagPin, HIGH);
    digitalWrite(googleFlagPin, LOW);
     }

    <<<
     
  • It will convert incoming eyeball data into one of several pre-determined position control values for each of the two servos inside the head. One for up/down, one for left/right. It uses the Arduino servo.h library to do this
    >>>
    The data coming in is used to access data from an array which has pre-determined values in it (these are the servo values) 
    int eyePos [] = {115, 60, 115, 60, 115, 90, 115, 115, 115, 115, 115, 60, 115, 60, 115, 90, 115, 115, 115, 115, 90, 60, 90, 60, 90, 90, 90, 115, 90, 115, 60, 60, 60, 60, 60, 90, 60, 115, 60, 115, 60, 60, 60, 60, 60, 90, 60, 115, 60, 115};
    <<<
     
  • It will activate the jaw lip-sync by monitoring the analogue audio input. It uses voltage peak detection to do this. If the sound wave rises above a pre-determined threshold, the jaw will open, and it will be forcibly close if the voltage drops below the threshold. This gives a pleasantly startling staccato.
    >>>
    void analogPeakCheck()
    {
      // @@@@@@@ this function is used if you are using raw audio output from an analog amplifier into the Analog pin 0
    valueAnalogIn = analogRead(analogInput); // This is checking for output above a threshold voltage to trigger jaw signal
    if (valueAnalogIn>thresholdAnalogIn)
    {
    digitalWrite(speechFlagPin, HIGH);
    digitalWrite(speechFlagPinLED, HIGH);


    else {
    digitalWrite(speechFlagPin, LOW);
    digitalWrite(speechFlagPinLED, LOW);


    }// @@ end threshold checking //
    }

    <<<
The individual code for controlling the Arduino is adapted from basic code on arduino.cc/

Download the full Arduino Sketch

The complete Arduino sketch is available to download here from GitHub as a text file:
http://cloud.github.com/downloads/rosemarybeetle/Twitr-Janus/twitr_janus_arduino_09.txt

For easy viewing, here is an image facsimile of the full text
 
Remove these adsRemove these ads by Signing Up
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!