ACESSE www.fisicarduino.com

Este projeto usa um controle de playstation 2 – PS2 – Para controlar dois servo motores que estão conectados a uma webcam. Através do controle do ps2 podemos controlar a camera.

Para baixar os código fonte clique no link ao lado: PS2 – Controller – Servo – Arduino

Foto do projeto:

Assista ao vídeo!

Veja o código fonte!

/*  PSX Controller Decoder Library (Psx.pde)
	Written by: Kevin Ahrendt June 22nd, 2008

	Controller protocol implemented using Andrew J McCubbin's analysis.

http://www.gamesx.com/controldata/psxcont/psxcont.htm

	Shift command is based on tutorial examples for ShiftIn and ShiftOut
	functions both written by Carlyn Maw and Tom Igoe

http://www.arduino.cc/en/Tutorial/ShiftIn


http://www.arduino.cc/en/Tutorial/ShiftOut

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

// www.fisicarduino.wordpress.com

#include <Servo.h>
#include <WProgram.h>
#include <Psx.h>

#define dataPin 2
#define cmndPin 3
#define attPin 4
#define clockPin 5

#define LEDPin 13

Servo x_servo, y_servo; // Cria dois objetos do tipo Servo.

Psx Psx;                                                  // Initializes the library

unsigned int data = 0;                                    // data stores the controller response
float x = 1;
float y = 1;

void setup()
{
  Psx.setupPins(dataPin, cmndPin, attPin, clockPin, 50);  // Defines what each pin is used
                                                          // (Data Pin #, Cmnd Pin #, Att Pin #, Clk Pin #, Delay)
                                                          // Delay measures how long the clock remains at each state,
                                                          // measured in microseconds.

  x_servo.attach(9); // Anexa o objeto x_servo ao pino 9 do arduino.
  y_servo.attach(10); // Anexa o objeto y_servo ao pino 10 do arduino.
  x_servo.write(0); // posiciona x_servo no angulo zero grau.
  y_servo.write(0); // posiciona y_servo no angulo zero grau.

  pinMode(LEDPin, OUTPUT);                                // Establishes LEDPin as an output so the LED can be seen
}

void loop()
{
  data = Psx.read();                                      // Psx.read() initiates the PSX controller and returns
                                                          // the button data

  if (data & psxDown)
  {
    digitalWrite(LEDPin, HIGH);     // If button is pressed, turn on the LED
    y+=0.4;
  } else if (data & psxUp)

  {
    digitalWrite(LEDPin, HIGH);     // If button is pressed, turn on the LED
   y-=0.4;
  } else if (data & psxLeft)
  {
    digitalWrite(LEDPin, HIGH);     // If button is pressed, turn on the LED
    x-=0.4;
  } else if (data & psxRight)
  {
    digitalWrite(LEDPin, HIGH);     // If button is pressed, turn on the LED
    x+=0.4;
  } else
  {
    digitalWrite(LEDPin, LOW);                            // If the button isn't pressed, turn off the LED
  }

  if (y > 180) y=180;
  if (y < 0) y=0;
  if (x > 180) x=180;
  if (x < 0) x=0;
  x_servo.write(x);
  y_servo.write(y);
  delay(10);
}