One Button Switch with PS3 BT Controller

Hey Friends,
I try to use one of the PS3 Buttons to switch a relay on ore off. First Press On, Second Press off. It Wont work and I don't know why.
Can u Help me?

/* 
 * This example shows how to control MDDS30 in PWM mode with Arduino.
 * Set MDDS30 input mode to 0b10110100
 * Open Serial Monitor, set baudrate to 9600bps and "No line ending".

 *
 * Hardware Connection:
 *   Arduino Uno    MDDS30
 *   GND ---------- GND
 *   4 ------------ IN1
 *   5 ------------ AN1
 *   6 ------------ AN2
 *   7 ------------ IN2
 *
 * Related Products:
 * - SmartDriveDuo-30: http://www.cytron.com.my/P-MDDS60
 * - CT UNO: http://www.cytron.com.my/p-ct-uno
 * - DC Brush Motors: http://www.cytron.com.my/c-84-dc-motor
 * - LiPo Battery: http://www.cytron.com.my/c-87-power/c-97-lipo-rechargeable-battery-and-charger
 * - Male to Male Jumper: https://www.cytron.com.my/p-wr-jw-mm65
 *
 * URL: http://www.cytron.com.my
 */
#include <PS3BT.h>
#include <usbhub.h>

// Satisfy the IDE, which needs to see the include statment in the ino too.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
#include <SPI.h>


#include <Cytron_SmartDriveDuo.h>
#define IN1 4 // Arduino pin 4 is connected to MDDS30 pin IN1.
#define AN1 5 // Arduino pin 5 is connected to MDDS30 pin AN1.
#define AN2 6 // Arduino pin 6 is connected to MDDS30 pin AN2.
#define IN2 7 // Arduino pin 7 is connected to MDDS30 pin IN2.
Cytron_SmartDriveDuo smartDriveDuo30(PWM_INDEPENDENT, IN1, IN2, AN1, AN2);
USB Usb;
//USBHub Hub1(&Usb); // Some dongles have a hub inside

BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
/* You can create the instance of the class in two ways */
//PS3BT PS3(&Btd); // This will just create the instance
PS3BT PS3(&Btd, 0x00, 0x1A, 0x7D, 0xDA, 0x71, 0x13);  // This will also store the bluetooth address - this can be obtained from the dongle when running the sketch


char inChar;
signed int speedLeft, speedRight;
int mappedfahren;
int mappedlenken;
int schildY;
int mappedschildY;
int links = 0;
int rechts = 0;
const int light= 27;
const int extswitch = 28;
boolean oldstatlight = LOW;
boolean newstatlight = LOW;
boolean lightstat = LOW;
int counterlight;

void setup()
{
  pinMode(13, OUTPUT);
  pinMode(light, OUTPUT);
    pinMode(extswitch, OUTPUT);
  Serial.begin(9600);
  digitalWrite(light,LOW);
digitalWrite(extswitch,LOW);


  
  /*digitalWrite(13, HIGH);
     speedLeft = 10;
     speedRight = 10;
  delay(1000); // Delay for 5 seconds.
  digitalWrite(13, LOW);
  */
//PS3 Controller Teil
  #if !defined(__MIPSEL__)
  while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
  if (Usb.Init() == -1) {
    Serial.print(F("\r\nOSC did not start"));
    while (1); //halt
  }
  Serial.print(F("\r\nPS3 Bluetooth Library Started"));
}

void loop()

{
    Usb.Task();
    //Lampensteuerung       
newstatlight=digitalRead(PS3.getButtonPress(CROSS));
if(newstatlight != oldstatlight)

{if (newstatlight == HIGH)

  {if (lightstat == LOW)
  
   { digitalWrite(light,HIGH); lightstat == HIGH;}
   else  { digitalWrite(light,LOW); lightstat == LOW;}
}
oldstatlight = newstatlight;


    } 

 //Lampensteuerung ENDE 



    
      if (PS3.PS3Connected || PS3.PS3NavigationConnected)
      { 

    int fahren = PS3.getAnalogHat(LeftHatY);
mappedfahren = map(fahren, 0, 255, 100, -100); 

int lenken = PS3.getAnalogHat(LeftHatX);
mappedlenken = map(lenken, 0, 255, 100, -100);

 int schildY = PS3.getAnalogButton(R2);
mappedschildY = map(schildY, 0, 255, 0, 100);

/* Wertebreich verkleinern
mappedfahren = mappedfahren / 2;
mappedlenken = mappedlenken /2;
*/ 
links = mappedfahren + mappedlenken;
rechts = mappedfahren - mappedlenken;

links = constrain(links, -100, 100);
rechts = constrain(rechts, -100, 100);
              
digitalWrite(13, HIGH);
      speedLeft = links;
digitalWrite(13, HIGH);
      speedRight = rechts;

    } 

    else
    Stop ();


    if(PS3.getButtonClick(PS)) {
      PS3.disconnect();
      Stop();
   }

    smartDriveDuo30.control(speedLeft, speedRight);
  
}


void Stop()
{
        digitalWrite(13, LOW);
      speedLeft = 0;
            digitalWrite(13, LOW);
      speedRight = 0;
}

void Lampen()
{
     
   
}

Start with a simple sketch that simply reads the state of the PS3 button and prints it to the Serial Monitor. Use that sketch to verify that you can successfully read the button state.

Next, add in the code that toggles the on/off state according to the PS3 button presses, but still just print the state on/off state to the Serial Monitor. Verify that is working exactly as you expect it to.

Finally, add the relay control code.

Thanks a lot, now it works

char inChar;
signed int speedLeft, speedRight;
int mappedfahren;
int mappedlenken;
int schildY;
int mappedschildY;
int links = 0;
int rechts = 0;
const int light= 27;
const int extswitch = 28;
boolean oldstatlight = LOW;
boolean newstatlight = LOW;
boolean lightstat = LOW;
int stattriangle;
int counterlight;




void setup()
{
  pinMode(13, OUTPUT);
  pinMode(light, OUTPUT);
    pinMode(extswitch, OUTPUT);
  Serial.begin(9600);
  digitalWrite(light,LOW);
digitalWrite(extswitch,LOW);

//PS3 Controller Teil
  #if !defined(__MIPSEL__)
  while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
  if (Usb.Init() == -1) {
    Serial.print(F("\r\nOSC did not start"));
    while (1); //halt
  }
  Serial.print(F("\r\nPS3 Bluetooth Library Started"));
}

void loop()

{
    Usb.Task();
 
    if (PS3.PS3Connected || PS3.PS3NavigationConnected)
      { 
      if (PS3.getButtonClick(TRIANGLE)) {
       
stattriangle=digitalRead(PS3.getButtonClick(TRIANGLE));

 if(stattriangle == HIGH && oldstatlight == LOW)


{if (lightstat == HIGH)

  {lightstat = LOW;    } 
  else 
   {lightstat = HIGH;}
   }
digitalWrite(light, lightstat);
oldstatlight == stattriangle;

}


    } 
      }

You're welcome. I'm glad to hear it's working now. Enjoy!
Per