Calibration Code

While this wont be the final calibration code, it’s what I developed for my most recent video. A lot of the code is form rather than function as 75% of the buttons perform the exact same task, however the sequence in which you push the buttons allows for a more consistent functionality. Those who can read the code will probably laugh at the redundancy, however it makes more sense to split it up in this way as it makes each slider and each button its own function.

Processing Code:

import hypermedia.net.*;
import processing.serial.*;

UDP udpr; 
Serial myPort;
int mx=0;
int my=0;
int premy = 0;

void setup() {

udpr = new UDP( this, 8000);
//udp.log( true );
udpr.listen( true );

myPort = new Serial(this, Serial.list()[0], 9600);
myPort.buffer(2);
size(400, 400);
}

void draw() {

String[] coo = split(message, ‘,’);
if (coo != null)
{
mx = int(coo[0]);
my = int(coo[1]);
}
if (mx > 16) {
myPort.write(mx);
println(mx);
}
if (my != premy) {
myPort.write(my);
}
println(my);
premy = my;
}

void receive( byte[] data, String ip, int port ) 

data = subset(data, 0, data.length);
message = new String( data );

}

 

Arduino Code:

#include <AFMotor.h>
#include <Servo.h>

Servo myservo; // pen servo
Servo myservo2; // machine servo
AF_Stepper stepper(200, 2); // number of steps per full rotation, PIN

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

void setup()
{
myservo.attach(9);
myservo2.attach(10);
Serial.begin(9600);
stepper.setSpeed(20);
}

void loop()
{
int input = Serial.read();
if (input > 16 && input < 180) {
myservo.write(input); 
delay(5); 
Serial.println(input);
}

switch (input){
case 1:
if(input == 1){
myservo.write(17);
delay(5);
myservo2.write(100);
delay(1000);
myservo2.write(110);
delay(1000);
myservo2.write(120);
delay(1000);
myservo2.write(130);
delay(1000);
myservo2.write(140);
delay(1000);
myservo2.write(150);
delay(1000);
myservo2.write(160);
delay(1000);
myservo2.write(170);
delay(1000);
myservo2.write(172);
stepper.step(1701, FORWARD, DOUBLE);
}
break;
case 2:
if(input == 2){
myservo.write(17);
delay(5);
myservo2.write(170);
stepper.step(567, BACKWARD, DOUBLE);
}
break;
case 3:
if(input == 3){
myservo.write(17);
delay(5);
myservo2.write(170);
stepper.step(567, BACKWARD, DOUBLE);
}
break;
case 4:
if(input == 4){
myservo.write(17);
delay(5);
myservo2.write(170);
stepper.step(567, BACKWARD, DOUBLE);
}
break;
case 5:
if(input == 5){
myservo.write(17);
delay(5);
myservo2.write(172);
delay(1000);
myservo2.write(168);
delay(1000);
myservo2.write(158);
delay(1000);
myservo2.write(148);
delay(1000);
myservo2.write(138);
delay(1000);
myservo2.write(128);
stepper.step(1701, FORWARD, DOUBLE);
}
break;
case 6:
if(input == 6){
myservo.write(17);
delay(5);
myservo2.write(128);
stepper.step(567, BACKWARD, DOUBLE);
}
break;
case 7:
if(input == 7){
myservo.write(17);
delay(5);
myservo2.write(128);
stepper.step(567, BACKWARD, DOUBLE);
}
break;
case 8:
if(input == 8){
myservo.write(17);
delay(5);
myservo2.write(128);
stepper.step(567, BACKWARD, DOUBLE);
}
break;
case 9:
if(input == 9){
myservo.write(17);
delay(5);
myservo2.write(128);
delay(1000);
myservo2.write(124);
delay(1000);
myservo2.write(114);
delay(1000);
myservo2.write(104);
delay(1000);
myservo2.write(94);
delay(1000);
myservo2.write(84);
stepper.step(1701, FORWARD, DOUBLE);
}
break;
case 10:
if(input == 10){
myservo.write(17);
delay(5);
myservo2.write(84);
stepper.step(567, BACKWARD, DOUBLE);
}
break;
case 11:
if(input == 11){
myservo.write(17);
delay(5);
myservo2.write(84);
stepper.step(567, BACKWARD, DOUBLE);
}
break;
case 12:
if(input == 12){
myservo.write(17);
delay(5);
myservo2.write(84);
stepper.step(567, BACKWARD, DOUBLE);
}
break;
case 13:
if(input == 13){
myservo.write(17);
delay(5);
myservo2.write(84);
delay(1000);
myservo2.write(80);
delay(1000);
myservo2.write(70);
delay(1000);
myservo2.write(60);
delay(1000);
myservo2.write(50);
delay(1000);
myservo2.write(40);
stepper.step(1701, FORWARD, DOUBLE);
}
break;
case 14:
if(input == 14){
myservo.write(17);
delay(5);
myservo2.write(40);
stepper.step(567, BACKWARD, DOUBLE);
}
break;
case 15:
if(input == 15){
myservo.write(17);
delay(5);
myservo2.write(40);
stepper.step(567, BACKWARD, DOUBLE);
}
break;
case 16:
if(input == 16){
myservo.write(17);
delay(5);
myservo2.write(40);
stepper.step(567, BACKWARD, DOUBLE);
}
break;
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: