Mira Laser
Autor: Fellipe Couto | Publicado: 2014-05-05T18:24:00-03:00




E o laser não erra! Projeto simples, mas muito interessante!
As posições são gravadas manualmente através de dois potenciômetros. Após isso, o robô mira laser trabalha sozinho. O movimento é feito por dois servo motores.

Já pensou que é essa a base de funcionamento de um robô de solda?! Com criatividade, a partir desse projeto podemos ter vários outros!

Segue o esquema elétrico do circuito e o código fonte.

Valeu! Abraços!







/*-----------------------
&nbsp;Programa : MIRA LASER
&nbsp;Autor&nbsp;&nbsp;&nbsp; : Fellipe Couto
&nbsp;Data&nbsp;&nbsp;&nbsp;&nbsp; : 03/05/2014
&nbsp;-----------------------*/

#include &lt;Servo.h&gt; //Biblioteca do Servo Motor
#define servo1 6 //Servo motor 1 no pino 6
#define servo2 7 //Servo motor 2 no pino 7
#define laser 8 //Laser no pino 8
#define potX A0 //Potenciometro 1 (right / left) ligado na entrada analogica A0
#define potY A1 //Potenciometro 2 (up / down) ligado na entrada analogica A1
#define bRec A2 //Botao para gravar as posicoes ligado na entrada analogica A2, com resistor de pull-down 10K

Servo servoRightLeft; //Varíavel do servo motor para direita e esquerda
Servo servoUpDown; //Varíavel do servo motor para cima e para baixo

int pX[100] = {}; //Array com as posicoes gravadas de right/left
int pY[100] = {}; //Array com as posicoes gravadas de up/down
int qtdRec = 0; //Quantidade de posicoes gravadas
boolean rec = true;

/*----- SETUP -----*/
void setup() {
&nbsp; //Configura o pino do laser
&nbsp; pinMode(laser, OUTPUT);

&nbsp; //Configura os pinos dos servos
&nbsp; servoRightLeft.attach(servo1);
&nbsp; servoUpDown.attach(servo2);
}

/*----- LOOP PRINCIPAL -----*/
void loop() {
&nbsp; if (rec == true) {
&nbsp;&nbsp;&nbsp; target();
&nbsp; } else {
&nbsp;&nbsp;&nbsp; autoLaser();
&nbsp; }
}

/*----- LASER MANUAL PARA GRAVAR OS ALVOS -----*/
void target() {
&nbsp; //Laser ligado
&nbsp; digitalWrite(laser, HIGH);

&nbsp; //Posicoes do potenciometro
&nbsp; int x = map(analogRead(potX), 0, 1023, 0, 180);
&nbsp; int y = map(analogRead(potY), 0, 1023, 0, 180);

&nbsp; //Posiciona os servos
&nbsp; servoRightLeft.write(x);
&nbsp; servoUpDown.write(y);

&nbsp; //Se o botao for pressionado, grava as posicoes
&nbsp; if (analogRead(bRec) == 1023) {
&nbsp;&nbsp;&nbsp; qtdRec++;
&nbsp;&nbsp;&nbsp; pX[qtdRec] = x;
&nbsp;&nbsp;&nbsp; pY[qtdRec] = y;
&nbsp;&nbsp;&nbsp; blinkLaser();

&nbsp;&nbsp;&nbsp; //Se o botao permanecer pressionado apos o laser piscar, inicia a mira automatica
&nbsp;&nbsp;&nbsp; if (analogRead(bRec) == 1023) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rec = false;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delay(2000);
&nbsp;&nbsp;&nbsp; }
&nbsp; }
}

/*----- LASER AUTOMATICO -----*/
void autoLaser() {
&nbsp; for (int i = 1; i &lt;= qtdRec; i++) {
&nbsp;&nbsp;&nbsp; //Posiciona os servos
&nbsp;&nbsp;&nbsp; servoRightLeft.write(pX[i]);
&nbsp;&nbsp;&nbsp; servoUpDown.write(pY[i]);
&nbsp;&nbsp;&nbsp; delay(500);

&nbsp;&nbsp;&nbsp; //Pisca o laser
&nbsp;&nbsp;&nbsp; blinkLaser();

&nbsp;&nbsp;&nbsp; //Se o botao permanecer pressionado apos o laser piscar, finaliza a mira automatica
&nbsp;&nbsp;&nbsp; if (analogRead(bRec) == 1023) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rec = true;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i = qtdRec + 1;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delay(2000);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Se o botao permanecer pressionado após o delay, não mantem as posicoes que ja estavam gravadas
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (analogRead(bRec) == 1023) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; qtdRec = 0;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digitalWrite(laser, HIGH);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delay(1000);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; digitalWrite(laser, LOW);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; delay(2000);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; }
&nbsp; }
}

/*----- PISCA O LASER -----*/
void blinkLaser() {
&nbsp; for (int i = 0; i &lt; 15; i++) {
&nbsp;&nbsp;&nbsp; digitalWrite(laser, HIGH);
&nbsp;&nbsp;&nbsp; delay(50);
&nbsp;&nbsp;&nbsp; digitalWrite(laser, LOW);
&nbsp;&nbsp;&nbsp; delay(50);
&nbsp; }
}