help with programming a robotic arm on a movable cart

I would like to make a robot arm on a cart on the ground and driven by three steppers using drv8825 drivers that move the arm , two dc motors using a TB6612FNG 1A dual motor driver to drive the cart back and forwards, left and right and two servos for the gripper for opening and closing and tilting up and down. I would also use a ping sensor to measure the distance to an object so that the cart automaticly remains at a distance from it.
Has someone an idea how I can tackle something like this?
I would like to bluetooth serial connection that can be controlled by serial commands that can be assigned to keys on my android. i already have an app that can send serial commands.
I had already thought of something but it proved very inefficient and there are also some unused functions:

#include <NewPing.h>
#include <Servo.h>
int motor1=52;
int motor2=53;
int step1=11;
int dir1=7;
int step2=10;
int dir2=6;
int step3=12;
int dir3=5;
#define TRIGGER_PIN 41
#define ECHO_PIN 42
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
Servo myservo;
int pos = 0;

char data;

void setup() 
{
  Serial.begin(9600);
  analogWrite(motor1, 0);
  analogWrite(motor2, 0);
  myservo.attach(9);
  if(Serial.available())
  {
    data = Serial.read();
  }
}

void loop() 
{
  delay(29); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  if (uS / US_ROUNDTRIP_CM < 20)
  {
    if (Serial.available())
    {
      int var = Serial.read();
      if(var == '0')
      {
        myservo.write(135);
      }
      if (var == '1')
      {
        myservo.write(45);
      }
      if ( data == '4')
      {
        analogWrite(step1, 1);
        digitalWrite(dir1, LOW);
      }
      if ( data == '5')
      {
        analogWrite(step1, 1);
        digitalWrite(dir1, HIGH);
      }
      if (data == 'a')
      { 
        digitalWrite(step1, LOW);
      }
      if ( data == '6')
      {
        analogWrite(step2, 1);
        digitalWrite(dir2, LOW);
      }
      if (data == '7')
      {
        analogWrite(step2, 1);
        digitalWrite(dir2, HIGH);
      }
      if (data =='b')
      {
        digitalWrite(step2, LOW);
      }
      if ( data == '8')
      {
        analogWrite(step3, 1);
        digitalWrite(dir3, LOW);
      }
      if (data == '9')
      {
        analogWrite(step3, 1);
        digitalWrite(dir3, HIGH);
      }
      if (data == 'c')
      { 
        digitalWrite(step3, LOW);
      }
    }
  } 
  if (uS / US_ROUNDTRIP_CM > 20)
  {
    if (Serial.available())
    {
      digitalWrite(step1, LOW);
      digitalWrite(step2, LOW);
      digitalWrite(step3, LOW);
      if (data == '1')
      {
        analogWrite(motor1, 255);
        analogWrite(motor2, 255);
      }
      if ( data == '0')
      {
        analogWrite(motor1, 0);
        analogWrite(motor2, 0);
      }
      if ( data == '2')
      {
        analogWrite(motor1, 255);
        analogWrite(motor2, 10);
      }
      if ( data == '3')
      {
        analogWrite(motor1, 10);
        analogWrite(motor2, 255);
      }
    }
  }
}

does anyone know how to do this in a better way.

with kind regards
Camiel Nijpels

Has someone an idea how I can tackle something like this?

One step at a time.

I would like to bluetooth serial connection that can be controlled by serial commands that can be assigned to keys on my android.

That is the LAST thing to add, when the arm and cart are moving correctly.

  Serial.begin(9600);
  analogWrite(motor1, 0);
  analogWrite(motor2, 0);
  myservo.attach(9);
  if(Serial.available())
  {
    data = Serial.read();
  }

Why would there be serial data available a few nanoseconds after you open the serial connection?

  delay(29); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.

If you are going to have useless comments, you MUST keep them accurate.

  if (uS / US_ROUNDTRIP_CM < 20)
  {
    if (Serial.available())
    {

If it’s about to crash into something, see if there is any serial data. Why?

  } 
  if (uS / US_ROUNDTRIP_CM > 20)
  {

If it’s exactly 20 centimeters away, do nothing. I don’t get that.

int step1=11;
int step2=10;
int step3=12;

Wouldn’t armPin, wristPin, etc. make more sense? If these are the stepper motors, I’m surprised that analogWrite() does anything to them.

    if (Serial.available())
    {
      int var = Serial.read();
      if(var == '0')
      {
        myservo.write(135);
      }
      if (var == '1')
      {
        myservo.write(45);
      }
      if ( data == '4')
      {
        analogWrite(step1, 1);
        digitalWrite(dir1, LOW);
      }

Why are you testing "var" and then "data"? "data" will almost certainly have nothing in it (except zero).

  if (uS / US_ROUNDTRIP_CM > 20)
  {
    if (Serial.available())
    {
...
    }

If serial data is available, don't read it?

hi everyone,
thanks for the quick responses.
i’m pretty new to programming so don’t know much about it.
can you give me some advise on how to do it in a good way.
i know that allot of the code i wrote isn’t correct, i hoped you could help me with that.
i’ve changed some of the code already to see if this could work or not:

void setup() 
{
  Serial.begin(9600);
  analogWrite(motor1, 0);
  analogWrite(motor2, 0);
  myservo.attach(9);

i removed the serial read from the

{
  delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  if (uS / US_ROUNDTRIP_CM < 20)
  {
    if (Serial.available())
    {
      data = Serial.read();
      {
        if(data == '0')
        {
          myservo.write(135);
        }

and also here

  if (uS / US_ROUNDTRIP_CM > 20)
  {
    if (Serial.available())
    {
      data = Serial.read();
      {
        digitalWrite(step1, LOW);
        digitalWrite(step2, LOW);
        digitalWrite(step3, LOW);

Have you looked at Planning and Implementing a Program ?

I think you mentioned 5 different parts to your project

  • Stepper motors to move the arm
  • DC motors for the wheels and steering
  • Servos for gripping
  • Ping sensor for distance detection
  • Bluetooth for control.

You need to write separate programs for each of these parts and get them working properly on their own before you even think of integrating them into a single project.

I suspect that servos would be much more suitable, and easier to program and use, for the arm movements.

How is the steering going to work? Differential control of two DC motors will almost certainly require encoders and the associated programming complexity. A steered wheel would probably be much easier to implement.

And under no circumstances use the delay() function for timing anything. Use millis() as in the above link.

…R