PS1コントローラーでサーボを動かす

#include <Psx.h>                     // Includes the Psx Library 

#include <Servo.h>

                          

#define dataPin 12

#define cmndPin 11

#define attPin 10

#define clockPin 13

 

#define LEDPin A1

 

Servo myservoA; // create servo object to control a servo

Servo myservoB; // create servo object to control a servo

 

Psx Psx;                         // Initializes the library

 

unsigned int data = 0;                  // data stores the controller response

int degA = 90;

int degB = 90;

int deg = 5;    //servo回転角

 

void setup()

{

 myservoA.attach(7); // attaches the servo on pin 7 to the servo object

 myservoB.attach(8); // attaches the servo on pin 8 to the servo object

 Psx.setupPins(dataPin, cmndPin, attPin, clockPin, 10); // 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.

                             // too small delay may not work (under 5)

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

 Serial.begin(9600);

}

 

void loop()

{

 data = Psx.read();                   

 Serial.println(data);                      // Display the returned numeric value

 if (data & psxUp)                        // If the data anded with a button's hex value is true,

 {                                                     // button can be found in Psx.h

  digitalWrite(LEDPin, HIGH);             // If button is pressed, turn on the LED 

  degA = min(degA + deg,180);

  myservoA.write(degA);

  delay(10);

 }

 else if (data & psxDown)

 {

  digitalWrite(LEDPin, HIGH);

  degA = max(degA - deg,0);

  myservoA.write(degA);

  delay(10);

 }

 else if (data & psxLeft)

 {

  digitalWrite(LEDPin, HIGH);

  degB = min(degB + deg,180);

  myservoB.write(degB);

  delay(10);

 }

 else if (data & psxRight)

 {

  digitalWrite(LEDPin, HIGH);

  degB = max(degB - deg,0);

  myservoB.write(degB);

  delay(10);

 }

  

 else

 {

  digitalWrite(LEDPin, LOW);              // If the button isn't pressed, turn off the LED

 }

 delay(20);

}

サイトメニュー

ホーム

製品情報

ニュース

掲示板

当研究所について

 

桐朋電子研究所

〒186-0004

国立市中3-1-10 桐朋中学校高等学校 会議室D

(当「研究所」は、学園祭の有志参加団体です)

電子研公式 LINE

電子研ツイッター

電子研オフィシャルブログ