PS2X_TEST6.ino


1
#include <PS2X_lib.h>  //for v1.6
2
#include <Servo.h> //Die Servobibliothek wird aufgerufen. Sie wird benötigt, damit die Ansteuerung des Servos vereinfacht wird.
3
Servo servoblau; //Erstellt für das Programm ein Servo mit dem Namen „servoblau“
4
5
PS2X ps2x; // create PS2 Controller Class
6
7
//right now, the library does NOT support hot pluggable controllers, meaning 
8
//you must always either restart your Arduino after you conect the controller, 
9
//or call config_gamepad(pins) again after connecting the controller.
10
int error = 0; 
11
byte type = 0;
12
byte vibrate = 0;
13
14
void setup(){
15
 Serial.begin(57600);
16
servoblau.attach(8); //Das Setup enthält die Information, dass das Servo an der Steuerleitung (gelb) mit Pin 8 verbunden wird. Hier ist natürlich auch ein anderer Pin möglich.
17
//CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************
18
  
19
 error = ps2x.config_gamepad(13,11,10,12, true, true);   //setup pins and settings:  GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
20
 
21
 if(error == 0){
22
   Serial.println("Found Controller, configured successful");
23
   Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
24
  Serial.println("holding L1 or R1 will print out the analog stick values.");
25
  Serial.println("Go to www.billporter.info for updates and to report bugs.");
26
 }
27
   
28
  else if(error == 1)
29
   Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
30
   
31
  else if(error == 2)
32
   Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
33
   
34
  else if(error == 3)
35
   Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
36
   
37
   //Serial.print(ps2x.Analog(1), HEX);
38
   
39
   type = ps2x.readType(); 
40
     switch(type) {
41
       case 0:
42
        Serial.println("Unknown Controller type");
43
       break;
44
       case 1:
45
        Serial.println("DualShock Controller Found");
46
       break;
47
       case 2:
48
         Serial.println("GuitarHero Controller Found");
49
       break;
50
     }
51
  
52
}
53
54
void loop(){
55
   /* You must Read Gamepad to get new values
56
   Read GamePad and set vibration values
57
   ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
58
   if you don't enable the rumble, use ps2x.read_gamepad(); with no values
59
   
60
   you should call this at least once a second
61
   */
62
   
63
   
64
   
65
 if(error == 1) //skip loop if no controller found
66
  return; 
67
  
68
 if(type == 2){ //Guitar Hero Controller
69
   
70
   ps2x.read_gamepad();          //read controller 
71
   
72
   if(ps2x.ButtonPressed(GREEN_FRET))
73
     Serial.println("Green Fret Pressed");
74
   if(ps2x.ButtonPressed(RED_FRET))
75
     Serial.println("Red Fret Pressed");
76
   if(ps2x.ButtonPressed(YELLOW_FRET))
77
     Serial.println("Yellow Fret Pressed");
78
   if(ps2x.ButtonPressed(BLUE_FRET))
79
     Serial.println("Blue Fret Pressed");
80
   if(ps2x.ButtonPressed(ORANGE_FRET))
81
     Serial.println("Orange Fret Pressed");
82
     
83
84
    if(ps2x.ButtonPressed(STAR_POWER))
85
     Serial.println("Star Power Command");
86
    
87
    if(ps2x.Button(UP_STRUM))          //will be TRUE as long as button is pressed
88
     Serial.println("Up Strum");
89
    if(ps2x.Button(DOWN_STRUM))
90
     Serial.println("DOWN Strum");
91
  
92
 
93
    if(ps2x.Button(PSB_START))                   //will be TRUE as long as button is pressed
94
         Serial.println("Start is being held");
95
    if(ps2x.Button(PSB_SELECT))
96
         Serial.println("Select is being held");
97
98
    
99
    if(ps2x.Button(ORANGE_FRET)) // print stick value IF TRUE
100
    {
101
        Serial.print("Wammy Bar Position:");
102
        Serial.println(ps2x.Analog(WHAMMY_BAR), DEC); 
103
    } 
104
 }
105
106
 else { //DualShock Controller
107
  
108
    ps2x.read_gamepad(false, vibrate);          //read controller and set large motor to spin at 'vibrate' speed
109
    
110
    if(ps2x.Button(PSB_START)) {                  //will be TRUE as long as button is pressed
111
         Serial.println("Start is being held");
112
    servoblau.write(0); //Position 1 ansteuern mit dem Winkel 0°
113
    delay(500); //Das Programm stoppt für 0,5 Sekunden
114
     servoblau.write(20); //Position 2 ansteuern mit dem Winkel 20°
115
    delay(500); //Das Programm stoppt für 0,5 Sekunden
116
  }
117
118
         
119
    if(ps2x.Button(PSB_SELECT))
120
         Serial.println("Select is being held");
121
122
          if(ps2x.Button(PSB_SELECT)){
123
          servoblau.write(0); //Position 1 ansteuern mit dem Winkel 0°
124
    delay(600); //Das Programm stoppt für 0,6 Sekunden
125
     servoblau.write(20); //Position 2 ansteuern mit dem Winkel 20°
126
    delay(300); //Das Programm stoppt für 0,3 Sekunden
127
  }
128
         
129
         
130
     if(ps2x.Button(PSB_PAD_UP)) {         //will be TRUE as long as button is pressed
131
       Serial.print("Up held this hard: ");
132
       Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
133
      }
134
      if(ps2x.Button(PSB_PAD_RIGHT)){
135
       Serial.print("Right held this hard: ");
136
        Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC);
137
      }
138
      if(ps2x.Button(PSB_PAD_LEFT)){
139
       Serial.print("LEFT held this hard: ");
140
        Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC);
141
      }
142
     if(ps2x.Button(PSB_PAD_DOWN)) {              //will be TRUE as long as button is pressed
143
    Serial.println("down just pressed");
144
    servoblau.write(90); //Position 1 ansteuern mit dem Winkel 0°
145
    delay(1000); //Das Programm stoppt für 3 Sekunden
146
    servoblau.write(45); //Position 2 ansteuern mit dem Winkel 90°
147
   delay(1000);
148
      }   
149
  
150
    
151
      vibrate = ps2x.Analog(PSAB_BLUE);        //this will set the large motor vibrate speed based on 
152
                                              //how hard you press the blue (X) button    
153
    
154
    if (ps2x.NewButtonState())               //will be TRUE if any button changes state (on to off, or off to on)
155
    {
156
     
157
       
158
         
159
        if(ps2x.Button(PSB_L3))
160
         Serial.println("L3 pressed");
161
        if(ps2x.Button(PSB_R3))
162
         Serial.println("R3 pressed");
163
        if(ps2x.Button(PSB_L2))
164
         Serial.println("L2 pressed");
165
        if(ps2x.Button(PSB_R2))
166
         Serial.println("R2 pressed");
167
        if(ps2x.Button(PSB_GREEN))
168
         Serial.println("Triangle pressed");
169
         
170
    }   
171
         
172
    
173
    if(ps2x.ButtonPressed(PSB_RED))             //will be TRUE if button was JUST pressed
174
         Serial.println("Circle just pressed");
175
          
176
    if(ps2x.ButtonReleased(PSB_PINK))             //will be TRUE if button was JUST released
177
         Serial.println("Square just released");     
178
    
179
    if(ps2x.NewButtonState(PSB_BLUE))            //will be TRUE if button was JUST pressed OR released
180
         Serial.println("X just changed");    
181
  
182
    
183
    
184
    if(ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) // print stick values if either is TRUE
185
    {
186
        Serial.print("Stick Values:");
187
        Serial.print(ps2x.Analog(PSS_LY), DEC); //Left stick, Y axis. Other options: LX, RY, RX  
188
        Serial.print(",");
189
        Serial.print(ps2x.Analog(PSS_LX), DEC); 
190
        Serial.print(",");
191
        Serial.print(ps2x.Analog(PSS_RY), DEC); 
192
        Serial.print(",");
193
        Serial.println(ps2x.Analog(PSS_RX), DEC); 
194
    } 
195
    
196
    
197
 }
198
 
199
 
200
 delay(50);
201
     
202
}