Prvni commit.
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
#include <Servo.h>
|
||||
Servo myservo1; // create servo object to control a servo
|
||||
Servo myservo2;
|
||||
Servo myservo3;
|
||||
Servo myservo4;
|
||||
int pos1=90, pos2=90, pos3=90, pos4=90; //define the variable of 4 servo angle and assign the initial value for installing
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
myservo1.attach(3); // set the control pin of servo 1 to 3 digital I/0
|
||||
myservo2.attach(5); // set the control pin of servo 2 to 5 digital I/0
|
||||
myservo3.attach(6); // set the control pin of servo 3 to 6 digital I/0
|
||||
myservo4.attach(9); // set the control pin of servo 4 to 9 digital I/0
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
myservo1.write(pos1); // Control servo motor rotation to specified angle
|
||||
myservo2.write(pos2);
|
||||
myservo3.write(pos3);
|
||||
myservo4.write(pos4);
|
||||
delay(1000);
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
#include <Servo.h>
|
||||
Servo myservo1; // create servo object to control a servo
|
||||
Servo myservo2;
|
||||
Servo myservo3;
|
||||
Servo myservo4;
|
||||
int pos1=90, pos2=90, pos3=90, pos4=90;
|
||||
|
||||
void setup()
|
||||
{
|
||||
myservo1.attach(3); // set the control pin of servo 1 to 3 digital I/0
|
||||
myservo2.attach(5); // set the control pin of servo 1 to 3 digital I/0
|
||||
myservo3.attach(6); // set the control pin of servo 1 to 3 digital I/0
|
||||
myservo4.attach(9); // set the control pin of servo 1 to 3 digital I/0
|
||||
|
||||
myservo1.write(pos1);
|
||||
myservo2.write(pos2);
|
||||
myservo3.write(pos3);
|
||||
myservo4.write(pos4);
|
||||
delay(1500);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
// close the claw
|
||||
for(pos4;pos4>45;pos4--)
|
||||
{
|
||||
myservo4.write(pos4);
|
||||
}
|
||||
delay(1000);
|
||||
|
||||
// open the claw
|
||||
for(pos4;pos4<120;pos4++)
|
||||
{
|
||||
myservo4.write(pos4);
|
||||
}
|
||||
delay(1000);
|
||||
|
||||
|
||||
// turn right
|
||||
for(pos1;pos1>30;pos1--)
|
||||
{
|
||||
myservo1.write(pos1);
|
||||
delay(5); // delay 5ms(used to adjust the servo speed)
|
||||
}
|
||||
delay(1000);
|
||||
|
||||
|
||||
|
||||
// turn to left
|
||||
for(pos1;pos1<150;pos1++)
|
||||
{
|
||||
myservo1.write(pos1);
|
||||
delay(5);
|
||||
}
|
||||
delay(1000);
|
||||
|
||||
// stretch out the arm
|
||||
for(pos2;pos2<130;pos2++)
|
||||
{
|
||||
myservo2.write(pos2);
|
||||
delay(5);
|
||||
}
|
||||
delay(1000);
|
||||
|
||||
|
||||
// retracte the arm
|
||||
for(pos2;pos2>80;pos2--)
|
||||
{
|
||||
myservo2.write(pos2);
|
||||
delay(5);
|
||||
}
|
||||
delay(1000);
|
||||
|
||||
|
||||
|
||||
|
||||
// raise the arm
|
||||
for(pos3;pos3<100;pos3++)
|
||||
{
|
||||
myservo3.write(pos3);
|
||||
delay(5);
|
||||
}
|
||||
delay(1500);
|
||||
|
||||
// Lower the arm
|
||||
for(pos3;pos3>40;pos3--)
|
||||
{
|
||||
myservo3.write(pos3);
|
||||
delay(5);
|
||||
}
|
||||
delay(1000);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
#include <Servo.h>
|
||||
Servo myservo; // create servo object to control a servo
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
myservo.attach(3); // modify each pin to adjust
|
||||
//myservo.write(0); // the servo will automatically rotate to 0°
|
||||
//delay(1000);
|
||||
myservo.write(90); // the servo will automatically rotate to 90°
|
||||
//myservo.write(180); // the servo will automatically rotate to 180°
|
||||
delay(1000);
|
||||
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
const int right_X = A2; // define the right X pin to A2
|
||||
const int right_Y = A5; // define the right Y pin to A5
|
||||
const int right_key = 7; //define the right key pin to 7(that is the value Z)
|
||||
|
||||
const int left_X = A3; //define the left X pin to A3
|
||||
const int left_Y = A4; // define the left Y pin to A4
|
||||
const int left_key = 8; //define the left key pin to 8(that is the value Z)
|
||||
|
||||
void setup()
|
||||
{
|
||||
pinMode(right_key, INPUT); // set the right/left key to INPUT
|
||||
pinMode(left_key, INPUT);
|
||||
Serial.begin(9600); // set the baud rate to 9600
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
int x1,y1,z1; // define the variable, used to save the joystick value it reads
|
||||
int x2,y2,z2;
|
||||
|
||||
x2 = analogRead(right_X); // read the value of right X
|
||||
y2 = analogRead(right_Y); // read the value of right Y
|
||||
z2 = digitalRead(right_key); //// read the value of right Z
|
||||
|
||||
x1 = analogRead(left_X); // read the value of left X
|
||||
y1 = analogRead(left_Y); // read the value of left Y
|
||||
z1 = digitalRead(left_key); // read the value of left Z
|
||||
|
||||
Serial.println("#####right_joystick########");
|
||||
Serial.print("right_X = "); // on the serial monitor, print out right_X =
|
||||
Serial.println(x2 ,DEC); // print out the value of right X and line wrap
|
||||
Serial.print("right_Y = ");
|
||||
Serial.println(y2 ,DEC);
|
||||
//Serial.print("right_key = ");
|
||||
//Serial.println(z2 ,DEC);
|
||||
Serial.println("##########################");
|
||||
Serial.println("#####left_joystick########");
|
||||
Serial.print("left_X = ");
|
||||
Serial.println(x1 ,DEC);
|
||||
Serial.print("left_Y = ");
|
||||
Serial.println(y1 ,DEC);
|
||||
//Serial.print("left_key = ");
|
||||
//Serial.println(z1 ,DEC);
|
||||
Serial.println("##########################");
|
||||
delay(2000);
|
||||
}
|
||||
+179
@@ -0,0 +1,179 @@
|
||||
#include <Servo.h> // add the servo libraries
|
||||
Servo myservo1; // create servo object to control a servo
|
||||
Servo myservo2;
|
||||
Servo myservo3;
|
||||
Servo myservo4;
|
||||
int pos1=90, pos2=90, pos3=90, pos4=90; // define the variable of 4 servo angle,and assign the initial value (that is the boot posture
|
||||
//angle value)
|
||||
const int right_X = A2; // define the right X pin to A2
|
||||
const int right_Y = A5; // define the right Y pin to A5
|
||||
const int right_key = 7; // define the right key pin to 7(that is the value of Z)
|
||||
const int left_X = A3; // define the left X pin to A3
|
||||
const int left_Y = A4; // define the left X pin to A4
|
||||
const int left_key = 8; //define the left key pin to 8(that is the value of Z)
|
||||
int x1,y1,z1; // define the variable, used to save the joystick value it read.
|
||||
int x2,y2,z2;
|
||||
|
||||
void setup()
|
||||
{
|
||||
// boot posture
|
||||
myservo1.write(pos1);
|
||||
delay(1000);
|
||||
myservo2.write(pos2);
|
||||
myservo3.write(pos3);
|
||||
myservo4.write(pos4);
|
||||
delay(1500);
|
||||
pinMode(right_key, INPUT); // set the right/left key to INPUT
|
||||
pinMode(left_key, INPUT);
|
||||
Serial.begin(9600); // set the baud rate to 9600
|
||||
}
|
||||
void loop()
|
||||
{
|
||||
myservo1.attach(3); // set the control pin of servo 1 to D3 dizuo-servo1-3
|
||||
myservo2.attach(5); // set the control pin of servo 2 to D5 arm-servo2-5
|
||||
myservo3.attach(6); //set the control pin of servo 3 to D6 lower arm-servo-6
|
||||
myservo4.attach(9); // set the control pin of servo 4 to D9 claw-servo-9
|
||||
x2 = analogRead(right_X); //read the right X value
|
||||
y2 = analogRead(right_Y); // read the right Y value
|
||||
z2 = digitalRead(right_key); //// read the right Z value
|
||||
x1 = analogRead(left_X); //read the left X value
|
||||
y1 = analogRead(left_Y); //read the left Y value
|
||||
z1 = digitalRead(left_key); // read the left Z value
|
||||
//delay(5); // lower the speed overall
|
||||
|
||||
// claw
|
||||
claw();
|
||||
|
||||
// rotate
|
||||
turn();
|
||||
|
||||
// upper arm
|
||||
upper_arm();
|
||||
|
||||
//lower arm
|
||||
lower_arm();
|
||||
}
|
||||
|
||||
|
||||
|
||||
//***************************************************
|
||||
//claw
|
||||
void claw()
|
||||
{
|
||||
//claw
|
||||
if(x1<50) // if push the left joystick to the right
|
||||
{
|
||||
pos4=pos4+3;
|
||||
myservo4.write(pos4); //servo 4 operates the motion, the claw gradually opens.
|
||||
delay(5);
|
||||
|
||||
if(pos4>120) //limit the largest angle when open the claw
|
||||
{
|
||||
pos4=120;
|
||||
}
|
||||
}
|
||||
|
||||
if(x1>1000) ////if push the right joystick to the left
|
||||
{
|
||||
pos4=pos4-3;
|
||||
myservo4.write(pos4); // servo 4 operates the action, claw is gradually closed.
|
||||
delay(5);
|
||||
if(pos4<45) //
|
||||
{
|
||||
pos4=45; //limit the largest angle when close the claw
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//******************************************************/
|
||||
// turn
|
||||
void turn()
|
||||
{
|
||||
if(x2<50) //if push the right joystick to the let
|
||||
{
|
||||
pos1=pos1+3;
|
||||
myservo1.write(pos1); // arm turns left
|
||||
delay(5);
|
||||
if(pos1>180) //limit the angle when turn right
|
||||
{
|
||||
pos1=180;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(x2>1000) // if push the right joystick to the right
|
||||
{
|
||||
pos1=pos1-3;
|
||||
myservo1.write(pos1); //servo 1 operates the motion, the arm turns right.
|
||||
delay(5);
|
||||
if(pos1<1) // limit the angle when turn left
|
||||
{
|
||||
pos1=1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//**********************************************************/
|
||||
// lower arm
|
||||
void lower_arm()
|
||||
{
|
||||
if(y2>1000) // if push the right joystick downward
|
||||
{
|
||||
pos2=pos2-2;
|
||||
myservo2.write(pos2); // lower arm will draw back
|
||||
delay(5);
|
||||
if(pos2<25) // limit the retracted angle
|
||||
{
|
||||
pos2=25;
|
||||
}
|
||||
}
|
||||
|
||||
if(y2<50) // if push the right joystick upward
|
||||
{
|
||||
pos2=pos2+2;
|
||||
myservo2.write(pos2); // lower arm will stretch out
|
||||
delay(5);
|
||||
if(pos2>180) // limit the stretched angle
|
||||
{
|
||||
pos2=180;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//*************************************************************/
|
||||
|
||||
//upper arm
|
||||
void upper_arm()
|
||||
{
|
||||
if(y1<50) // if push the left joystick downward
|
||||
{
|
||||
pos3=pos3-2;
|
||||
myservo3.write(pos3); // upper arm will go down
|
||||
delay(5);
|
||||
if(pos3<1) // limit the angle when go down
|
||||
{
|
||||
pos3=1;
|
||||
}
|
||||
}
|
||||
if(y1>1000) // if push the left joystick upward
|
||||
{
|
||||
pos3=pos3+2;
|
||||
myservo3.write(pos3); // the upper arm will lift
|
||||
delay(5);
|
||||
|
||||
if(pos3>135) //limit the lifting angle
|
||||
{
|
||||
pos3=135;
|
||||
}
|
||||
}
|
||||
}
|
||||
+186
@@ -0,0 +1,186 @@
|
||||
#include <Servo.h> // add the servo libraries
|
||||
Servo myservo1; // create servo object to control a servo
|
||||
Servo myservo2;
|
||||
Servo myservo3;
|
||||
Servo myservo4;
|
||||
int pos1=90, pos2=90, pos3=90, pos4=90; // define the variable of 4 servo angle and assign the initial value( that is the boot posture angle value)
|
||||
char val;
|
||||
int incomingByte = 0; // Received data byte
|
||||
String inputString = ""; // Used to store received content
|
||||
boolean newLineReceived = false; // Previous data end flag
|
||||
boolean startBit = false; //Acceptance Agreement Start Sign
|
||||
int num_reveice=0;
|
||||
void setup()
|
||||
{
|
||||
// boot posture
|
||||
myservo1.write(pos1);
|
||||
delay(1000);
|
||||
myservo2.write(pos2);
|
||||
myservo3.write(pos3);
|
||||
myservo4.write(pos4);
|
||||
delay(1500);
|
||||
|
||||
Serial.begin(9600); // set the baud rate to 9600
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
myservo1.attach(3); // set the control pin of servo 1 to D3
|
||||
myservo2.attach(5); // set the control pin of servo 2 to D5
|
||||
myservo3.attach(6); // set the control pin of servo 3 to D6
|
||||
myservo4.attach(9); // set the control pin of servo 4 to D9
|
||||
|
||||
while (Serial.available())
|
||||
{
|
||||
incomingByte = Serial.read(); //One byte by byte, the next sentence is read into a string array to form a completed packet
|
||||
if (incomingByte == '%')
|
||||
{
|
||||
num_reveice = 0;
|
||||
startBit = true;
|
||||
}
|
||||
if (startBit == true)
|
||||
{
|
||||
num_reveice++;
|
||||
inputString += (char) incomingByte;
|
||||
}
|
||||
if (startBit == true && incomingByte == '#')
|
||||
{
|
||||
newLineReceived = true;
|
||||
startBit = false;
|
||||
}
|
||||
|
||||
if(num_reveice >= 20)
|
||||
{
|
||||
num_reveice = 0;
|
||||
startBit = false;
|
||||
newLineReceived = false;
|
||||
inputString = "";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if(newLineReceived)
|
||||
{
|
||||
switch(inputString[1])
|
||||
{
|
||||
case 'B': T_left(); break; // turn left
|
||||
case 'C': T_right(); break;//turn right
|
||||
case 'A': RB(); break;// the lower arm will draw back
|
||||
case 'D': RF(); break;// the lower arm will stretch out
|
||||
case '5': ZK(); break;//close the claw
|
||||
case '6': ZB(); break;//close the claw
|
||||
case '4': LB(); break;//the upper arm will lift up
|
||||
case '7': LF(); break;//the upper arm will go down
|
||||
default:break;
|
||||
}
|
||||
|
||||
inputString = ""; // clear the string
|
||||
newLineReceived = false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
//**************************************************
|
||||
// turn left
|
||||
void T_left()
|
||||
{
|
||||
pos1=pos1+8;
|
||||
myservo1.write(pos1);
|
||||
delay(5);
|
||||
if(pos1>180)
|
||||
{
|
||||
pos1=180;
|
||||
}
|
||||
}
|
||||
//turn right
|
||||
void T_right()
|
||||
{
|
||||
pos1=pos1-8;
|
||||
myservo1.write(pos1);
|
||||
delay(5);
|
||||
if(pos1<1)
|
||||
{
|
||||
pos1=1;
|
||||
}
|
||||
}
|
||||
//********************************************
|
||||
//close the claw
|
||||
|
||||
void ZK()
|
||||
{
|
||||
pos4=pos4-8;
|
||||
Serial.println(pos4);
|
||||
myservo4.write(pos4);
|
||||
delay(5);
|
||||
if(pos4<45)
|
||||
{
|
||||
pos4=45;
|
||||
}
|
||||
}
|
||||
// open the claw
|
||||
void ZB()
|
||||
{
|
||||
pos4=pos4+8;
|
||||
Serial.println(pos4);
|
||||
myservo4.write(pos4);
|
||||
delay(5);
|
||||
if(pos4>120)
|
||||
{
|
||||
pos4=120;
|
||||
}
|
||||
}
|
||||
|
||||
//******************************************
|
||||
// the lower arm will stretch out
|
||||
void RF()
|
||||
{
|
||||
pos2=pos2-8;
|
||||
myservo2.write(pos2);
|
||||
delay(5);
|
||||
if(pos2<25)
|
||||
{
|
||||
pos2=25;
|
||||
}
|
||||
}
|
||||
// the lower arm will draw back
|
||||
void RB()
|
||||
{
|
||||
pos2=pos2+8;
|
||||
myservo2.write(pos2);
|
||||
delay(5);
|
||||
if(pos2>180)
|
||||
{
|
||||
pos2=180;
|
||||
}
|
||||
}
|
||||
|
||||
//***************************************
|
||||
//the upper arm will lift up
|
||||
void LB()
|
||||
{
|
||||
pos3=pos3+8;
|
||||
myservo3.write(pos3);
|
||||
delay(5);
|
||||
if(pos3>135)
|
||||
{
|
||||
pos3=135;
|
||||
}
|
||||
}
|
||||
|
||||
//the upper arm will go down
|
||||
|
||||
void LF()
|
||||
{
|
||||
pos3=pos3-8;
|
||||
myservo3.write(pos3);
|
||||
delay(5);
|
||||
if(pos3<0)
|
||||
{
|
||||
pos3=0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
int pos1=90, pos2=90, pos3=90, pos4=90; // define the variable of 4 servo angle and assign the initial value( that is the boot posture angle value)
|
||||
char val;
|
||||
int incomingByte = 0; // Received data byte
|
||||
String inputString = ""; // Used to store received content
|
||||
boolean newLineReceived = false; // Previous data end flag
|
||||
boolean startBit = false; //Acceptance Agreement Start Sign
|
||||
int num_reveice=0;
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600); // set the baud rate to 9600
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
while (Serial.available())
|
||||
{
|
||||
incomingByte = Serial.read(); //One byte by byte, the next sentence is read into a string array to form a completed packet
|
||||
if (incomingByte == '%')
|
||||
{
|
||||
num_reveice = 0;
|
||||
startBit = true;
|
||||
}
|
||||
if (startBit == true)
|
||||
{
|
||||
num_reveice++;
|
||||
inputString += (char) incomingByte;
|
||||
}
|
||||
if (startBit == true && incomingByte == '#')
|
||||
{
|
||||
newLineReceived = true;
|
||||
startBit = false;
|
||||
}
|
||||
|
||||
if(num_reveice >= 20)
|
||||
{
|
||||
num_reveice = 0;
|
||||
startBit = false;
|
||||
newLineReceived = false;
|
||||
inputString = "";
|
||||
}
|
||||
}
|
||||
|
||||
if(newLineReceived)
|
||||
{
|
||||
Serial.println(inputString);
|
||||
inputString = ""; // clear the string
|
||||
newLineReceived = false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,279 @@
|
||||
|
||||
#include <PS2X_lib.h>
|
||||
|
||||
PS2X ps2x; // create PS2 Controller Class
|
||||
|
||||
|
||||
//right now, the library does NOT support hot pluggable controllers, meaning
|
||||
//you must always either restart your Arduino after you connect the controller,
|
||||
//or call config_gamepad(pins) again after connecting the controller.
|
||||
int error = 0;
|
||||
byte vibrate = 0;
|
||||
|
||||
#include <Servo.h> // add the servo libraries
|
||||
Servo myservo1; // create servo object to control a servo
|
||||
Servo myservo2;
|
||||
Servo myservo3;
|
||||
Servo myservo4;
|
||||
|
||||
int pos1=90, pos2=90, pos3=90, pos4=90; // define the variable of 4 servo angle and assign the initial value( that is the boot posture angle value)
|
||||
|
||||
void setup(){
|
||||
Serial.begin(57600);
|
||||
|
||||
// boot posture
|
||||
myservo1.write(pos1);
|
||||
delay(1000);
|
||||
myservo2.write(pos2);
|
||||
myservo3.write(pos3);
|
||||
myservo4.write(pos4);
|
||||
delay(1500);
|
||||
|
||||
error = ps2x.config_gamepad(13,11,10,12); //setup GamePad(clock, command, attention, data) pins, check for error
|
||||
|
||||
if(error == 0){
|
||||
Serial.println("Found Controller, configured successful");
|
||||
Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
|
||||
Serial.println("holding L1 or R1 will print out the analog stick values.");
|
||||
Serial.println("Go to www.billporter.info for updates and to report bugs.");
|
||||
}
|
||||
|
||||
else if(error == 1)
|
||||
Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
|
||||
|
||||
else if(error == 2)
|
||||
Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
|
||||
|
||||
//Serial.print(ps2x.Analog(1), HEX);
|
||||
|
||||
|
||||
ps2x.enableRumble(); //enable rumble vibration motors
|
||||
ps2x.enablePressures(); //enable reading the pressure values from the buttons.
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
void loop(){
|
||||
/* You must Read Gamepad to get new values
|
||||
Read GamePad and set vibration values
|
||||
ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
|
||||
if you don't enable the rumble, use ps2x.read_gamepad(); with no values
|
||||
|
||||
you should call this at least once a second
|
||||
*/
|
||||
|
||||
myservo1.attach(3); // set the control pin of servo 1 to A1
|
||||
myservo2.attach(5); // set the control pin of servo 2 to A0
|
||||
myservo3.attach(6); // set the control pin of servo 3 to D6
|
||||
myservo4.attach(9); // set the control pin of servo 4 to D9
|
||||
|
||||
if(error != 0)
|
||||
return;
|
||||
|
||||
ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed
|
||||
|
||||
if(ps2x.Button(PSB_START)) //will be TRUE as long as button is pressed
|
||||
Serial.println("Start is being held");
|
||||
if(ps2x.Button(PSB_SELECT))
|
||||
Serial.println("Select is being held");
|
||||
|
||||
|
||||
if(ps2x.Button(PSB_PAD_UP)) { //will be TRUE as long as button is pressed
|
||||
Serial.print("Up held this hard: ");
|
||||
Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
|
||||
}
|
||||
if(ps2x.Button(PSB_PAD_RIGHT)){
|
||||
Serial.print("Right held this hard: ");
|
||||
Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC);
|
||||
}
|
||||
if(ps2x.Button(PSB_PAD_LEFT)){
|
||||
Serial.print("LEFT held this hard: ");
|
||||
Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC);
|
||||
}
|
||||
if(ps2x.Button(PSB_PAD_DOWN)){
|
||||
Serial.print("DOWN held this hard: ");
|
||||
Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC);
|
||||
}
|
||||
|
||||
|
||||
vibrate = ps2x.Analog(PSAB_BLUE); //this will set the large motor vibrate speed based on
|
||||
//how hard you press the blue (X) button
|
||||
|
||||
if (ps2x.NewButtonState()) //will be TRUE if any button changes state (on to off, or off to on)
|
||||
{
|
||||
if(ps2x.Button(PSB_R3))
|
||||
|
||||
Serial.println("R3 pressed");
|
||||
|
||||
|
||||
if(ps2x.Button(PSB_L3))
|
||||
Serial.println("L3 pressed");
|
||||
|
||||
if(ps2x.Button(PSB_L2))
|
||||
Serial.println("L2 pressed");
|
||||
if(ps2x.Button(PSB_R2))
|
||||
Serial.println("R2 pressed");
|
||||
if(ps2x.Button(PSB_GREEN))
|
||||
Serial.println("Triangle pressed");
|
||||
|
||||
}
|
||||
|
||||
|
||||
if(ps2x.ButtonPressed(PSB_RED)) //will be TRUE if button was JUST pressed
|
||||
Serial.println("Circle just pressed");
|
||||
|
||||
if(ps2x.ButtonReleased(PSB_PINK)) //will be TRUE if button was JUST released
|
||||
Serial.println("Square just released");
|
||||
|
||||
if(ps2x.NewButtonState(PSB_BLUE)) //will be TRUE if button was JUST pressed OR released
|
||||
Serial.println("X just changed");
|
||||
|
||||
// claw
|
||||
claw();
|
||||
|
||||
// rotate
|
||||
turn();
|
||||
|
||||
// upper arm
|
||||
upper_arm();
|
||||
|
||||
//lower arm
|
||||
lower_arm();
|
||||
|
||||
if(ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) // print stick values if either is TRUE
|
||||
{
|
||||
Serial.print("Stick Values:");
|
||||
Serial.print(ps2x.Analog(PSS_LY), DEC); //Left stick, Y axis. Other options: LX, RY, RX
|
||||
Serial.print(",");
|
||||
Serial.print(ps2x.Analog(PSS_LX), DEC);
|
||||
Serial.print(",");
|
||||
Serial.print(ps2x.Analog(PSS_RY), DEC);
|
||||
Serial.print(",");
|
||||
Serial.println(ps2x.Analog(PSS_RX), DEC);
|
||||
}
|
||||
delay(5);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//***************************************************
|
||||
//claw
|
||||
void claw()
|
||||
{
|
||||
//claw
|
||||
if(ps2x.Analog(PSS_LX)<10) // if push the left joystick to the right
|
||||
{
|
||||
pos4=pos4-3;
|
||||
myservo4.write(pos4); //servo 4 operates the motion, the claw gradually opens.
|
||||
delay(5);
|
||||
if(pos4<45) // limit the largest angle when open the claw
|
||||
|
||||
pos4=45;
|
||||
}
|
||||
|
||||
|
||||
if(ps2x.Analog(PSS_LX)>220) ////if push the right joystick to the left
|
||||
{
|
||||
pos4=pos4+3;
|
||||
myservo4.write(pos4); // servo 4 operates the action, claw is gradually closed.
|
||||
delay(5);
|
||||
if(pos4>120) // limit the largest angle when close the claw
|
||||
{
|
||||
pos4=120;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//******************************************************/
|
||||
// turn
|
||||
void turn()
|
||||
{
|
||||
if(ps2x.Analog (PSS_RX) < 50) //if push the right joystick to the let
|
||||
{
|
||||
pos1=pos1+3;
|
||||
myservo1.write(pos1); // arm turns left
|
||||
delay(5);
|
||||
if(pos1>180) //limit the angle when turn right
|
||||
{
|
||||
pos1=180;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(ps2x.Analog (PSS_RX) > 200) // if push the right joystick to the right
|
||||
{
|
||||
pos1=pos1-3;
|
||||
myservo1.write(pos1); //servo 1 operates the motion, the arm turns right.
|
||||
delay(5);
|
||||
if(pos1<1) // limit the angle when turn left
|
||||
{
|
||||
pos1=1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//**********************************************************/
|
||||
// lower arm
|
||||
void lower_arm()
|
||||
{
|
||||
if(ps2x.Analog(PSS_RY)>200) // if push the right joystick downward
|
||||
{
|
||||
pos2=pos2-2;
|
||||
myservo2.write(pos2); // lower arm will draw back
|
||||
delay(5);
|
||||
if(pos2<25) // limit the retracted angle
|
||||
{
|
||||
pos2=25;
|
||||
}
|
||||
}
|
||||
|
||||
if(ps2x.Analog(PSS_RY)<50) // if push the right joystick upward
|
||||
{
|
||||
pos2=pos2+2;
|
||||
myservo2.write(pos2); // lower arm will stretch out
|
||||
delay(5);
|
||||
if(pos2>180) // limit the stretched angle
|
||||
{
|
||||
pos2=180;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//*************************************************************/
|
||||
|
||||
//upper arm
|
||||
void upper_arm()
|
||||
{
|
||||
if(ps2x.Analog(PSS_LY)<10) // if push the left joystick downward
|
||||
{
|
||||
pos3=pos3+2;
|
||||
myservo3.write(pos3); // upper arm will go down
|
||||
delay(5);
|
||||
if(pos3>135) // limit the angle when go down
|
||||
{
|
||||
pos3=135;
|
||||
}
|
||||
}
|
||||
if(ps2x.Analog(PSS_LY)>200) // if push the left joystick upward
|
||||
{
|
||||
pos3=pos3-2;
|
||||
myservo3.write(pos3); // the upper arm will lift
|
||||
delay(5);
|
||||
if(pos3<1) //limit the lifting angle
|
||||
{
|
||||
pos3=1;
|
||||
}
|
||||
}
|
||||
}
|
||||
+401
@@ -0,0 +1,401 @@
|
||||
|
||||
#include <PS2X_lib.h>
|
||||
|
||||
PS2X ps2x; // create PS2 Controller Class
|
||||
|
||||
|
||||
//right now, the library does NOT support hot pluggable controllers, meaning
|
||||
//you must always either restart your Arduino after you connect the controller,
|
||||
//or call config_gamepad(pins) again after connecting the controller.
|
||||
int error = 0;
|
||||
byte vibrate = 0;
|
||||
int s1,s2,s3,s4;
|
||||
int jiyi1[20]; // define four array, separately used to save the angle of 4 servos.
|
||||
int jiyi2[20];
|
||||
int jiyi3[20];
|
||||
int jiyi4[20];
|
||||
int i=0;
|
||||
int j=0;
|
||||
#include <Servo.h> // add the servo libraries
|
||||
Servo myservo1; // create servo object to control a servo
|
||||
Servo myservo2;
|
||||
Servo myservo3;
|
||||
Servo myservo4;
|
||||
|
||||
int pos1=90, pos2=90, pos3=90, pos4=90; // define the variable of 4 servo angle and assign the initial value( that is the boot posture angle value)
|
||||
|
||||
void setup(){
|
||||
Serial.begin(57600);
|
||||
|
||||
// boot posture
|
||||
myservo1.write(pos1);
|
||||
delay(1000);
|
||||
myservo2.write(pos2);
|
||||
myservo3.write(pos3);
|
||||
myservo4.write(pos4);
|
||||
delay(1500);
|
||||
|
||||
error = ps2x.config_gamepad(13,11,10,12); //setup GamePad(clock, command, attention, data) pins, check for error
|
||||
|
||||
if(error == 0){
|
||||
Serial.println("Found Controller, configured successful");
|
||||
Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
|
||||
Serial.println("holding L1 or R1 will print out the analog stick values.");
|
||||
Serial.println("Go to www.billporter.info for updates and to report bugs.");
|
||||
}
|
||||
|
||||
else if(error == 1)
|
||||
Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
|
||||
|
||||
else if(error == 2)
|
||||
Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
|
||||
|
||||
//Serial.print(ps2x.Analog(1), HEX);
|
||||
|
||||
|
||||
ps2x.enableRumble(); //enable rumble vibration motors
|
||||
ps2x.enablePressures(); //enable reading the pressure values from the buttons.
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
void loop(){
|
||||
/* You must Read Gamepad to get new values
|
||||
Read GamePad and set vibration values
|
||||
ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
|
||||
if you don't enable the rumble, use ps2x.read_gamepad(); with no values
|
||||
|
||||
you should call this at least once a second
|
||||
*/
|
||||
|
||||
myservo1.attach(3); // set the control pin of servo 1 to A1
|
||||
myservo2.attach(5); // set the control pin of servo 2 to A0
|
||||
myservo3.attach(6); // set the control pin of servo 3 to D6
|
||||
myservo4.attach(9); // set the control pin of servo 4 to D9
|
||||
|
||||
if(error != 0)
|
||||
return;
|
||||
|
||||
ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed
|
||||
|
||||
if(ps2x.Button(PSB_START)) //will be TRUE as long as button is pressed
|
||||
Serial.println("Start is being held");
|
||||
if(ps2x.Button(PSB_SELECT))
|
||||
Serial.println("Select is being held");
|
||||
|
||||
|
||||
if(ps2x.Button(PSB_PAD_UP)) { //will be TRUE as long as button is pressed
|
||||
Serial.print("Up held this hard: ");
|
||||
Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
|
||||
}
|
||||
if(ps2x.Button(PSB_PAD_RIGHT)){
|
||||
Serial.print("Right held this hard: ");
|
||||
Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC);
|
||||
}
|
||||
if(ps2x.Button(PSB_PAD_LEFT)){
|
||||
Serial.print("LEFT held this hard: ");
|
||||
Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC);
|
||||
}
|
||||
if(ps2x.Button(PSB_PAD_DOWN)){
|
||||
Serial.print("DOWN held this hard: ");
|
||||
Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC);
|
||||
}
|
||||
|
||||
|
||||
vibrate = ps2x.Analog(PSAB_BLUE); //this will set the large motor vibrate speed based on
|
||||
//how hard you press the blue (X) button
|
||||
if (ps2x.NewButtonState()) //will be TRUE if any button changes state (on to off, or off to on)
|
||||
{
|
||||
if(ps2x.Button(PSB_R3))
|
||||
{
|
||||
//Serial.println("R3 pressed");
|
||||
// record
|
||||
s1=myservo1.read();
|
||||
delay(100);
|
||||
Serial.println(s1);
|
||||
s2=myservo2.read();
|
||||
delay(100);
|
||||
Serial.println(s2);
|
||||
s3=myservo3.read();
|
||||
delay(100);
|
||||
Serial.println(s3);
|
||||
s4=myservo4.read();
|
||||
delay(100);
|
||||
Serial.println(s4);
|
||||
|
||||
jiyi1[i]=s1; // save the servo value read in the array sequentially
|
||||
jiyi2[i]=s2;
|
||||
jiyi3[i]=s3;
|
||||
jiyi4[i]=s4;
|
||||
i++;
|
||||
j=i;
|
||||
// delay(100);
|
||||
Serial.println(i);
|
||||
}
|
||||
|
||||
if(ps2x.Button(PSB_L3))
|
||||
{
|
||||
//Serial.println("L3 pressed");
|
||||
i=0;
|
||||
//执行
|
||||
pos1 = myservo1.read();
|
||||
pos2 = myservo2.read();
|
||||
pos3 = myservo3.read();
|
||||
pos4 = myservo4.read();
|
||||
|
||||
for(int k=0;k<j;k++) //for loop, to execute all the stored actions
|
||||
{
|
||||
if(pos1<jiyi1[k]) //if the current servo 1 angle is less than the value stored in array 1.
|
||||
{
|
||||
while(pos1<jiyi1[k]) //while loop, make servo turn to the position of value stored in the array.
|
||||
{
|
||||
myservo1.write(pos1); // servo 1 executes the action
|
||||
delay(5); // delay 5ms,controlling the rotating speed of servo
|
||||
pos1++;
|
||||
//Serial.println(pos1);
|
||||
}
|
||||
}
|
||||
else //if the current servo 1 angle is greater than the value stored in array 1.
|
||||
{
|
||||
while(pos1>jiyi1[k]) //while loop, make servo turn to the position of value stored in the array.
|
||||
{
|
||||
myservo1.write(pos1); // servo 1 executes the action
|
||||
delay(5); //delay 5ms,controlling the rotating speed of servo
|
||||
pos1--;
|
||||
//Serial.println(pos1);
|
||||
}
|
||||
|
||||
//**********************************************
|
||||
// the same analysis as the previous servo
|
||||
if(pos2<jiyi2[k])
|
||||
{
|
||||
while(pos2<jiyi2[k])
|
||||
{
|
||||
myservo2.write(pos2);
|
||||
delay(5);
|
||||
pos2++;
|
||||
//Serial.println(pos1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while(pos2>jiyi2[k])
|
||||
{
|
||||
myservo2.write(pos2);
|
||||
delay(5);
|
||||
pos2--;
|
||||
//Serial.println(pos1);
|
||||
}
|
||||
}
|
||||
//*****************************************************
|
||||
//the same analysis
|
||||
if(pos3<jiyi3[k])
|
||||
{
|
||||
while(pos3<jiyi3[k])
|
||||
{
|
||||
myservo3.write(pos3);
|
||||
delay(5);
|
||||
pos3++;
|
||||
//Serial.println(pos1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while(pos3>jiyi3[k])
|
||||
{
|
||||
myservo3.write(pos3);
|
||||
delay(5);
|
||||
pos3--;
|
||||
//Serial.println(pos1);
|
||||
}
|
||||
}
|
||||
//*****************************************************
|
||||
//the same analysis
|
||||
if(pos4<jiyi4[k])
|
||||
{
|
||||
while(pos4<jiyi4[k])
|
||||
{
|
||||
myservo4.write(pos4);
|
||||
delay(5);
|
||||
pos4++;
|
||||
//Serial.println(pos1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
while(pos4>jiyi4[k])
|
||||
{
|
||||
myservo4.write(pos4);
|
||||
delay(5);
|
||||
pos4--;
|
||||
//Serial.println(pos1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(ps2x.Button(PSB_L2))
|
||||
Serial.println("L2 pressed");
|
||||
if(ps2x.Button(PSB_R2))
|
||||
Serial.println("R2 pressed");
|
||||
if(ps2x.Button(PSB_GREEN))
|
||||
Serial.println("Triangle pressed");
|
||||
|
||||
}
|
||||
|
||||
|
||||
if(ps2x.ButtonPressed(PSB_RED)) //will be TRUE if button was JUST pressed
|
||||
Serial.println("Circle just pressed");
|
||||
|
||||
if(ps2x.ButtonReleased(PSB_PINK)) //will be TRUE if button was JUST released
|
||||
Serial.println("Square just released");
|
||||
|
||||
if(ps2x.NewButtonState(PSB_BLUE)) //will be TRUE if button was JUST pressed OR released
|
||||
Serial.println("X just changed");
|
||||
|
||||
// claw
|
||||
claw();
|
||||
|
||||
// rotate
|
||||
turn();
|
||||
|
||||
// upper arm
|
||||
upper_arm();
|
||||
|
||||
//lower arm
|
||||
lower_arm();
|
||||
|
||||
if(ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) // print stick values if either is TRUE
|
||||
{
|
||||
Serial.print("Stick Values:");
|
||||
Serial.print(ps2x.Analog(PSS_LY), DEC); //Left stick, Y axis. Other options: LX, RY, RX
|
||||
Serial.print(",");
|
||||
Serial.print(ps2x.Analog(PSS_LX), DEC);
|
||||
Serial.print(",");
|
||||
Serial.print(ps2x.Analog(PSS_RY), DEC);
|
||||
Serial.print(",");
|
||||
Serial.println(ps2x.Analog(PSS_RX), DEC);
|
||||
}
|
||||
delay(5);
|
||||
}
|
||||
//***************************************************
|
||||
//claw
|
||||
void claw()
|
||||
{
|
||||
//claw
|
||||
if(ps2x.Analog(PSS_LX)<10) // if push the left joystick to the right
|
||||
{
|
||||
pos4=pos4-3;
|
||||
myservo4.write(pos4); //servo 4 operates the motion, the claw gradually opens.
|
||||
delay(5);
|
||||
if(pos4<45) // limit the largest angle when open the claw
|
||||
|
||||
pos4=45;
|
||||
}
|
||||
|
||||
|
||||
if(ps2x.Analog(PSS_LX)>220) ////if push the right joystick to the left
|
||||
{
|
||||
pos4=pos4+3;
|
||||
myservo4.write(pos4); // servo 4 operates the action, claw is gradually closed.
|
||||
delay(5);
|
||||
if(pos4>120) // limit the largest angle when close the claw
|
||||
{
|
||||
pos4=120;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//******************************************************/
|
||||
// turn
|
||||
void turn()
|
||||
{
|
||||
if(ps2x.Analog (PSS_RX) < 50) //if push the right joystick to the let
|
||||
{
|
||||
pos1=pos1+3;
|
||||
myservo1.write(pos1); // arm turns left
|
||||
delay(5);
|
||||
if(pos1>180) //limit the angle when turn right
|
||||
{
|
||||
pos1=180;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(ps2x.Analog (PSS_RX) > 200) // if push the right joystick to the right
|
||||
{
|
||||
pos1=pos1-3;
|
||||
myservo1.write(pos1); //servo 1 operates the motion, the arm turns right.
|
||||
delay(5);
|
||||
if(pos1<1) // limit the angle when turn left
|
||||
{
|
||||
pos1=1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//**********************************************************/
|
||||
// lower arm
|
||||
void lower_arm()
|
||||
{
|
||||
if(ps2x.Analog(PSS_RY)>200) // if push the right joystick downward
|
||||
{
|
||||
pos2=pos2-2;
|
||||
myservo2.write(pos2); // lower arm will draw back
|
||||
delay(5);
|
||||
if(pos2<25) // limit the retracted angle
|
||||
{
|
||||
pos2=25;
|
||||
}
|
||||
}
|
||||
|
||||
if(ps2x.Analog(PSS_RY)<50) // if push the right joystick upward
|
||||
{
|
||||
pos2=pos2+2;
|
||||
myservo2.write(pos2); // lower arm will stretch out
|
||||
delay(5);
|
||||
if(pos2>180) // limit the stretched angle
|
||||
{
|
||||
pos2=180;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//*************************************************************/
|
||||
|
||||
//upper arm
|
||||
void upper_arm()
|
||||
{
|
||||
if(ps2x.Analog(PSS_LY)<10) // if push the left joystick downward
|
||||
{
|
||||
pos3=pos3+2;
|
||||
myservo3.write(pos3); // upper arm will go down
|
||||
delay(5);
|
||||
if(pos3>135) // limit the angle when go down
|
||||
{
|
||||
pos3=135;
|
||||
}
|
||||
}
|
||||
if(ps2x.Analog(PSS_LY)>200) // if push the left joystick upward
|
||||
{
|
||||
pos3=pos3-2;
|
||||
myservo3.write(pos3); // the upper arm will lift
|
||||
delay(5);
|
||||
if(pos3<1) //limit the lifting angle
|
||||
{
|
||||
pos3=1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user