Step 4Hook 'em together
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
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 Step | Download PDFView All Steps | Next Step » |
1
comment
|
Add Comment
|
![]() |
Add Comment
|

















































