3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

PUPPET CONTROLLER

Step 4Hook 'em together

Hook \
Now - swap out the mouse for the flex sensor.  

We can do this by using the serial output from the Arduino as the input to the processing code.

Now we have two peices of code.  The Arduino code reads the flex center, makes the readings into useful numbers  and sends the values to the serial port. 

The Processing code will read the values from the serial port and use the info to draw on the screen.

Here is a video clip:

[embeded video]

Here is the code:

//PROCESSING CODE
//pacman moouth opens/close with X input from arduino output (flex sensor)

import processing.serial.*;
Serial port;
float x = 0;
float val = 0;

void setup(){
size(400,400);
background(100);
smooth();
println(Serial.list());
String arduinoPort = Serial.list()[1];
port =new Serial(this, arduinoPort, 9600);
}

void draw(){
if (port.available() > 0) {
val = port.read();
print(val);
//val = mouseX;
print("-----");
x = map(val,0,150,0.01,0.5); //translate values input to useful values/println(x);
println(x);
}
background(100); //clear last image
arc(200,200,100,100,(x)*PI,PI+(1-x)*PI); //draw packman, using radians... p21

}



////////// Arduino Code ///////////////
//This is from an example inluded with Arduino IDE download.
// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott
//MPC - added serial output 'n stuff


//Here is the wiring:
//
//Wiring -
//|------Arduino Gnd
//Encoder |------Analog 0
//|------Arduino 5V
//
//| Yellow ------ Digi 9 (PWM)
//Top servo | Red -----------5V
//| Brown -------Gnd
//
//| Yellow ------ Digi 10 (PWM)
//Bottom servo | Red -----------5V
//| Brown ------

#include ; //use servo library

Servo myservo; // create servo object to control a servo
Servo otherServo ;

int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int valComp;

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
otherServo.attach(10);
Serial.begin(9600);
}

void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
//Serial.print(val);
//Serial.print("---");
val = map(val, 0,1023, 0,255);
valComp = val;
//Serial.println(val);
Serial.print(val,BYTE);
delay(10);
val = map(val, 10, 100, 90, 179); // scale it to use it with the servo (value between 0 and 180)
valComp = map(valComp, 100, 10, 0, 90);
myservo.write(val); // sets the servo position according to the scaled value
delay(10);
otherServo.write(valComp);
delay(10); // waits for the servo to get there
}







« Previous StepDownload PDFView All StepsNext Step »
1 comment
Aug 20, 2011. 2:55 PMêH says:
Great example.

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
42
Followers
18
Author:marc.cryan
Looking for access to land or water in Boston Metrowest area.