Midterm_Haotian Li

Drawing Robot

This robot can draw four different shapes according to the distance. The shapes are rectangle, trapezoid, transverse line and vertical line. It draws vertical lines when distance is between 27cm and 37 cm, transverse lines when distance is between 20cm and 27 cm, rectangle when distance is between 10 cm and 20 cm, trapezoid when distance is between 0 cm and 10 cm. The robot is sleeping when it lies down so it doesn’t work. It begins working when it stands up.

I use ultrasonic distance sensor to measure the distance and use three servos to control the arms and draw shapes. There is a triple-axis accelerometer inside to control it working or not.

I design the machinery structures and out looking of the robot in AI. Then use laser cutter to make all the components.

IMG_3281 IMG_3283 647C35D0-B629-42AA-BC74-290FD7DF29ED

Code_______________________

#define SERVOPINLIFT 2
#define SERVOPINLEFT 3
#define SERVOPINRIGHT 4

#define trigPin 9
#define echoPin 8

#include <Servo.h>

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

//ADXL 335
const int xInput = A0;
const int yInput = A1;
const int zInput = A2;

int xRawMin = 512;
int xRawMax = 512;

int yRawMin = 512;
int yRawMax = 512;

int zRawMin = 512;
int zRawMax = 512;

const int sampleSize = 10;

int led1=13;
int led2=12;

void setup()
{
analogReference(EXTERNAL);
Serial.begin(9600);
servo1.attach(SERVOPINLIFT); // lifting servo
servo2.attach(SERVOPINLEFT); // left servo
servo3.attach(SERVOPINRIGHT); // right servo
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);

servo1.write(25);
servo2.write(30);
servo3.write(150);

}

void loop()
{

int xRaw = ReadAxis(xInput);
int yRaw = ReadAxis(yInput);
int zRaw = ReadAxis(zInput);

int duration,distance;
digitalWrite(trigPin,HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin,LOW);
duration=pulseIn(echoPin,HIGH);
distance=(duration/2)/29.1;
Serial.println(distance);
delay(100);

if(zRaw>470){
digitalWrite(led1,HIGH);
digitalWrite(led2,HIGH);

//up down
if(distance>37){
servo2.write(30);
servo3.write(150);
delay(15);
}

if(distance>27 && distance<=37){
for(int i=20; i<50; i+=1)
{
servo2.write(i);
servo3.write(180-i);
delay(15);
}

for(int i=50; i>20; i-=1)
{
servo2.write(i);
servo3.write(180-i);
delay(15);
}
}

//left right
if(distance>20 && distance<=27){
for(int i=85; i>20; i-=1)
{
servo2.write(i);
servo3.write(180+i-85);
delay(15);
}
for(int i=20; i<85; i+=1)
{
servo2.write(i);
servo3.write(180+i-85);
delay(15);
}
}

//rectangle
if(distance>10 && distance<=20){
for(int i=85; i>45; i-=1)
{
servo2.write(i);
servo3.write(160+(0.37*(85-i)));
delay(15);
}
for(int i=45; i>15; i-=1)
{
servo2.write(i);
servo3.write(175+i-45);
delay(15);
}
for(int i=15; i<40; i+=1)
{
servo2.write(i);
servo3.write(145-i+15);
delay(15);
}
for(int i=40; i<85; i+=1)
{
servo2.write(i);
servo3.write(120+(i-35)*0.7);
delay(15);
}
}

//???
if(distance>0 && distance<=10){
for(int i=65; i>30; i-=1)
{
servo2.write(i);
servo3.write(168-(0.5*(65-i)));
delay(15);
}
for(int i=30; i>25; i-=1)
{
servo2.write(i);
servo3.write(150-4.4*(30-i));
delay(15);
}
for(int i=25; i<62.5; i+=1)
{
servo2.write(i);
servo3.write(128+0.4*(62.5-i));
delay(15);
}
for(int i=62.5; i<65; i+=1)
{
servo2.write(i);
servo3.write(143+10*(65-i));
delay(15);
}
}
}
if(zRaw<=470){
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
servo2.write(30);
servo3.write(150);
}

Serial.println(distance);
}

int ReadAxis(int axisPin)
{
long reading = 0;
analogRead(axisPin);
delay(1);
for (int i = 0; i < sampleSize; i++)
{
reading += analogRead(axisPin);
}
return reading/sampleSize;
}

Leave a Reply

Your email address will not be published.