domingo, 22 de enero de 2012

Arduino 4: Brazo robótico con Arduino



Entre examen y examen he decidido darle un empujoncito a lo del brazo robótico y he estado experimentando con los servos. Este programa seria una ampliación de la cámara robotizada de la entrada Arduino 3.

El programa seria:

Proccesing

import processing.serial.*;
Serial port;
int i;

void setup() {
size(256, 150);
println("Available serial ports:");
println(Serial.list());
port = new Serial(this, Serial.list()[2], 9600);
}



void draw() {
byte prueba;
if (key == 's') {
fill(45);
prueba=40;
port.write(prueba);
}
if (key == 'd') {
fill(90);
prueba=60;
port.write(prueba);
}
if (key == 'f') {
fill(135);
prueba=90;
port.write(prueba);
}
if (key == 'g') {
fill(255);
prueba=120;
port.write(prueba);
}
if (key == 'a') {
fill(0);
prueba=1;
port.write(prueba);
}
rect(25, 25, 50, 50);
if (key == 'w') {
fill(45);
prueba=2;
port.write(prueba);
}
if (key == 'e') {
fill(90);
prueba=3;
port.write(prueba);
}
if (key == 'r') {
fill(135);
prueba=4;
port.write(prueba);
}
if (key == 't') {
fill(255);
prueba=5;
port.write(prueba);
}
if (key == 'q') {
fill(0);
prueba=6;
port.write(prueba);
}


if (key == 'z') {
fill(0);
prueba=16;
port.write(prueba);
}
rect(25, 25, 50, 50);
if (key == 'x') {
fill(45);
prueba=12;
port.write(prueba);
}
if (key == 'c') {
fill(90);
prueba=13;
port.write(prueba);
}
if (key == 'v') {
fill(135);
prueba=14;
port.write(prueba);
}
if (key == 'b') {
fill(255);
prueba=15;
port.write(prueba);
}

rect(50, 25, 50, 50);

}



Arduino

#include


Servo myservo; // crea un objeto tipo servo para controlar el servo
Servo myservo2; // crea un objeto tipo servo para controlar el servo
Servo myservo3; // crea un objeto tipo servo para controlar el servo
int pos = 0; // variable para almacenar la posición del servo

void setup()
{
Serial.begin(9600);
myservo.attach(9); // liga el servo conectado en el pin 9 al objeto servo
myservo2.attach(10); // liga el servo conectado en el pin 9 al objeto servo
myservo3.attach(11); // liga el servo conectado en el pin 9 al objeto servo
}

void loop()
{
byte brightness;
byte angulo;
// check if data has been sent from the computer:
if (Serial.available()) {
// read the most recent byte (which will be from 0 to 255):
brightness = Serial.read();
// set the brightness of the LED:
if(brightness == 1){
angulo=0;
myservo.write(1);
brightness = Serial.read();
delay(30);
brightness = Serial.read();
}
if(brightness == 40){
angulo=40;
myservo.write(40);
brightness = Serial.read();
delay(30);
brightness = Serial.read();
}
if(brightness == 60){
angulo=60;
myservo.write(60);
brightness = Serial.read();
delay(30);
brightness = Serial.read();
}
if(brightness == 90){
angulo=90;
myservo.write(90);
brightness = Serial.read();
delay(30);
brightness = Serial.read();
}
if(brightness == 120){
angulo=70;
myservo.write(120);
brightness = Serial.read();
delay(30);
brightness = Serial.read();


//Segundo Servo
}
if(brightness == 2){
angulo=45;
myservo2.write(angulo);
brightness = Serial.read();
delay(30);
brightness = Serial.read();
}
if(brightness == 3){
angulo=90;
myservo2.write(angulo);
brightness = Serial.read();
delay(30);
brightness = Serial.read();
}
if(brightness == 4){
angulo=120;
myservo2.write(angulo);
brightness = Serial.read();
delay(30);
brightness = Serial.read();
}
if(brightness == 5){
angulo=180;
myservo2.write(angulo);
brightness = Serial.read();
delay(30);
brightness = Serial.read();
}
if(brightness == 6){
angulo=1;
myservo2.write(angulo);
brightness = Serial.read();
delay(30);
brightness = Serial.read();
}

//Tercer Servo
if(brightness == 12){
angulo=45;
myservo3.write(angulo);
brightness = Serial.read();
delay(30);
brightness = Serial.read();
}
if(brightness == 13){
angulo=90;
myservo3.write(angulo);
brightness = Serial.read();
delay(30);
brightness = Serial.read();
}
if(brightness == 14){
angulo=120;
myservo3.write(angulo);
brightness = Serial.read();
delay(30);
brightness = Serial.read();
}
if(brightness == 15){
angulo=180;
myservo3.write(angulo);
brightness = Serial.read();
delay(30);
brightness = Serial.read();
}
if(brightness == 16){
angulo=1;
myservo3.write(angulo);
brightness = Serial.read();
delay(30);
brightness = Serial.read();
}
}
}


Y el video:



No hay comentarios:

Publicar un comentario

Seguir @Exergy123