I need help with arduino coding for clawbot to grasp a cup using a sensor.

Dear Arduino community:

I need help with making an arduino code for a clawbot to grasp a cup using an ultrasonic sensor. We wrote the following code below for our clawbot to be able to follow a black line using two sensors at the bottom.

If anyone can improve on this code below to help me make my clawbot grasp a cup while still being able to sense a black line track, please write it into this coding I have so far below. It took our group hours to figure out how to make our robot sense the black line.

Sincerely,
Z

Line Following Code (that needs a grasping sensing code added in for our clawbot):

int leftInput = 3;
int rightInput = 2;
int leftMotor = 9;
int rightMotor = 5;
boolean leftValue;
boolean rightValue;
void setup()
{
Serial.begin(9600);
pinMode (leftMotor, OUTPUT);
pinMode (rightMotor, OUTPUT);
pinMode (10, OUTPUT);
pinMode(6, OUTPUT);
digitalWrite(6, LOW);
digitalWrite(10, LOW);

}
void loop()
{
leftValue = digitalRead (leftInput);
rightValue = digitalRead (rightInput);

Serial.print("Left sensor ");
Serial.println(leftValue);
Serial.print("Right sensor ");
Serial.println(rightValue);

if ( leftValue == 1 && rightValue == 1)
{
digitalWrite (leftMotor, HIGH);
digitalWrite (rightMotor, HIGH);
}
else if (leftValue == 0 && rightValue == 1) { //Turns left
digitalWrite (leftMotor,LOW);
digitalWrite (rightMotor, HIGH);
}
else if (leftValue == 1 && rightValue == 0) { //Turns right
digitalWrite (leftMotor, HIGH);
digitalWrite (rightMotor, LOW);
}
else{
digitalWrite (leftMotor,LOW);
digitalWrite (rightMotor, HIGH);
delay(500);
digitalWrite (leftMotor, HIGH);
digitalWrite (rightMotor, HIGH);
delay(750);
digitalWrite (leftMotor, HIGH);
digitalWrite (rightMotor, LOW);
delay(500);
digitalWrite (leftMotor, HIGH);
digitalWrite (rightMotor, HIGH);
delay(750);

}

}

Welcome to the Forum. Please read these two posts:

How to use this forum - please read.
and
Read this before posting a programming question …
You may also find useful information that would answer your question here:
Useful links - check here for reference posts / tutorials

You have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don’t do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons.
If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

Before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read.

I think your specification needs some more detail. For example how are you going to know that there's a cup somewhere to be grasped? And there's nothing in there to explain how the claw, if it exists, can be opened or closed or directed to any particular position.

I expect you know what your "clawbot" looks like, what sensors, motors and servos it has and how it's all connected to an Arduino but my mind-reading skills aren't up to it I'm afraid.

The basic "flow chart" is easy enough:

  • Open claw
  • Find cup
  • Move to correct distance from cup
  • Close claw just the right amount
  • Game over!

You just need to convert that into code.

Steve

slipstick:
my mind-reading skills aren't up to it I'm afraid.

No crystal ball either? :smiley:

slipstick:
The basic "flow chart" is easy enough:

  • Open claw
  • Find cup
  • Move to correct distance from cup
  • Close claw just the right amount
  • Game over!

You just need to convert that into code.

Steve

You make it sound so easy. :smiley:

These newbies!

zbrown:
It took our group hours to figure out how to make our robot sense the black line.

There is nothing unusual in that. Some of my projects have taken me weeks to get working.

Now you need to spend some more hours trying to figure out the claw business.

You will almost certainly need to get rid of all those delay()s so have a look at the demo Several Things at a Time which illustrates the use of millis() to manage timing without blocking.

Also look at Planning and Implementing a Program

Then look carefully at Reply #2 which covers all the essentials for your project. Just extend the list that @slipstick has provided so that each part is outlined in full detail.

...R