Arduino program to control a robot arm to detect, pick up, and sort colored sponges

Hello everyone. I am completely new in this forum and glad to be!
Please I need help. I have a project that I have been trying how to solve.
The project is to integrate an ultrasonic sensor and color sensor with a robot arm using Arduino. The ultrasonic sensor is to control the robot arm to detect and pick the colored sponge and then bring the sponge to the color sensor which will in turn detect the colored sponge and thereafter place the colored sponge at a specified place depending on the color.
Need serious help please.
Thank you

Welcome to the forum

What electronics and programming experience do you have ? Is this a project that you have decided to do yourself or perhaps a school/college assignment ?

Either way, what have you done so far and which Arduino board and sensors will you be using ?

I have little experience in C. Its a school/college assignment.
I have programmed the robot arm to pick and place the sponge. What remains are ultrasonic sensor and color sensor integration. Thank you.

Actually your first project to to discover that ultrasonic energy will be absorbed by the sponge, NOT reflected back to the sensor. Sorry.

1 Like

Save us a lot of guessing and wasting time post your annotated schematic showing exactly how you have wired this. Note any leads over 10"/25 cm in length. Also post your code using code tags. That sounds like an interesting project that could be a lot of fun! However, please keep in mind that we are not a free design or code-writing service. We’re more than happy to help with your design or code, but we need you to make an initial attempt. Please design and write your code, then post it along with an explanation of what’s not working properly.

  1. Show Your Work First: Before asking for assistance, make an attempt to design or write the code yourself. Share your work along with details about what isn’t working.
  2. Provide Clear Documentation: Since we can’t see your project, share an annotated schematic (best) or a clear drawing of your setup. Pictures are welcome, but avoid using Fritzing diagrams as they are wiring diagrams, not schematics, and are not ideal for troubleshooting.
  3. Include Technical Details: If there is specific hardware involved, include links to technical information. There are often many versions of similar components, so precise details are essential.
  4. Reference Resources: For additional help, check out useful links and tutorials: Useful Links on Arduino Forum.

Consider an optical distance/presence/proximity sensor. Pololu has the best selection.

#include <Servo.h>
#include <Braccio.h>

Servo base;
Servo shoulder;
Servo elbow;
Servo wrist_rot;
Servo wrist_ver;
Servo gripper;

// Define pins for ultrasonic sensor and IR sensor
// defines pins numbers
const int trigPin = 24;
const int echoPin = 25;
// defines variables
float duration;
float distance;

void setup() {
// Initialize serial communication
//Initialization functions and set up the initial position for Braccio
//All the servo motors will be positioned in the "safety" position:
//Base (M1):90 degrees
//Shoulder (M2): 45 degrees
//Elbow (M3): 180 degrees
//Wrist vertical (M4): 180 degrees
//Wrist rotation (M5): 90 degrees
//gripper (M6): 10 degrees
Braccio.begin();
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication

}

void loop() {

// Clears the trigPin
digitalWrite(trigPin, LOW);
delay(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delay(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration * 0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
// Check if object is in range
if (distance > 0 && distance < 20) {

  // the arm is aligned upwards  and the gripper is closed
                 //(step delay, M1, M2, M3, M4, M5, M6);

Braccio.ServoMovement(20, 90, 90, 90, 90, 90, 73);
delay(1000);

Braccio.ServoMovement(20, 90,120,180,120,100,10); //the arm is aligned download and the gripper is open to pick the sponge
delay(1000);

Braccio.ServoMovement(20, 90,120,180,120,100,70); //the arm is aligned download and the gripper is closed, picked the sponge
delay(1000);

Braccio.ServoMovement(20, 90, 70, 170, 90, 80, 70); //the arm is alligned upwards and the gripper is holding the sponge
delay(1000);

Braccio.ServoMovement(20, 170, 60, 170, 90, 70, 70); // the arm rotates at angle 170 while holding the sponge
delay(1000);

Braccio.ServoMovement(20, 170,120,170,120,100,70); //the arm is aligned download and the gripper is closed, still holding the sponge
delay(1000);

Braccio.ServoMovement(20,         170,120,170,120,100,10);  //the arm is aligned download  and the gripper is open, dropped the sponge

delay(1000);

}
else{
Braccio.ServoMovement(20, 90, 90, 90, 90, 90, 73);
delay(1000);
}

}

This is what I have been able to do

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Please post your sketch, using code tags when you do

Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

It is also helpful to post error messages in code tags as it makes it easier to scroll through them and copy them for examination

I am having a hard time following your invisible annotated schematic. If you had followed the forum guidelines and used code tags I would have been able to read your code. See @UKHeliBob post 10.

Very sorry about that. I will do my best to follow to guidelines and my codes across.

Thank you so much for your effort.

#include <Servo.h>
#include <Braccio.h>
Servo base;
Servo shoulder;
Servo elbow;
Servo wrist_rot;
Servo wrist_ver;
Servo gripper;
// Define pins for ultrasonic sensor and IR sensor
// defines pins numbers
const int trigPin = 24;
const int echoPin = 25;
// defines variables
float duration;
float distance;
void setup() {
  // Initialize serial communication
  //Initialization functions and set up the initial position for Braccio
  //All the servo motors will be positioned in the "safety" position:
  //Base (M1):90 degrees
  //Shoulder (M2): 45 degrees
  //Elbow (M3): 180 degrees
  //Wrist vertical (M4): 180 degrees
  //Wrist rotation (M5): 90 degrees
  //gripper (M6): 10 degrees
  Braccio.begin();
  pinMode(trigPin, OUTPUT);  // Sets the trigPin as an Output
  pinMode(echoPin, INPUT);   // Sets the echoPin as an Input
  Serial.begin(9600);        // Starts the serial communication
}
void loop() {
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delay(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delay(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2;
  // Prints the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.println(distance);
  // Check if object is in range
  if (distance > 0 && distance < 20) {
    // the arm is aligned upwards  and the gripper is closed
    //(step delay, M1, M2, M3, M4, M5, M6);
    Braccio.ServoMovement(20, 90, 90, 90, 90, 90, 73);
    delay(1000);
    Braccio.ServoMovement(20, 90, 120, 180, 120, 100, 10);  //the arm is aligned download and the gripper is open to pick the sponge
    delay(1000);
    Braccio.ServoMovement(20, 90, 120, 180, 120, 100, 70);  //the arm is aligned download and the gripper is closed, picked the sponge
    delay(1000);
    Braccio.ServoMovement(20, 90, 70, 170, 90, 80, 70);  //the arm is alligned upwards and the gripper is holding the sponge
    delay(1000);
    Braccio.ServoMovement(20, 170, 60, 170, 90, 70, 70);  // the arm rotates at angle 170 while holding the sponge
    delay(1000);
    Braccio.ServoMovement(20, 170, 120, 170, 120, 100, 70);  //the arm is aligned download and the gripper is closed, still holding the sponge
    delay(1000);
    Braccio.ServoMovement(20, 170, 120, 170, 120, 100, 10);  //the arm is aligned download  and the gripper is open, dropped the sponge
    delay(1000);
  } else {
    Braccio.ServoMovement(20, 90, 90, 90, 90, 90, 73);
    delay(1000);
  }
}

When you post the schematic we will see what processor you are using. In place of delay I suggest you do it with non blocking code such as mills.

Why not mount the color sensor on the arm and use it as the detector? You'll know right away which destination is needed for the sponge - as long as none of the sponges is the same color as the background! :grinning_face:

Place blue at x1, y1, z1; red at x2, y2, z2, etc.

What's the budget? Pixy2 – PixyCam

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.