Introduction: Connect Your Arduino With Processing
Have you ever thought about...? Using Arduino with Processing? In this Instructable I show you the quick and easiest way to do this. Just keep reading!
Cover photo by jeanbaptisteparis: https://www.flickr.com/photos/jeanbaptisteparis/119421176/in/photolist-by4KN-7Zzrcc-adQqaY-5vhJcc-eRBNH-5Drouk-3kCyDK-drpMiz-fa7aQD-aCTkn4-5MteZi-aqzAxZ-9zADJH-5EC27P-8hQcFC-5b1qa2-f3qTCx-7qrKSa-dumdLx-79ymo7-8hQcM1-8hLXBM-8hQcHo-adQqb3-5oZuGc-aLebFB-fa7aLT-3Lf1Fr-4RspWS-HczTQ-5Zcr1h-e3MTdX-hXzQ4X-aCX4pj-4PXBXR-8hLXCn-8hQcKh-4ZBGm3-hXAEBM-6NJLQ6-a5yLTf-78RTx2-5Mtf8R-7CoxMG-84NCNx-euAT7A-fqLG7X-azDEM3-6gYhJm-9UTBCj
Step 1: Step 1: Open Processing
The first step is opening the Processing program. This is the code:
import processing.serial.*;
Serial myPort; // Create a serial object for the usb port
void setup(){
size(600, 200);
String portName = Serial.list()[7]; //here is sometimes an error with the compilation. Try to change numbers until //you get the right one.
myPort = new Serial(this, portName, 9600);
}
void draw() {
if(mousePressed == true){ // if you press the mouse, then...
myPort.write('1'); //write one
println("1"); // print one
}
else{ // if not
myPort.write('0'); // write zero
}
}
Step 2: Step 2: Arduino
Code:
#include <Servo.h> // Servo library
Servo myservo; // Create a Servo object
char val; // Data received from the serial port
void setup() {
myservo.attach(7); // Where are you attaching your servo? You can change this number.
Serial.begin(9600); // Start serial comunication at 9600 bps
}
void loop(){
if(Serial.available()){ // If the serial port is available
val = Serial.read(); //Read the serial port
}
if(val == '1'){ // if you're getting one, then
myservo.write(180);} // move your servo 180º
else{ // if not
myservo.write(50); // move your servo 50º
}
delay(10); // wait 10 miliseconds for the next reading
}
Step 3: Schematics
Just connect your servo to Arduino as it's shown.
Enjoy!