Claw Programing Problems

Salutations!

We are coding a robot (her name is Artemis) and she's giving us some trouble. Her arm and claw aren't working, and we can't make any sense of it. We just found out that we are going to state for the WA SkillsUSA robotics competition in late March, so we are trying to get everything up and ready. We are very new to coding, so our issues could probably just be something we're not catching because we're new to this.

Artemis uses Pitsco's Tele-Op, DC Motor Expansion Controller, and PRIZM Brain. We're using Arduino IDE with the Uno library to code her. Her servo motors for the claw are plugged into CR1 and CR2. We have tried switching the wires around, but that wasn't it either.

There are no errors coming up when we compile and upload the program, but it's not working when we try to use our controller. Our concern is that this code isn't compatible with our joystick (ps4), but it could easily be something else that we're not catching. Any wisdom available would be greatly appreciated!


//Original code by Ryan Chan. Updated code by MilesPeterson101 Thanks  to Ryan Chan for the concept!

#include <Servo.h>

Servo servo1; //Servos
Servo  servo2;
Servo servo3;

const int LED1 = 2; //LEDs
const int LED2 = 3;
const  int LED3 = 4;
const int LED4 = 7;
const int LED5 = 8;

const int button1  = 12; //Buttons
const int button2 = 13;

int button1Presses = 0; //Button  values
boolean button2Pressed = false;

const int pot1 = A0; //Potentimeters
const  int pot2 = A1; 
const int pot3 = A2;

int pot1Val; //Potentimeter values
int  pot2Val;
int pot3Val;
int pot1Angle;
int pot2Angle;
int pot3Angle;

int  servo1PosSaves[] = {1,1,1,1,1}; //position saves
int servo2PosSaves[] = {1,1,1,1,1};
int  servo3PosSaves[] = {1,1,1,1,1};

 void setup() {
  servo1.attach(5); //  Set up everything and will run once; attach servos and define the pin modes
  servo2.attach(6);
  servo3.attach(9);
  
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);

  Serial.begin(9600);
}

void loop() {
  // put your main code here,  to run repeatedly: 
  pot1Val = analogRead(pot1); // This will read the values  from the potentimeters and store it...
  pot1Angle = map(pot1Val, 0, 1023, 0,  179); // ... and this will map the values from the potentiometers to values the  servos can use and store it for later use
  pot2Val = analogRead(pot2); 
  pot2Angle = map(pot2Val, 0, 1023, 0, 179);
  pot3Val = analogRead(pot3);
  pot3Angle = map(pot3Val, 0, 1023, 0, 179);
  
  servo1.write(pot1Angle);  // These will make the servos move to the mapped angles
  servo2.write(pot2Angle);
  servo3.write(pot3Angle);
  
  if(digitalRead(button1) == HIGH){ // This  will check how many times button1 is pressed and save the positions to an array  depending on how many times it is pressed; switch/case works like a if statement
    button1Presses++;
    switch(button1Presses){
      case 1:
        servo1PosSaves[0]  = pot1Angle;
        servo2PosSaves[0] = pot2Angle;
        servo3PosSaves[0]  = pot3Angle;
        digitalWrite(LED1, HIGH);
        Serial.println("Pos  1 Saved");
        break;
      case 2:
         servo1PosSaves[1] = pot1Angle;
         servo2PosSaves[1] = pot2Angle;
         servo3PosSaves[1] = pot3Angle;
         digitalWrite(LED2, HIGH);
         Serial.println("Pos 2 Saved");
         break;
      case 3:
         servo1PosSaves[2] = pot1Angle;
         servo2PosSaves[2] = pot2Angle;
         servo3PosSaves[2] = pot3Angle;
         digitalWrite(LED3, HIGH);
         Serial.println("Pos 3 Saved");
         break;
       case 4:
         servo1PosSaves[3] = pot1Angle;
         servo2PosSaves[3] = pot2Angle;
         servo3PosSaves[3] = pot3Angle;
         digitalWrite(LED4, HIGH);
         Serial.println("Pos 4 Saved");
         break;
       case 5:
         servo1PosSaves[4] = pot1Angle;
         servo2PosSaves[4] = pot2Angle;
         servo3PosSaves[4] = pot3Angle;
         digitalWrite(LED5, HIGH);
         Serial.println("Pos 5 Saved");
         break;
    }
  }

  if(digitalRead(button2) == HIGH){ // Pretty  self-explnatory here
    button2Pressed = true;   
  }
  
  if(button2Pressed){  // if the boolean button2Press is true, then the servos will run though all their  saved positions
    for(int i = 0; i < 5; i++){
        servo1.write(servo1PosSaves[i]);
        servo2.write(servo2PosSaves[i]);
        servo3.write(servo3PosSaves[i]);
        Serial.println(" potentimeter Angles: ");
        Serial.println(servo1PosSaves[i]);
        Serial.println(servo2PosSaves[i]);
        Serial.println(servo3PosSaves[i]);
        delay(1050);
      }
  }
  delay(300);
}

It would help us help if you posted an annotated schematic showing exactly how you have wired this, include all power sources, grounds, and any other extra items connected. Also post links to the hardware items data sheet.

That description points to the fact that you did NOT develop your code in steps for each connected device so you could find the errors in one device logic before you added the next device.
Now the only way to debug is to begin scattering serial.Print() statements in the code so you can now discover where the logic problem is located. Since you already have some print statements, why not tell us what you have discovered already. No guesses allowed!

1 Like

What exactly does this mean? It does nothing at all, it moves in uncontrollable jerks, it goes up and down when you want it to go left and right?

Our problem is that it simply does nothing at all. The code is correctly compiled and uploaded, but her arm and claw aren’t moving at all.

Then use serial.Print() to discover where the problem is located. Right now you are asking strangers to guess.

Say your loop loops 1000 times per second. What value do you think 'button1Presses' will have after you press the button for just 0.25 seconds?

What would happen if you pressed the button just 6 times?

What would happen if you pressed it during one of your delay() statements?

Do you see anything printed on the serial monitor?

See post 2.

(no controller is used)

Apologies for the late response, I will try to post an annotated schematic today.

Apologies for the late response, but I've just had a 'eureka' moment (thank you for prompting us to think about this). We're going to experiment with that feature to see if there is any effect when the 'button press' is altered.

Apologies for the late response, thank you for the idea. We'll be trying this out today.

Hello again! We are coding a robot (her name is Artemis) and she's giving us some trouble. We recently found out that we are going to state for the WA SkillsUSA robotics competition in late March, so we are trying to get everything up and ready. We are very new to coding, so our issues could probably just be something we're not catching because we're new to this.

Artemis uses Pitsco's Tele-Op, DC Motor Expansion Controller, and PRIZM Brain. We're using Arduino IDE with the Uno library to code her. Her servo motors for the claw are plugged into servo pins 5 & 6 on the PRIZM brain.

We're trying to code two servo motors to be controlled by the right joystick of the PS4 controller. We know the PS4 is correctly paired because we can drive her, but we know we're missing something in our code for the arm and claw. We are trying to get the motors to respond to the controller, because we have to be able to use the controller far away from the robot during the competition. Is there some mistake that we're overlooking?

#include <TELEOP.h>

#include <Servo.h>

#include <PRIZM.h>

PRIZM prizm; // Defining the PRIZM

PS4 ps4; // Defining the PS4 Controller

Servo servo1; // Defining Servos

Servo servo2; // Defining the Servos

const int servo1Pin = 5; // Defining the Servo Pin

const int servo2Pin = 6; // Defining the Servo Pin

void setup() {

prizm.PrizmBegin();

prizm.setServoSpeed(1,25); // Set Servo 1 Power to 25%

prizm.setServoSpeed(2,25); // Set Servo 2 power to 25%

servo1.attach(5); // Declaring where the Sevos are in the PRIZM/Pin

servo2.attach(6); // Declaring where the Servos are in the PRIZM/Pin

}

void loop() {

// Read the right joystick values from the PS4 controller

// Replace with actual function names from the PS4Controller library

ps4.getPS4();

int servo1position(180);

int rightJoystickX = 180;

int servo2position(90);

int rightJoystickY = 90;

// Map the joystick values to servo motor positions

ps4.getPS4();

int servo1Position = map(90, -128, 127, 0, 180);

int servo2Position = map(90, -128, 127, 0, 180);

// Write the servo motor positions

ps4.getPS4();

servo1.write(0); // Set Servo 1 to 0 Degrees

servo2.write(90); // Set Servo 2 to 90 Degrees

}

Continued in part two of new post. Thank you all for the helpful ideas. We're working on our own new code now (still struggling) but we're in the learning process!

Your two or more topics on the same or similar subject have been merged.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.