For the redesign, I will be focusing on the coding and aesthetics of the robot.
My ultimate goal with this robot is that it successfully sees an object (preferably the cup), closes the claw around it, and will be able to drive around with it.
Today I started compiling the code. Basically, it is supposed to make the servo turn ninety degrees once it sees an object that is within reach of the claw, about 15 centimeters.
This is my first try at the code.
At first I didn't understand what the comment was telling me but it turns out I was using the old version of Arduino software, which doesn't support the code. Make sure you use Arduino 2.
After a significant amount of trial and error, I finally got the code to work.
Here it is:
#include <Servo.h>
#define trigpin 5//set trigpin
#define echopin 6//set echopin
Servo myservo;// declare servo name type servo
int duration, distance;//declare variable for unltrasonic sensor
void setup() {
Serial.begin(9600);
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
myservo.attach(2);// attach your servo
myservo.writeMicroseconds(1500);
// put your setup code here, to run once:
}
void loop() {
myservo.write(90);// always set servo to 90 to position it to the middle
//ultrasonic code
digitalWrite(trigpin,HIGH);
_delay_ms(10000);// change the delay to make claw hold objects longer
digitalWrite(trigpin, LOW);
duration=pulseIn(echopin,HIGH);
distance=(duration/2)/29.1;
if(distance <=15)// if ultrasonic sensor detects an obstacle less than 1cm in 90 degree angle.
{
myservo.write(0); //servo rotates at full speed to the right
delay(600);
}
else
{
myservo.write(90);// else servo stays at 90 degree angle.
delay(600);
}
Serial.print("cm"); //print distance unit cm
Serial.println(distance);//distance
// put your main code here, to run repeatedly:
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Basically, what it is telling the claw to do is open and close when it sees something that is 15 cm away, hold the closed position for 10 seconds, then open and close again. (The numbers in bold can be changed if you want the claw to hold an object longer or shorter than 10 seconds, or see something that is closer or farther away than 15 cm.)
In order for the code and the circuit to match up and work, it is crucial that the pins on the board correspond with what it says in the code.
For the ultrasonic sensor:
For the servo:
This is the wiring. If you are reproducing it, make sure everything is lined up. (Wired up!)
Now that the code it done, I must work on connecting the servo to the robot in a more efficient and aesthetically pleasing way (than duck tape).
I decided to 3D print a piece that would hold the servo in place and would be screwed into the robot.
What it looks like on Tinkercad.
The printer's progress-- so mesmerizing that I spent two minutes of my life watching liquid plastic coming out of an extruder.
The finished print. Now it's time to put the servo in and screw it up again. (I'm not a big fan of screws... )
Final screwing...
The test run: it takes a while for the robot to see the object with the ultrasonic sensor (an aspect of further redesign), but once it does, it opens the claw, grabs the object, and closes!
The final run! The robot claw works, and it can grab an object and bring it back! It would be exciting to see something like this used in hospitals, where robots would be able to bring tools and materials to doctors or dentists (of course the robot would need a much greater amount of precision!)