lunes, 5 de diciembre de 2011

Proyector Laser DIY v1

Materiales:
  • un laser rojo de 5mw
 (http://www.dealextreme.com/p/red-laser-module-focused-dot-3-5v-4-5v-9mm-5mw-5900)
  • 2 motores pap unipolares a 12V
(http://www.riabelectronics.cl/motor-paso-a-paso-de-alta- calidad-12v.html)

El laser lo conecto a un pin digital del arduino como si se tratase de
un led

Para conectar los motores al arduino, utilizo dos chip ULN2003A.
Conectándolo al arduino por el metodo de los 4 cables.
(http://www.arduino.cc/en/Tutorial/StepperUnipolar)



He utilizado la libreria Stepper, y los motores los he configurado a
una velocidad de 9 vueltas por minuto (lentorro) y 2039 pasos por
vuelta. Todo esto a ojo. Modificandolo por el método de prueba-
error...

Si los pongo a mas velocidad, los motores empiezan a dar pasos de
mas o de menos.

Le he añadido un joystick de los de playstation (http://www.seeedstudio.com/depot/electronic-brick-playstation2-analog-joystickanalog-p-468.html?cPath=190) para colocar los espejos en la posicion inicial indicada, y cuando aprieto el botón, que dibuje un dibujo preestablecido. Un cuadrado, un
triangulo... en este caso fué una especie de corazón que está dibujado en papel en la siguiente foto en la esquina.








Para dibujar he hecho una función que le vas pasando punto inicial y
punto final, y te dibuja la recta entre los dos puntos.Implementando el algoritmo de Bresenham.

 #include <Stepper.h>

#define STEPS 2039
#define ledPin 2  //LASER

Stepper stepper1(STEPS, 8, 9, 10, 11);
Stepper stepper2(STEPS, 4, 3, 5, 6);

const int buttonPin = 12;
int buttonState = 0;

int posX = 0;
int posY = 0;

void setup()
{
  Serial.begin(9600);
  // set the speed of the motor to 30 RPMs
  stepper1.setSpeed(9);
  stepper2.setSpeed(9);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
 
  digitalWrite(ledPin, HIGH);
}

void loop()
{
   buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    corazon();
  }
 
  digitalWrite(ledPin, HIGH);
  // get the sensor value
  int val = analogRead(0);

  if(val>850){
    moverMotor1i();
  }else if(val < 190){
    moverMotor1();
  }
  
  int val2 = analogRead(1);
  Serial.println(val2);
  if(val2>950){
    moverMotor2();
  }else if(val2 < 100){
    moverMotor2i();
  }
}

void corazon(){
  posX = 0;
  posY = 0;
  dibujaLinea(8, 4);
  dibujaLinea(12, 12);
  dibujaLinea(8, 20);
  dibujaLinea(5, 15);
  dibujaLinea(3, 15);
 
  dibujaLinea(0, 9);
  dibujaLinea(0, 7);
  dibujaLinea(0, 9);
 
  dibujaLinea(-3, 15);
  dibujaLinea(-5, 15);
  dibujaLinea(-8, 20);
  dibujaLinea(-12, 12);
  dibujaLinea(-8, 4);
  dibujaLinea(0, 0);
}

void dibujaLinea(int x1, int y1){
  bresenham(posX, posY, x1, y1);
  posX = x1;
  posY = y1;
}

void bresenham(int x0, int y0, int x1, int y1){
  int x;
  int y;
  int dx;
  int dy;
  int p;
  int incE;
  int incNE;
  int stepx;
  int stepy;
  int nStepX;
  int nStepY;
 
  nStepX = 0;
  nStepY = 0;
 
  dx = (x1 - x0);
  dy = (y1 - y0);
/* determinar que punto usar para empezar, cual para terminar */
  if (dy < 0) {
    dy = -dy;
    stepy = -1;
  } else{
    stepy = 1;
  }
 
  if (dx < 0) {
    dx = -dx;
    stepx = -1;
  }
  else {
    stepx = 1;
  }
  x = x0;
  y = y0;
 
  //g.drawLine( x0, y0, x0, y0);
/* se cicla hasta llegar al extremo de la línea */
 
  if(dx>dy){
    p = 2*dy - dx;
    incE = 2*dy;
    incNE = 2*(dy-dx);
    while (x != x1){
      x = x + stepx;
      stepper1.step(stepx);
      nStepX = nStepX + stepx;
      if (p < 0){
        p = p + incE;
      }
      else {
        y = y + stepy;
        stepper2.step(stepy);
      nStepY = nStepY + stepy;
        p = p + incNE;
      }
      //g.drawLine( x0, y0, x0, y0);
    }
  }
  else{
    p = 2*dx - dy;
    incE = 2*dx;
    incNE = 2*(dx-dy);
    while (y != y1){
      y = y + stepy;
      stepper2.step(stepy);
      nStepY = nStepY + stepy;
    
      if (p < 0){
        p = p + incE;
      }
      else {
        x = x + stepx;
        stepper1.step(stepx);
        nStepX = nStepX + stepx;
        p = p + incNE;
      }
      //g.drawLine( x0, y0, x0, y0);
    }
  }
}






No dejeis de visitar http://www.negadeth.es

No hay comentarios:

Publicar un comentario