Viewing 1 post (of 1 total)
  • Author
    Posts
  • #3241
    Anonymous
    Inactive

    I’m not getting my ps2 control work correctly. appears that the control was not found, where and how I declare the pins of the receiver? This here is the code:
    <pre style=”color: #000000; line-height: normal; word-wrap: break-word; white-space: pre-wrap;”>#include <PS2X_lib.h>
    #include <Servo.h>

    PS2X ps2x;

    Servo headPan;
    Servo headTiltR;
    Servo headTiltL;
    Servo torsoPan;
    Servo torsoBent;
    Servo shoulderR1;
    Servo shoulderL1;
    Servo shoulderR2;
    Servo shoulderL2;
    Servo elbowR;
    Servo elbowL;
    Servo handR;
    Servo handL;

    float a = 1;
    float b = 1;
    float c = 1;
    float d = 1;
    float e = 1;
    float f = 1;
    float g = 1;
    float h = 1;
    float i = 1;
    float j = 1;
    float k = 1;
    float l = 1;
    float m = 1;

    int valLX;
    int valLY;
    int valRX;
    int valRY;

    int error = 0;
    byte type = 0;
    byte vibrate = 0;

    int motor1Pin1 = 30; //don’t need to be PWM
    int motor1Pin2 = 31; //don’t need to be PWM
    int motor1enablePin = 2; //must be PWM

    int motor2Pin1 = 32; //don’t need to be PWM
    int motor2Pin2 = 33; //don’t need to be PWM
    int motor2enablePin = 3; //must be PWM

    void setup()
    {
    error = ps2x.config_gamepad(34,35,36,37); //setup GamePad(clock, command, attention, data) pins
    if(error == 0){}

    headPan.attach(38);
    headPan.write(88);

    headTiltR.attach(39);
    headTiltR.write(95);

    headTiltL.attach(40);
    headTiltL.write(105);

    torsoPan.attach(41);
    torsoPan.write(70);

    torsoBent.attach(42);
    torsoBent.write(5);

    shoulderR1.attach(43);
    shoulderR1.write(100);

    shoulderL1.attach(44);
    shoulderL1.write(90);

    shoulderR2.attach(45);
    shoulderR2.write(75);

    shoulderL2.attach(46);
    shoulderL2.write(95);

    elbowR.attach(47);
    elbowR.write(130);

    elbowL.attach(48);
    elbowL.write(75);

    handR.attach(49);
    handR.write(90);

    handL.attach(50);
    handL.write(160);

    pinMode(motor1Pin1, OUTPUT);
    pinMode(motor1Pin2, OUTPUT);
    pinMode(motor1enablePin, OUTPUT);

    pinMode(motor2Pin1, OUTPUT);
    pinMode(motor2Pin2, OUTPUT);
    pinMode(motor2enablePin, OUTPUT);

    a = 88;
    b = 95;
    c = 105;
    d = 70;
    // e = 5;
    f = 100;
    g = 90;
    h = 75;
    i = 95;
    j = 130;
    k = 75;
    l = 90;
    m = 160;
    }

    void loop()
    {
    if(error == 1)
    return;

    else
    {
    ps2x.read_gamepad();

    if (ps2x.Button(PSB_PAD_UP))
    {
    analogWrite(motor1enablePin, 200);
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, HIGH);

    analogWrite(motor2enablePin, 200);
    digitalWrite(motor2Pin1, HIGH);
    digitalWrite(motor2Pin2, LOW);
    }

    if (ps2x.Button(PSB_PAD_DOWN))
    {
    analogWrite(motor1enablePin, 200);
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);

    analogWrite(motor2enablePin, 200);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);
    }

    if (ps2x.Button(PSB_PAD_RIGHT))
    {
    analogWrite(motor1enablePin, 200);
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);

    analogWrite(motor2enablePin, 200);
    digitalWrite(motor2Pin1, HIGH);
    digitalWrite(motor2Pin2, LOW);
    }

    if (ps2x.Button(PSB_PAD_LEFT))
    {
    analogWrite(motor1enablePin, 200);
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, HIGH);

    analogWrite(motor2enablePin, 200);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);
    }

    if (ps2x.Button(PSB_PAD_UP) & ps2x.Button(PSB_PAD_RIGHT))
    {
    analogWrite(motor1enablePin, 125);
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, HIGH);

    analogWrite(motor2enablePin, 255);
    digitalWrite(motor2Pin1, HIGH);
    digitalWrite(motor2Pin2, LOW);
    }

    if (ps2x.Button(PSB_PAD_UP) & ps2x.Button(PSB_PAD_LEFT))
    {
    analogWrite(motor1enablePin, 255);
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, HIGH);

    analogWrite(motor2enablePin, 125);
    digitalWrite(motor2Pin1, HIGH);
    digitalWrite(motor2Pin2, LOW);
    }

    if (ps2x.Button(PSB_PAD_DOWN) & ps2x.Button(PSB_PAD_RIGHT))
    {
    analogWrite(motor1enablePin, 125);
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);

    analogWrite(motor2enablePin, 255);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);
    }

    if (ps2x.Button(PSB_PAD_DOWN) & ps2x.Button(PSB_PAD_LEFT))
    {
    analogWrite(motor1enablePin, 255);
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);

    analogWrite(motor2enablePin, 125);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, HIGH);
    }

    if (ps2x.ButtonReleased(PSB_PAD_DOWN) || ps2x.ButtonReleased(PSB_PAD_UP) || ps2x.ButtonReleased(PSB_PAD_RIGHT) || ps2x.ButtonReleased(PSB_PAD_LEFT))
    {
    analogWrite(motor1enablePin, 0);
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, LOW);

    analogWrite(motor2enablePin, 0);
    digitalWrite(motor2Pin1, LOW);
    digitalWrite(motor2Pin2, LOW);
    }

    valLX = (ps2x.Analog(PSS_LX));

    if ((ps2x.Button(PSB_TRIANGLE)) & (valLX >= 156))
    {
    a+=1.7;
    }

    if ((ps2x.Button(PSB_TRIANGLE)) & (valLX <= 89))
    {
    a-=1.7;
    }

    valLY = (ps2x.Analog(PSS_LY));

    if ((ps2x.Button(PSB_TRIANGLE)) & (valLY >= 156))
    {
    b-=1.7;
    c+=1.7;
    }

    if ((ps2x.Button(PSB_TRIANGLE)) & (valLY <= 89))
    {
    b+=1.7;
    c-=1.7;
    }

    valLX = (ps2x.Analog(PSS_LX));

    if ((ps2x.Button(PSB_SQUARE)) & (valLX >= 156))
    {
    a+=2.0;
    d+=1.7;
    }

    if ((ps2x.Button(PSB_SQUARE)) & (valLX <= 89))
    {
    a-=2.0;
    d-=1.7;
    }

    // valLY = (ps2x.Analog(PSS_LY));
    //
    // if ((ps2x.Button(PSB_SQUARE)) & (valLY >= 156))
    // {
    // e-=0.5;
    // }
    //
    // if ((ps2x.Button(PSB_SQUARE)) & (valLY <= 89))
    // {
    // e+=0.5;
    // }
    //

    valRY = (ps2x.Analog(PSS_RY));

    if ((ps2x.Button(PSB_R1)) & (valRY >= 156))
    {
    f+=1.5;
    }

    if ((ps2x.Button(PSB_R1)) & (valRY <= 89))
    {
    f-=1.5;
    }

    valLY = (ps2x.Analog(PSS_LY));

    if ((ps2x.Button(PSB_L1)) & (valLY >= 156))
    {
    g-=1.5;
    }

    if ((ps2x.Button(PSB_L1)) & (valLY <= 89))
    {
    g+=1.5;
    }

    valRX = (ps2x.Analog(PSS_RX));

    if ((ps2x.Button(PSB_R1)) & (valRX >= 156))
    {
    h-=1.5;
    }

    if ((ps2x.Button(PSB_R1)) & (valRX <= 89))
    {
    h+=1.5;
    }

    valLX = (ps2x.Analog(PSS_LX));

    if ((ps2x.Button(PSB_L1)) & (valLX >= 156))
    {
    i-=1.5;
    }

    if ((ps2x.Button(PSB_L1)) & (valLX <= 89))
    {
    i+=1.5;
    }

    valRY = (ps2x.Analog(PSS_RY));

    if ((ps2x.Button(PSB_R2)) & (valRY >= 156))
    {
    j-=1.5;
    }

    if ((ps2x.Button(PSB_R2)) & (valRY <= 89))
    {
    j+=1.5;
    }

    valLY = (ps2x.Analog(PSS_LY));

    if ((ps2x.Button(PSB_L2)) & (valLY >= 156))
    {
    k+=1.5;
    }

    if ((ps2x.Button(PSB_L2)) & (valLY <= 89))
    {
    k-=1.5;
    }

    valRX = (ps2x.Analog(PSS_RX));

    if ((ps2x.Button(PSB_R2)) & (valRX >= 156))
    {
    l+=1.5;
    }

    if ((ps2x.Button(PSB_R2)) & (valRX <= 89))
    {
    l-=1.5;
    }

    valLX = (ps2x.Analog(PSS_LX));

    if ((ps2x.Button(PSB_L2)) & (valLX >= 156))
    {
    m+=1.5;
    }

    if ((ps2x.Button(PSB_L2)) & (valLX <= 89))
    {
    m-=1.5;
    }
    }

    int hi = 0;
    boolean test = false;

    if (a > 158) a=158;
    if (a < 18) a=18;

    if (b > 140) b=140;
    if (b < 60) b=60;

    if (c > 140) c=140;
    if (c < 60) c=60;

    if (d > 125) d=125;
    if (d < 15) d=15;

    // if (e > 45) e=45;
    // if (e < 5) e=5;

    if (f > 170) f=170;
    if (f < 30) f=30;

    if (g > 160) g=160;
    if (g < 20) g=20;

    if (h > 105) h=105;
    if (h < 5) h=5;

    if (i > 170) i=170;
    if (i < 70) i=70;

    if (j > 150) j=150;
    if (j < 40) j=40;

    if (k > 165) k=165;
    if (k < 55) k=55;

    if (l > 180) l=180;
    if (l < 80) l=80;

    if (m > 170) m=170;
    if (m < 70) m=70;

    headPan.write(a);
    headTiltR.write(b);
    headTiltL.write(c);
    torsoPan.write(d);
    // torsoBent.write(e);
    shoulderR1.write(f);
    shoulderL1.write(g);
    shoulderR2.write(h);
    shoulderL2.write(i);
    elbowR.write(j);
    elbowL.write(k);
    handR.write(l);
    handL.write(m);

    delay(10);

    }

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.