Programm optimieren

Hallo zusammen,
ich habe mir ein Modell Fahrlader gebaut, dieser wird aktuell mit einer Arduino mega betrieben.
Eingabe erfolgt über einen Bluetooth-Controller (PS2) damit werden mehrere Zylinder (Motoren) und LED´s angesteuert. Das Programm ist immer weitergewachsen und habe nach und nach Beleuchtung hinzugefügt.
Nun habe ich alles angeschlossen und alle Funktionen sind geben, lediglich die Bewegung bzw. die Motoren laufen nicht sauber durch. Meine Vermutung ist, dass die einzelnen Delay´s für die Selbst Haltung (Tasten entprellen) der Beleuchtung dieses verursachen.
Wie kann ich dieses nun beheben? Die Bewegungen sollen schon flüssig durchlaufen. Wie kann dieses am besten umgesetzt werden? Hatte leider kein Beispiel oder ein ähnliches Projekt gefunden.
Danke und Gruß
Alex

#include <PS2X_lib.h>  //for v1.6
#include <Servo.h>
Servo servo1;
/******************************************************************
   set pins connected to PS2 controller:
     - 1e column: original
     - 2e colmun: Stef?
   replace pin numbers by the ones you use
 ******************************************************************/
#define PS2_DAT        6  //14  ((13))  
#define PS2_CMD        7  //15  ((11))
#define PS2_SEL        8  //16  ((10))
#define PS2_CLK        9  //17  ((12))

/******************************************************************
   select modes of PS2 controller:
     - pressures = analog reading of push-butttons
     - rumble    = motor rumbling
   uncomment 1 of the lines for each mode selection
 ******************************************************************/
//#define pressures   true
#define pressures   false
//#define rumble      true
#define rumble      false
//#define pin7 7
//#define pin6 6

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 type = 0;
byte vibrate = 0;
int pin6 = 3; // Kippzylinder pwm
int pin7 = 2; //Kippzylinder digital
int pin5 = 12; //hubzylinder pwm
int pin4 = 13; //hubzylinder digital
int pin8 = 5; //fahren pwm
int pin9 = 4; //fahren digital 
int val1;// bucket up
int val2; //bucket down
int val3; //Liftarm
int val4; //Liftarm
int val5; // vorwärts
int val6; // rückwarts
int val7;// lenken links
int val8; // lenken rechts

///////////////ENDschalter
int s0 = A0;//kippzylinder oben
int s1 = A1;//kippzylinder unten
int s2 = A2;//hubzylinder unten
int s3 = A3;//hubzylinder oben


int stepdelay;
//////////////////////Beleuchtung Abblendlicht//////////////////

const int ledPin1 = 16 ; // Abblendlicht vorne
const int ledPin3 = 19; // Abblendlicht hinten
int buttonPushCounter = 0;
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

/////////////////////////Beleuchtung Fernlicht////////////////

const int ledPin2 = 17; // Fernlicht vorne
const int ledPin4 = 18; // Fernlicht hinten
int buttonPushCounter2 = 0;
int buttonState2 = 0;         // current state of the button
int lastButtonState2 = 0;     // previous state of the button
/////////////////////////Beleuchtung Dreick Stoßbeleuchtung ////////////////

const int ledPin5 = 14; // stoßbeleuchtung
int buttonPushCounter3 = 0;
int buttonState3 = 0;         // current state of the button
int lastButtonState3 = 0;     // previous state of the button

/////////////////////////Beleuchtung Viereck RUK ////////////////
const int ledPin6 = 15; // RUK
int ledState = LOW;   
unsigned long previousMillis = 0; 
const long interval = 1000; 
int buttonPushCounter4 = 0;
int buttonState4 = 0;         // current state of the button
int lastButtonState4 = 0;     // previous state of the button
////////////////// Position leuchte /////////////////////////7
const int ledPin9 = 20; // RUK


void setup() {

  Serial.begin(57600);

  delay(300);  //added delay to give wireless ps2 module some time to startup, before configuring it

  pinMode(pin6, OUTPUT); // bucket pin6
  pinMode(pin7, OUTPUT);  // bucket pin7
  pinMode(pin5, OUTPUT); // liftarm Pin 5
  pinMode(pin4, OUTPUT); // liftarm pin 1
  pinMode(pin8, OUTPUT); // fahren pwm
  pinMode(pin9, OUTPUT); // fahren digital
  
  pinMode(ledPin1, OUTPUT);//LED 1
  pinMode(ledPin2, OUTPUT);//LED 2
  pinMode(ledPin3, OUTPUT);//LED 3
  pinMode(ledPin4, OUTPUT);//LED 4
  pinMode(ledPin5, OUTPUT);//LED 5
  pinMode(ledPin6, OUTPUT);//LED 6
  //pinMode(ledPin7, OUTPUT);//LED 7
  pinMode(A0, INPUT);//kippzylinder unten
  pinMode(A1, INPUT);//kippzylinder oben 
  pinMode(A2, INPUT);//hubzylinder unten 
  pinMode(A3, INPUT);//hubzylinder oben
 servo1.attach(10);     //PIn 10
  //CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************

  //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
  error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);

  if (error == 0) {
    digitalWrite (ledPin9, HIGH);
    Serial.print("Found Controller, configured successful ");
    Serial.print("pressures = ");
    if (pressures)
      Serial.println("true ");
    else
      Serial.println("false");
    Serial.print("rumble = ");
    if (rumble)
      Serial.println("true)");
    else
      Serial.println("false");
   //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("Note: 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");

  else if (error == 3)
    Serial.println("Controller refusing to enter Pressures mode, may not support it. ");

  //  Serial.print(ps2x.Analog(1), HEX);

  type = ps2x.readType();
  switch (type) {
    case 0:
      Serial.print("Unknown Controller type found ");
      break;
    case 1:
      Serial.print("DualShock Controller found ");
      break;
    case 2:
      Serial.print("GuitarHero Controller found ");
      break;
    case 3:
      Serial.print("Wireless Sony DualShock Controller found ");
      break;
  }
 
}


void loop() {
  /* You must Read Gamepad to get new values 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
  */
  if (error == 1) //skip loop if no controller found
    return;

  if (type == 2) { //Guitar Hero Controller
    ps2x.read_gamepad();          //read controller



    if (ps2x.ButtonPressed(STAR_POWER))
      Serial.println("Star Power Command");

    if (ps2x.Button(UP_STRUM))         //will be TRUE as long as button is pressed
      Serial.println("Up Strum");
    if (ps2x.Button(DOWN_STRUM))
      Serial.println("DOWN Strum");

    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(ORANGE_FRET)) {    // print stick value IF TRUE
      Serial.print("Wammy Bar Position:");
      Serial.println(ps2x.Analog(WHAMMY_BAR), DEC);
    }
  }
  else { //DualShock Controller  dieser Teile wird verwedet //////////////////////
    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_CROSS);  //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_L3))
        Serial.println("L3 pressed");
      if (ps2x.Button(PSB_R3))
        Serial.println("R3 pressed");
      if (ps2x.Button(PSB_L2))
        Serial.println("L2 pressed");
      if (ps2x.Button(PSB_R2))
        Serial.println("R2 pressed");
      if (ps2x.Button(PSB_TRIANGLE))
        Serial.println("Triangle pressed");
    }

    if (ps2x.ButtonPressed(PSB_CIRCLE))             //will be TRUE if button was JUST pressed
      Serial.println("Circle just pressed");
    if (ps2x.NewButtonState(PSB_CROSS))              //will be TRUE if button was JUST pressed OR released
      Serial.println("X just changed");
    if (ps2x.ButtonReleased(PSB_SQUARE))             //will be TRUE if button was JUST released
      Serial.println("Square just released");


    //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(50);
  }
  int nJoyX = ps2x.Analog(PSS_LX); // read x-joystick rechts
  int nJoyY = ps2x.Analog(PSS_LY); // read y-joystick rechts

  nJoyX = map(nJoyX, 0, 255, -1023, 1023);
  nJoyY = map(nJoyY, 0, 255, 1023, -1023);

  int nJoyXl = ps2x.Analog(PSS_RX); // read x-joystick links
  int nJoyYl = ps2x.Analog(PSS_RY); // read y-joystick links

  nJoyXl = map(nJoyXl, 0, 255, -1023, 1023);
  nJoyYl = map(nJoyYl, 0, 255, 1023, -1023);


  /////// vorwärts fahren ////////////////////////////////////////////7
  if (nJoyY >= 40) {
    Serial.print("forward");
    val5 = ps2x.Analog(PSS_RY);
    val5 = map (val5, 127 , 0 , 0, 255);
    {
      digitalWrite (pin8, val5);
     Serial.print("val5:"); Serial.print(val5); Serial.print(pin6);
      digitalWrite (pin9, HIGH);
      Serial.print("HIGH");
      delayMicroseconds (100);
    }
  }
  else if (nJoyY <= 40) {
    analogWrite (pin8, 0);
  }


  ////////// rückfährts/////////////////////////
  if (nJoyY <= -550) {
    Serial.print("backward");
    val6 = ps2x.Analog(PSS_RY);
    val6 = map (val6, 127, 256 , 100, 255);
    {
      analogWrite (pin8, val6);
      Serial.print("val6:"); Serial.print(val6);
      digitalWrite (pin9, LOW);
        digitalWrite(ledPin2, LOW);
    
      delayMicroseconds (100);
    }
  }
  else if (nJoyY  <= -400) {
    analogWrite (pin8, 0);
    delayMicroseconds (100);
  }

  ///////////////////lenken rechts ////////////////////7
  if (nJoyX  >=60) {
    Serial.print("lenken rechts");
    val8 = ps2x.Analog(PSS_LX);
   val8 = map (val8, 160, 254 , 90, 30);

       servo1.write(val8);
       Serial.print("val8:"); Serial.print(val8);
       delayMicroseconds(1000);
     }




  //////////////////LEnkeb links///////////////////////
   if (nJoyX <= -550) {
      Serial.print("lenken links");
      val7 = ps2x.Analog(PSS_LX);
     val7 = map (val7,  100, 0 , 90, 150);

        servo1.write(val7);
        Serial.print("val7:"); Serial.print(val7);
      delayMicroseconds(1000);

      }
  
  // bucket down  Motor 2 rechtsrum
  if ((nJoyYl >=100) && (digitalRead(A0) ==LOW)) {
    Serial.print("bucket down");
    val2 = ps2x.Analog(PSS_RY);
    val2 = map (val2, 127 , 0 , 0, 255);
    {
      digitalWrite (pin6, val2);
      Serial.print("val2:"); Serial.print(val2);Serial.print(pin6);
      digitalWrite (pin7, HIGH);
      Serial.print("HIGH");
      delayMicroseconds (100);
    }
  }
  else if ((nJoyYl <= 40)|(digitalRead(A0) ==HIGH))  {
    analogWrite (pin6, 0);
  }
  // bucket up   Motor 2 linksrum 
  if ((nJoyYl <= -550)&& (digitalRead(A1) ==LOW)) {
    Serial.print("bucket up");
    val1 = ps2x.Analog(PSS_RY);
    val1 = map (val1, 127, 256 , 100, 255);
    {
      analogWrite (pin6, val1);
      Serial.print("val1:"); Serial.print(val1);
      digitalWrite (pin7, LOW);
      delayMicroseconds (100);
    }
  }
  else if ((nJoyYl <= -400)| ((nJoyYl <= -400)&&(digitalRead(A1) ==HIGH))) {
   analogWrite (pin6, 0);
    delayMicroseconds (100);
  }

    // lirmarm down
  if ((nJoyXl <= -550)&&(digitalRead(A2) ==LOW)) {
    Serial.print("lirmarm down");
    val4 = ps2x.Analog(PSS_RX);
   val4 = map (val4,  127 , 0 , 0, 255);
    {
      analogWrite(pin5, val4); //geschwindigkeit
      Serial.print("val4:"); Serial.print(val4);
    digitalWrite (pin4, LOW); //drehrichtung
    delayMicroseconds(100);

    }
  }
  else if ((nJoyXl >= -500)| ((nJoyXl <= -400)&&(digitalRead(A2) ==HIGH)))  {
   analogWrite(pin5, 0);
     
  }
  // lirmarm up Motor 1 rechtsrum
  if ((nJoyXl  >=60)&&(digitalRead(A3) ==LOW)) {
    Serial.print("lirmarm up");
   val3 = ps2x.Analog(PSS_RX);
   val3 = map (val3, 127, 256 , 100, 255);
    {
      analogWrite(pin5, val3); //geschwindigkeit
      Serial.print("val3:"); Serial.print(val3);
      digitalWrite (pin4, HIGH); //drehrichtung
      delayMicroseconds(100);
    }
  }
  else if ((nJoyXl  < 40)|(digitalRead(A3) ==HIGH)) {
    digitalWrite (pin4, LOW);
  }




if (digitalRead(A2) ==HIGH)
{ Serial.print("s0 high");
}
else if (digitalRead(A0) ==LOW)
{Serial.print("s0 low");
}
if (digitalRead(A3) ==HIGH)
{ Serial.print("s1 high");
}
else if (digitalRead(A1) ==LOW)
{Serial.print("s1 low");
}
  ////////////////Beleuchtung///////////////
  // LED X  Abblendlicht /////////////////////////////////////////////////////////
  buttonState = (ps2x.NewButtonState(PSB_CROSS));
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);

    }
    else {

      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(10);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;

  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin1, HIGH); //Abblendlicht vorne
    digitalWrite(ledPin3, HIGH); //Abblendlicht hinten

    Serial.println("ledPin1, HIGH");
  }
  else {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin3, LOW);
    Serial.println("ledPin, LOW");
  }

  ///////////////LED 2 Kreis Fernlicht////////////////
  buttonState2 = (ps2x.NewButtonState(PSB_CIRCLE));
  if (buttonState2 != lastButtonState2) {
    // if the state has changed, increment the counter
    if (buttonState2 == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter2++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter2);
    }
    else {

      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(5);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState2 = buttonState2;



  if (buttonPushCounter2 % 4 == 0) {
    digitalWrite(ledPin2, HIGH);   //Fernlicht vorne
    digitalWrite(ledPin4, HIGH);    //Fernlicht hinten
    Serial.println("ledPin2, HIGH");
  }
  else {
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin4, LOW);
    Serial.println("ledPin2, LOW");
  }

  ///////////////LED Dreieck Stoßbleuchtung  ////////////////
  buttonState3 = (ps2x.NewButtonState(PSB_TRIANGLE));
  if (buttonState3 != lastButtonState3) {
    // if the state has changed, increment the counter
    if (buttonState3 == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter3++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter3);
    }
    else {

      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(10);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState3 = buttonState3;



  if (buttonPushCounter3 % 4 == 0) {
     digitalWrite(ledPin5, HIGH);
     Serial.println("ledPin5, HIGH");
  }
  else {
    digitalWrite(ledPin5, LOW);
    Serial.println("ledPin5, LOW");
  }

  ///////////////LED Viereck RUK ////////////////
    unsigned long currentMillis = millis();
  buttonState4 = (ps2x.NewButtonState(PSB_SQUARE));
  if (buttonState4 != lastButtonState4) {
    // if the state has changed, increment the counter
    if (buttonState4 == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter4++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter4);
    }
    else {

     
     
    }
    // Delay a little bit to avoid bouncing
    delay(10);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState4 = buttonState4;



  if (buttonPushCounter4 % 4 == 0) {
  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
   
    }
   else {
     ledState = LOW;
    }

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin6, ledState);
  
  }
}
}

Siehe IDE Beispiele Debounce und StateChangeDetection. Und alle anderen natürlich auch, die helfen beim Einstieg in die Materie.

ledState = !ledState ;
  unsigned long currentMillis = millis();

...

    if (currentMillis - previousMillis >= interval) {
      // save the last time you blinked the LED
      previousMillis = currentMillis;
    if (millis() - previousMillis >= interval) {
      // save the last time you blinked the LED
      previousMillis += interval;

oder ledState ganz weglassen weil:

int ledState = LOW;

...

      ledState = !ledState ;
      // set the LED with the ledState of the variable:
      digitalWrite(ledPin6, ledState);
      // set the LED with the ledState of the variable:
      digitalWrite(ledPin6, !digitalRead(ledPin6));

das kann auch weg:

    // Delay a little bit to avoid bouncing
    delay(10);

was soll das denn? jeden Tastendruck wird Licht aus gemacht, aber jeder vierter ein?

if (buttonPushCounter % 4 == 0) {

Verstehe leider nicht wie das meinst .. :sweat_smile:

hast du das nicht geschrieben?

It seems to be :nerd_face:

doch schon, mit der Selbsthaltung habe ich über google gefunden

die Ausgabe zu Serial Port, brauchst du die wirklich?

Habe diese rein gemacht damit ich mir sicher bin das es auch klappt

auch bei LEDs einschalten?

ja zum anfang hatte noch keine Hardware
verlangsam der Serialprint die Arduino stark?

Warum benennst du die Pins nicht wie im Kommentar beschrieben?
btw:
das sind alles Konstanten, die mit constexpr spezifiziert und deklariert werden können.

zum Beispiel:

constexpr byte KippZylinderPWM {3};

Have a nice day and enjoy programming in C++ and learning.
Errors and omissions excepted.
Дайте миру шанс!

Dein Subjekt lautet Programm optimieren.
Das fängt vor Setup() an.

mach das nicht:

#define PS2_DAT        6  //14  ((13))  

Wenn Du irgendwo im Code PS2_DAT erneut zuweist, wird das Murks.
Vorschlag:

const byte PS2_DAT = 6;  //14  ((13))  

Das für alle Pins.

Hier das Gleiche:

#define pressures   false

neu:

bool presssures = false;

Wenn sich das nicht ändert, gehört ein const davor.

int error = 0;

kann das kleiner 0 werden?
Wenn nein, dann unsigned int, vermutlich sogar nur byte...

int pin6 = 3; // Kippzylinder pwm

Warum gibst Du Deinen Pins keinen Namen, der auch im Code lesbar wiedergibt, was der Pin macht?
Darüber hinaus braucht int auf einem Arduino 2 bytes.
Also mach byte daraus und const - der kann sich ja nicht ändern.

Das auch mit allen anderen. Endschalter ....
counter, egal welche, können die negativ werden? Nein? Dann unsigned.

int buttonState2 = 0;         // current state of the button

states, egal ob lastbutton, button oder led, können nur zwei Zustände.
Da mach ein bool draus.

welchen motor Treiber nutzt du? ich schaue dein Code und bin ein wenig erstaunt, denn z.Z. sieht es so aus du muss beide sticks nach vorne zum Fahren voraus. aber vlht ergibt es noch ein Sinn.

#include <PS2X_lib.h>
#include <Servo.h>
Servo servo1;
#define PS2_DAT 6
#define PS2_CMD 7
#define PS2_SEL 8
#define PS2_CLK 9
#define pressures   false
#define rumble      false

PS2X ps2x; // create PS2 Controller Class

byte type = 0;
const byte KipPWM_Pin = 3; // Kippzylinder pwm
const byte KipDIR_Pin = 2; //Kippzylinder digital
const byte HubPWM_Pin = 12; //hubzylinder pwm
const byte HubDir_Pin = 13; //hubzylinder digital
const byte MovePWM_Pin = 5; //fahren pwm
const byte MoveDir_Pin = 4; //fahren digital

///////////////ENDschalter
const byte KiZylO_Pin = A0;//kippzylinder oben
const byte KiZylU_Pin = A1;//kippzylinder unten
const byte HuZylU_Pin = A2;//hubzylinder unten
const byte HuZylO_Pin = A3;//hubzylinder oben

//////////////////////Beleuchtung Abblendlicht//////////////////
const byte AbBlLichtVorn_Pin = 16 ; // Abblendlicht vorne
const byte AbBlLichtHinte_Pin = 19; // Abblendlicht hinten
bool buttonState = 0;         // current state of the button
bool lastButtonState = 0;     // previous state of the button

/////////////////////////Beleuchtung Fernlicht////////////////
const byte FeLiVo_Pin = 17; // Fernlicht vorne
const byte FeLiHi_Pin = 18; // Fernlicht hinten
bool buttonState2 = 0;         // current state of the button
bool lastButtonState2 = 0;     // previous state of the button
/////////////////////////Beleuchtung Dreick Stoßbeleuchtung ////////////////

const byte StoLed_Pin = 14; // stoßbeleuchtung
bool buttonState3 = 0;         // current state of the button
bool lastButtonState3 = 0;     // previous state of the button

/////////////////////////Beleuchtung Viereck RUK ////////////////
const byte RUKLED_Pin = 15; // RUK
unsigned long previousMillis = 0;
const int interval = 1000;
bool buttonState4 = 0;         // current state of the button
bool lastButtonState4 = 0;     // previous state of the button

////////////////// Position leuchte /////////////////////////7
const byte ledOK_Pin = 20;


void setup() {
  Serial.begin(57600);
  delay(300);  //added delay to give wireless PS2 module some time to startup, before configuring it

  pinMode(KipPWM_Pin, OUTPUT); // bucket KipPWM_Pin
  pinMode(KipDIR_Pin, OUTPUT);  // bucket KipDIR_Pin
  pinMode(HubPWM_Pin, OUTPUT); // liftarm Pin 5
  pinMode(HubDir_Pin, OUTPUT); // liftarm pin 1
  pinMode(MovePWM_Pin, OUTPUT); // fahren pwm
  pinMode(MoveDir_Pin, OUTPUT); // fahren digital

  pinMode(AbBlLichtVorn_Pin, OUTPUT);
  pinMode(FeLiVo_Pin, OUTPUT);
  pinMode(AbBlLichtHinte_Pin, OUTPUT);
  pinMode(FeLiHi_Pin, OUTPUT);
  pinMode(StoLed_Pin, OUTPUT);
  pinMode(RUKLED_Pin, OUTPUT);
  //pinMode(ledKipDIR_Pin, OUTPUT);
  servo1.attach(10);     //PIn 10
 
  //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
  byte error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);

  if (error == 0) {
    digitalWrite (ledOK_Pin, HIGH);
    Serial.print("Found Controller, configured successful ");
  }
  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");
  else if (error == 3)Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
  type = ps2x.readType();
  switch (type) {
    case 0: Serial.print("Unknown Controller type found "); break;
    case 1: Serial.print("DualShock Controller found "); break;
    case 2: Serial.print("GuitarHero Controller found "); break;
    case 3: Serial.print("Wireless Sony DualShock Controller found "); break;
  }
  if (error == 1)while (1); //skip loop if no controller found
}

void loop() {
  
  ps2x.read_gamepad();

  byte Lenken = ps2x.Analog(PSS_LX);
  byte Fahren = ps2x.Analog(PSS_LY);
  byte Schwenken = ps2x.Analog(PSS_RX);// read x-joystick links
  byte Kippen = ps2x.Analog(PSS_RY);// read y-joystick links

  // fahren
  if (Fahren > 138) {                                   // vorwärts
    digitalWrite (MoveDir_Pin, HIGH);
    analogWrite (MovePWM_Pin, map(Fahren, 127, 255, 0, 255));
    digitalWrite(FeLiVo_Pin, HIGH);
    digitalWrite(FeLiHi_Pin, LOW);
  }
  else if (Fahren < 118) {                              // rückfährts
    digitalWrite (MoveDir_Pin, LOW);
    analogWrite (MovePWM_Pin, map(Fahren, 127, 0, 0, 255));
    digitalWrite(FeLiHi_Pin, HIGH);
    digitalWrite(FeLiVo_Pin, LOW);
  }
  else digitalWrite (MovePWM_Pin, LOW);

  if (Lenken > 138)servo1.write(map (Lenken, 127, 255, 90, 150));//lenken rechts
  else if (Lenken < 118)servo1.write(map (Lenken,  0, 127, 30, 90)); //LEnkeb links
  else servo1.write(90);                                  //mitte


  //------------------------------------------------------------------------------------------
  bool KO = digitalRead(KiZylO_Pin) == HIGH;
  bool KU = digitalRead(KiZylU_Pin) == HIGH;
  bool HU = digitalRead(HuZylU_Pin) == HIGH;
  bool HO = digitalRead(HuZylO_Pin) == HIGH;
  if (KO)Serial.print("Kippzylinder oben activ");
  if (KU)Serial.print("Kippzylinder Unten activ");
  if (HU)Serial.print("Hubzylinder Unten activ");
  if (HO)Serial.print("Hubzylinder oben activ");

  if ((Kippen > 137) && !KO) {
    Serial.print("bucket down");
    digitalWrite (KipDIR_Pin, HIGH);
    analogWrite (KipPWM_Pin, map(Kippen, 127 , 0 , 0, 255));
  }
  else if (Kippen < 119 && !KU) {
    Serial.print("bucket up");
    digitalWrite (KipDIR_Pin, LOW);
    analogWrite (KipPWM_Pin, map (Kippen, 127, 255 , 0, 255));
  }
  else digitalWrite (KipPWM_Pin, LOW);

  if ((Schwenken < 119) && !HU) {
    Serial.print("lirmarm down");
    digitalWrite (HubDir_Pin, LOW); //drehrichtung
    analogWrite(HubPWM_Pin, map (Schwenken,  127 , 0 , 10, 255)); //geschwindigkeit
  }
  else if (Schwenken  > 137 && !HO) {
    Serial.print("lirmarm up");
    digitalWrite (HubDir_Pin, HIGH); //drehrichtung
    analogWrite(HubPWM_Pin, map (Schwenken, 127, 256 , 10, 255)); //geschwindigkeit
  }
  else digitalWrite (HubPWM_Pin, LOW);


  ////////////////Beleuchtung///////////////
  // LED X  Abblendlicht /////////////////////////////////////////////////////////
  buttonState = (ps2x.NewButtonState(PSB_CROSS));
  if (buttonState && !lastButtonState) {
    digitalWrite(AbBlLichtVorn_Pin, !digitalRead(AbBlLichtVorn_Pin)); //Abblendlicht vorne
    digitalWrite(AbBlLichtHinte_Pin, !digitalRead(AbBlLichtHinte_Pin)); //Abblendlicht hinten
  }
  lastButtonState = buttonState;

  ///////////////LED 2 Kreis Fernlicht////////////////
  buttonState2 = (ps2x.NewButtonState(PSB_CIRCLE));
  if (buttonState2 && !lastButtonState2) {
    digitalWrite(FeLiVo_Pin, !digitalRead(FeLiVo_Pin));   //Fernlicht vorne
    digitalWrite(FeLiHi_Pin, !digitalRead(FeLiHi_Pin));    //Fernlicht hinten
  }
  lastButtonState2 = buttonState2;

  ///////////////LED Dreieck Stoßbleuchtung  ////////////////
  buttonState3 = (ps2x.NewButtonState(PSB_TRIANGLE));
  if (buttonState3 && !lastButtonState3)digitalWrite(StoLed_Pin, !digitalRead(StoLed_Pin));
  else lastButtonState3 = buttonState3;

  ///////////////LED Viereck RUK ////////////////
  static bool Led4BlinkingIsOn = false;
  buttonState4 = (ps2x.NewButtonState(PSB_SQUARE));
  if (buttonState4 && !lastButtonState4)Led4BlinkingIsOn = !Led4BlinkingIsOn;
  lastButtonState4 = buttonState4;
  if (Led4BlinkingIsOn) {
    if (millis() - previousMillis >= interval) {
      previousMillis += interval;
      digitalWrite(RUKLED_Pin, !digitalRead(RUKLED_Pin));
    }
  }
}

vielen schonmal für die Mühe. :grinning:
Wo finde ich den die Bibliothek #include <ps2x_lib.h>
Habe die nur diese #include <PS2X_lib.h>

Beim Hochladen von dem Code erscheint folgende Fehlermeldung

Arduino: 1.8.19 (Windows Store 1.8.57.0) (Windows 10), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"





















LF-10_optimiert:34:1: error: 'ps2x' does not name a type

 ps2x ps2x; // create PS2 Controller Class

 ^~~~

C:\Users\Raab\Documents\Arduino\LF-10_optimiert\LF-10_optimiert.ino: In function 'void setup()':

LF-10_optimiert:97:11: error: 'ledDreieck_Pin' was not declared in this scope

   pinMode(ledDreieck_Pin, OUTPUT);

           ^~~~~~~~~~~~~~

C:\Users\Raab\Documents\Arduino\LF-10_optimiert\LF-10_optimiert.ino:97:11: note: suggested alternative: 'ledOK_Pin'

   pinMode(ledDreieck_Pin, OUTPUT);

           ^~~~~~~~~~~~~~

           ledOK_Pin

LF-10_optimiert:108:16: error: 'ps2x' was not declared in this scope

   byte error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);

                ^~~~

C:\Users\Raab\Documents\Arduino\LF-10_optimiert\LF-10_optimiert.ino: In function 'void loop()':

LF-10_optimiert:132:3: error: 'ps2x' was not declared in this scope

   ps2x.read_gamepad();

   ^~~~

LF-10_optimiert:184:64: error: expected ')' before ';' token

     analogWrite(HubPWM_Pin, map (Schwenken,  127 , 0 , 10, 255);); //geschwindigkeit

                                                                ^

LF-10_optimiert:184:65: error: expected primary-expression before ')' token

     analogWrite(HubPWM_Pin, map (Schwenken,  127 , 0 , 10, 255);); //geschwindigkeit

                                                                 ^

LF-10_optimiert:213:54: error: 'ledDreieck_Pin' was not declared in this scope

   if (buttonState3 && !lastButtonState3)digitalWrite(ledDreieck_Pin, !digitalRead(ledDreieck_Pin));

                                                      ^~~~~~~~~~~~~~

C:\Users\Raab\Documents\Arduino\LF-10_optimiert\LF-10_optimiert.ino:213:54: note: suggested alternative: 'ledOK_Pin'

   if (buttonState3 && !lastButtonState3)digitalWrite(ledDreieck_Pin, !digitalRead(ledDreieck_Pin));

                                                      ^~~~~~~~~~~~~~

                                                      ledOK_Pin

exit status 1

'ps2x' does not name a type

Schreib das so - dann geht auch die Fehlermeldung weg.

es ist deine Aufgabe, Links zu allen von dir verwendeten Libraries bereitzustellen.
Also: wo ist deine PS2X_lib.h zu finden?

gehe die Fehlermeldungen Zeile für Zeile durch.
Der Compiler schreibt dir EXAKT raus, was ihm nicht gefällt.

nur zwei Beispiele
a)

```
LF-10_optimiert:34:1: error: 'ps2x' does not name a type

 ps2x ps2x; // create PS2 Controller Class

 ^~~~
```

sieh IN DEINER ps2x Library nach, wie die Anlage des ps2x Objektes aussehen muss.
Nur du weist welche ps2x Library du verwendest.

b)

LF-10_optimiert:97:11: error: 'ledDreieck_Pin' was not declared in this scope

lies mal diese Zeile GANZ GENAU. Ja wo hast du denn die Variable (vieleicht sogar Konstante) denn deklariert?

Der Code wurde ihm geschrieben. :wink: Er ist da nicht schuld dran.

@raab90 Das ist oben als

const byte StoLed_Pin = 14; // stoßbeleuchtung

vorgesehen.
Entweder oben ändern oder über all im Code austauschen.

Und dann änder mal von:

#define PS2_DAT        6  //14  ((13))  
#define PS2_CMD        7  //15  ((11))
#define PS2_SEL        8  //16  ((10))
#define PS2_CLK        9  //17  ((12))

in const byte....