Library for PS2

I am text block. Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
// include libraries
#include <PS2X_lib.h>
PS2X ps2x; // create PS2 Controller Class
int error = 0;
byte type = 0;
byte vibrate = 0;
void setup(){
// Start serial communication
Serial.begin(57600);
error = ps2x.config_gamepad(13,40,42,44, true, true); //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
}
// Main loop
void loop(){
ps2x.read_gamepad(false, vibrate); // disable vibration of the controller
// Perform movements based on D-pad buttons
int nJoyR = ps2x.Analog(PSS_RY); // read right stick
Serial.println(nJoyR);
// MOVE FORWARD
if(ps2x.Button(PSB_PAD_UP)) {
Serial.println("Move forward");
}
// TURN RIGHT
if(ps2x.Button(PSB_PAD_RIGHT)){
Serial.println("Turn right");
}
// TURN LEFT
if(ps2x.Button(PSB_PAD_LEFT)){
Serial.println("Turn left");
}
// MOVE BACK
if(ps2x.Button(PSB_PAD_DOWN)){
Serial.println("Move back");
}
if (!ps2x.Button(PSB_PAD_DOWN) && !ps2x.Button(PSB_PAD_UP) && !ps2x.Button(PSB_PAD_RIGHT) && !ps2x.Button(PSB_PAD_LEFT)) {
Serial.println("stop");
}
delay(50);
}
I am text block. Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
#define trigPin 8
#define echoPin 9
float duration, distance; // HC-SR04 ultrasonic sensor variable
int varultrasound; //variable to check if collision button is pressed
#define in1 7 //Right
#define in2 6 //Right
#define en1 3 //right speed
#define in3 10 //Left
#define in4 11 //Left
#define en2 5 //Left speed
#define LED 4 // Front lights pin
#define LED2 A1 // Back lights pin
const int buzzer = A0; //Buzzer to arduino pin A0 (Horn)
const int buzzer2 = A2; //Buzzer for HC-SR04 ultrasonic sensor pin
int command; //Int to store app command state.
#define R_S A6 //ir sensor Right
#define L_S A7 //ir sensor Left
int lineTracker=0;
int pwm_speedA = 255; //forward speed
int pwm_speedB = 48; //turn speed
int pwm_speedC = 110; //line tracker speed
const byte BTpin = 12; //connect the STATE pin to Arduino pin D12
#include <PS2X_lib.h>
PS2X ps2x; // create PS2 Controller Class
int error = 0;
byte type = 0;
byte vibrate = 0;
int LX = 0;
int LY = 0;
int RX = 0;
int RY = 0;
int joyStick=0;
void setup() {
error =ps2x.config_gamepad(13,40,42,44, true, true); //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
pinMode(BTpin, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(en1, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(en2, OUTPUT);
pinMode(LED, OUTPUT); //Set the Front Lights pin.
pinMode(LED2, OUTPUT); //Set the Back lights.
pinMode(buzzer, OUTPUT); //buzzer to arduino pin A0 (Horn)
pinMode(buzzer2, OUTPUT); //buzzer for HC-SR04 ultrasonic sensor pin
varultrasound=0 ; // initialize variable
pinMode(R_S, INPUT); // declare if sensor as input
pinMode(L_S, INPUT); // declare ir sensor as input
Serial.begin(9600); //Set the baud rate to your Bluetooth module.
}
void loop() {
ps2x.read_gamepad(false, vibrate);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the response from the HC-SR04 Echo Pin
duration = pulseIn(echoPin, HIGH);
// Determine distance from duration
// Use 343 metres per second as speed of sound
distance = (duration / 2) * 0.0343;
/////////////////////////PS2//////////////////////////////
//if (joyStick==1) {
if (ps2x.Button(PSB_L1)){
LY = ps2x.Analog(PSS_LY); //receive values from p22 joystick
RX = ps2x.Analog(PSS_RX);
if (LY > 200 ) //check if the joystick pushed up side
{
back();
}
if (LY < 50 )
{
forward();
}
if ( RX < 50)
{
left();
}
if ( RX > 200)
{
right();
}
// if( LY == 128 && RX == 128 )
if( LY < 150 && LY >100 && RX <150 && RX > 100)
{
Stop();
}
//LY = LX = 128; //return to default vlaues
//RY = RX = 128; //return to default values
}
//}
///////////////////////////////////////////////////////////////////////
/////////////////check if line tracker button is pressed///////////////////////
if (lineTracker==1) {
if((digitalRead(R_S) == 0)&&(digitalRead(L_S) == 0)){
pwm_speedA = pwm_speedC;
forward();
} //if Right Sensor and Left Sensor are at White color then it will call forword function
if((digitalRead(R_S) == 0)&&(digitalRead(L_S) == 1)){
pwm_speedA = pwm_speedC;
right();
delay(10);
right();
} //if Right Sensor is Black and Left Sensor is White then it will call turn Right function
if((digitalRead(R_S) == 1)&&(digitalRead(L_S) == 0)){
pwm_speedA = pwm_speedC;
left();
delay(10);
left();
} //if Right Sensor is White and Left Sensor is Black then it will call turn Left function
if((digitalRead(R_S) == 1)&&(digitalRead(L_S) == 1)){Stop();} //if Right Sensor and Left Sensor are at Black color then it will call Stop function
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
// check distance from obstacle and if the button "Collision" from the application is pressed
if ((varultrasound > 0)&& (distance<=10)){
digitalWrite(in4, HIGH);
analogWrite(en2,255);
digitalWrite(in1, HIGH);
analogWrite(en1,255);
tone(buzzer2, 2500); // Send 1KHz sound signal...
delay(500);
analogWrite(in4, 0);
analogWrite(en2,0);
analogWrite(in1, 0);
analogWrite(en1,0);
noTone(buzzer2);// Stop sound...
}
///////////////////////////////////////////////////////////////////Read BTpin and check if Bluetooth Disconnected
//if (digitalRead(BTpin)==LOW){
// Stop();
//}
///////////////////////////////////////////////////////////////////////////////////////////
if (Serial.available() > 0) {
command = Serial.read();
Stop(); //Initialize with motors stoped.
switch (command) {
///////////////////////////////////// Turn on/off Front lights
case 'W':
digitalWrite(LED, HIGH);
break;
case 'w':
digitalWrite(LED, LOW);
break;
///////////////////////////////////// Change variable varultrasound if button "Collision is pressed
case 'M':
varultrasound=varultrasound+1 ;
break;
case 'm':
varultrasound=0 ;
break;
////////////////////////////////////// Turn on/off Back lights
case 'U':
digitalWrite(LED2, HIGH);
break;
case 'u':
digitalWrite(LED2, LOW);
break;
/////////////////////////////////////// Toggle on/off Horn
case 'V':
tone(buzzer, 2500); // Send 1KHz sound signal...
break;
case 'v':
noTone(buzzer);// Stop sound...
break;
////////////////////////////////////////
case 'F':
forward();
break;
case 'B':
back();
break;
case 'L':
left();
break;
case 'R':
right();
break;
case 'G':
forwardleft();
break;
case 'I':
forwardright();
break;
case 'H':
backleft();
break;
case 'J':
backright();
break;
case '0':
pwm_speedA = 100;
break;
case '1':
pwm_speedA = 140;
break;
case '2':
pwm_speedA = 153;
break;
case '3':
pwm_speedA = 165;
break;
case '4':
pwm_speedA = 178;
break;
case '5':
pwm_speedA = 191;
break;
case '6':
pwm_speedA = 204;
break;
case '7':
pwm_speedA=216;
break;
case '8':
pwm_speedA = 229;
break;
case '9':
pwm_speedA = 242;
break;
case 'q':
pwm_speedA = 255;
break;
case 'K':
lineTracker= lineTracker+1;
break;
case 'k':
lineTracker=0;
Stop();
break;
case 'P':
joyStick= joyStick+1;
break;
case 'p':
joyStick=0;
Stop();
break;
}
}
}
void forward() {
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(en2,pwm_speedA);
digitalWrite(in2, HIGH);
digitalWrite(in1, LOW);
analogWrite(en1,pwm_speedA);
}
void back() {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(en1,pwm_speedA);
digitalWrite(in4, HIGH);
digitalWrite(in3, LOW);
analogWrite(en2,pwm_speedA);
}
void left() {
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(en2,pwm_speedA);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(en1,pwm_speedA);
}
void right() {
digitalWrite(in2, HIGH);
digitalWrite(in1, LOW);
analogWrite(en1,pwm_speedA);
digitalWrite(in4, HIGH);
digitalWrite(in3, LOW);
analogWrite(en2,pwm_speedA);
}
void forwardleft() {
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(en2,pwm_speedA);
digitalWrite(in2, HIGH);
digitalWrite(in1, LOW);
analogWrite(en1,pwm_speedB);
}
void forwardright() {
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(en2,pwm_speedB);
digitalWrite(in2, HIGH);
digitalWrite(in1, LOW);
analogWrite(en1,pwm_speedA);
}
void backright() {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(en1,pwm_speedA);
digitalWrite(in4, HIGH);
digitalWrite(in3, LOW);
analogWrite(en2,pwm_speedB);
}
void backleft() {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(en1,pwm_speedB);
digitalWrite(in4, HIGH);
digitalWrite(in3, LOW);
analogWrite(en2,pwm_speedA);
}
void Stop() {
analogWrite(in1, LOW);
analogWrite(in2, LOW);
analogWrite(in3, LOW);
analogWrite(in4, LOW);
}






Exceolent goods from you, man. I’ve understand your stuff previous too and you are just too fantastic.
I actually like what you have acquired here, certainly like what you are stating and tthe way in which you
say it. You make it enjoyable and you still take care of to keep
iit smart. I can not wait to read much more fro you.
This is actually a great site.
php patterns