Engineer

Eytan I

Area of Interest

Computer Engineering/Computer Science

School

SAR High School

Grade

Incoming Senior

Main Project: 4 Joint Robotic Arm

1st Milestone: Build structure and connect potentiometers

My name is Eytan and I am a rising senior at SAR High School. I chose to make a 4 joint robotic arm for my main project. I just completed my first milestone, finishing the structure of the arm, as well as making it controllable through 4 potentiometers and an arduino. A potentiometer is a three prong device that takes in power and ground, and lets out a certain amount of current. The way it does this is it acts as a variable resistor, allowing the user to choose how much current passes through. The current is connected to the arduino, and is read as a value between 0 and 1023. Next, the arduino converts this into a degree value (0-180) and communicates to the servo where to be. The robotic arm has 4 servos. There is a servo on the bottom that allows the arm base to move 180 degrees. The second servo allows the arm to move forwards and backwards. This allows it to reach out for the grab. A third servo controls vertical motion, and a last one allows for the claw to open and close. In my next milestones I hope to allow my robot to operate autonomously using ultra-sonic senors, as well as be controlled by an app.

Set Back

  1. The arm was shaking a lot and not staying still
  2. It glitched out often and would change positions rapidly and then come back rapidly

Solution

  1. I added code that instead of sending every value to the servo, the program added every seven values together and then sent the average of those numbers
  2. I also added code that if the command to the servo is 5 degrees more than a few milliseconds ago, it will cancel it as well

Diagram of potentiometer

Diagram of Potentiometer connection to Arduino

#include <Servo.h>

Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
Servo myservo3; // create servo object to control a servo
Servo myservo4; // create servo object to control a servo

int potpin1 = 0; // analog pin used to connect the potentiometer
int potpin2= A1; // analog pin used to connect the potentiometer
int potpin3= A2; // analog pin used to connect the potentiometer
int potpin4= A3;// analog pin used to connect the potentiometer

int val1=0; // variable to read the value from the analog pin
int val1avg=0; // variable to read the value from the analog pin
int val2=0; // variable to read the value from the analog pin
int val2avg=0; // variable to read the value from the analog pin
int val3=0; // variable to read the value from the analog pin
int val3avg=0; // variable to read the value from the analog pin
int val4=0; // variable to read the value from the analog pin
int val4avg=0;// variable to read the value from the analog pin
int timeslooped = 0; // variable to read the value from the analog pin

void setup() {
myservo1.attach(9); // attaches the servo1 on pin 9 to the servo object
myservo2.attach(10); // attaches the servo2 on pin 10 to the servo object
myservo3.attach(11); // attaches the servo3 on pin 11 to the servo object
myservo4.attach(3); // attaches the servo4 on pin 4 to the servo object

Serial.begin(9600);
}

void loop() {
timeslooped=timeslooped + 1;
val1 = val1+analogRead(potpin1); // reads the value of the potentiometer (value between 0 and 1023) and makes the value, the sum of seven
val2 = val2+analogRead(potpin2); // this is to make it jitter less because it is an average
val3 = val3+analogRead(potpin3);
val4= val4+analogRead(potpin4);
Serial.println(val1);
if (timeslooped>=7) //if seven values have been added
{
val1avg= val1/7; // take an average
val2avg=val2/7;
val3avg=val3/7;
val4avg=val4/7;

val1avg = map(val1avg, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180), using the average value
val2avg= map(val2avg,0,1023,60,180);
val3avg= map(val3avg,2,1023,0,180);
val4avg= map(val4avg,0,1023,60,180);

myservo1.write(val1avg); // sets the servo position according to the scaled value
myservo2.write(val2avg);
myservo3.write(val3avg);
myservo4.write(val4avg);

timeslooped=0; // reset loop
val1=0;
val2=0;
val3=0;
val4=0;
}
}

2nd Milestone: Code Arm To Work Autonomously 

For my second milestone, I added an autonomous function to my 4 joint arm. In order to do this, I used an ultra-sonic sensor and mounted it on the top of the arm. When a user clicks the “change mode” button, the arm moves to a position where it is close to the ground, parallel to it, and at “3 O’ Clock”. In order to make this happen, I coded a function which gradually increments a value until it reaches the goal value. Once the arm reaches its starting position, the arm then rotates counter-clockwise, while the ultra-sonic sensor searches for an object within 20 cm. If the sensor detects an object. Ultra-sonic sensor has two “eyes,” one for sending waves and one for receiving them. The Arduino communicates to one of the eyes to send waves at a very high rate, and when a wave bounces off of an object and returns to the second eye, a pulse is given back to the arduino. The arduino measures the amount of time it took for the wave to come back, and determines a distance based on that. If the sensor detects an object, the forward and up motors are incremented forward, until the distance measured is less than 4 cm. At that point, the next sequence begins and the claws close, and then the object is lifted into the air. One set back I had when coding this process was in the “gradual move” function. This function takes in two parameters and increments the first value until it reaches the second. Several times, the value I wanted to increment was a global variable, which cannot be changed when it is a parameter of a function. In order to solve this, I learned about pointers. I learned how to reference a space in memory, and change its content. This will help me with engineering projects in the future.

Ultra-sonic sensor

Ultra-sonic sensor attached to arduino

#include <Servo.h>

Servo myservo1; // create servo object to control a servo
Servo myservo2; // create servo object to control a servo
Servo myservo3; // create servo object to control a servo
Servo myservo4; // create servo object to control a servo

int potpin1 = 0; // analog pin used to connect the potentiometer
int potpin2= A1; // analog pin used to connect the potentiometer
int potpin3= A2; // analog pin used to connect the potentiometer
int potpin4= A3;// analog pin used to connect the potentiometer

int val1=0; // variable to read the value from the analog pin
int val1avg=0; // variable to read the value from the analog pin
int val1RevertTo; // variable that keeps value of potentiameter
int val2=0; // variable to read the value from the analog pin
int val2avg=0; // variable to read the value from the analog pin
int val2RevertTo; // variable that keeps value of potentiameter
int val3=0; // variable to read the value from the analog pin
int val3avg=0; // variable to read the value from the analog pin
int val3RevertTo ; // variable that keeps value of potentiameter
int val4=0; // variable to read the value from the analog pin
int val4avg=0;// variable to read the value from the analog pin
int val4RevertTo; // variable that keeps value of potentiameter
int timeslooped = 0; // variable to read the value from the analog pin

// For ultrasonic
const int trigPin = 13;
const int echoPin = 12;
// defines variables
long duration;
int distance;

int mode = 1; //Mode of either manual or autonomous
int setposition = 0;
int rotatingPosition = 1;
bool clockwise= true;
bool foundObject= false;
int endposition;

void setup() {
myservo1.attach(9); // attaches the servo1 on pin 9 to the servo object
myservo2.attach(10); // attaches the servo2 on pin 10 to the servo object
myservo3.attach(11); // attaches the servo3 on pin 11 to the servo object
myservo4.attach(3); // attaches the servo4 on pin 4 to the servo object

pinMode(2,INPUT); // attach button reader
pinMode(8, OUTPUT); // attach mode indicator LED

pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600);
}

void loop() {

checkForButton(); // function that checks if the button is pressed and if it is, changes modes

if (mode == 1) // if in manual mode
{

timeslooped=timeslooped + 1;
val1 = val1+analogRead(potpin1); // reads the value of the potentiometer (value between 0 and 1023) and makes the value, the sum of seven
val2 = val2+analogRead(potpin2); // this is to make it jitter less because it is an average
val3 = val3+analogRead(potpin3);
val4= val4+analogRead(potpin4);

if (timeslooped>=7) //if seven values have been added
{
val1avg= val1/7; // take an average
val2avg=val2/7;
val3avg=val3/7;
val4avg=val4/7;

val1avg = map(val1avg, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180), using the average value
val2avg= map(val2avg,0,1023,60,180);
val3avg= map(val3avg,2,1023,0,90);
val4avg= map(val4avg,0,1023,60,180);

val1RevertTo= val1avg;
val2RevertTo= val2avg;
val3RevertTo= val3avg;
val4RevertTo= val4avg;

myservo1.write(val1avg); // sets the servo position according to the scaled value
myservo2.write(val2avg);
myservo3.write(val3avg);
myservo4.write(val4avg);
//
timeslooped=0; // reset loop
val1=0;
val2=0;
val3=0;
val4=0;
}
}
else if( mode == 2)
{

if (setposition<200) { // this variable ensures that all arms are in correct possition before starting to search
goGraduallyTowards(&val1avg,1); // function that gradually increments a value to another value
goGraduallyTowards(&val2avg,108);
goGraduallyTowards(&val3avg, 90);
goGraduallyTowards(&val4avg,60);
myservo1.write(val1avg); // sets the servo position according to the scaled value
myservo2.write(val2avg);
myservo3.write(val3avg);
myservo4.write(val4avg);
Serial.println(“STILL SETTING”);
}
else if (setposition >= 200 and foundObject==false) // if position is in start, and object is not yet found
{

goAround(); // function that sweeps arm across and searches for object in proximity

}

else if (setposition >= 200 and foundObject==true)
{

while (distance >4 and distance < 20) // if object is found, make the arm go forward(and up to stay at level) until 3 cm away from object
{
Ultrasonic();
val4avg=val4avg+4;
val2avg=val2avg+3;
// val4avg=val4avg+5;
delay(200);
myservo2.write(val2avg);
myservo4.write(val4avg);
Serial.println(distance);
}
while ((distance <=4) or (distance > 20)) // once within 3 (or the reading reads incorrect value because it is too close) close arms
{
if (val3avg>0){
val3avg–;
delay(30);
myservo3.write(val3avg);
}
else if (val3avg<70) //and (abs(val4avg-130)>5) and (abs(val2avg-90)>5)) // once arms are closed, move object back and up
{

goGraduallyTowards(&val4avg, 130);
goGraduallyTowards(&val2avg, 90);
myservo4.write(val4avg); //
myservo2.write(val2avg); //

if (digitalRead(2)== true) // IF SWITCH IS PRESSED
{
setposition = 0; // go back to mode one and reset variables
rotatingPosition = 1;
clockwise= true;
foundObject= false;
mode=1;

}
}

}

}

}

}

int goGraduallyTowards(int *x, int y)
{
Serial.println(“GRADUAL”);
if ( (abs(*x-y))<3)
{
setposition=setposition+1;
endposition= 1;
//
}
else if ( *x < y)
{
*x=*x+1;
Serial.println(“value x up”);
Serial.println(*x);
//delay(500);
}
else if ( *x > y)
{
*x=*x-1;
Serial.println(*x);
Serial.println(“value x down”);
// delay(500);
}

//return x;

}
//PROBLEM— DOESNT RESET NUMBER OF INT X
//MODEL LIKE OTHER ONE

int goAround()
{

while (( rotatingPosition <180 ) and (clockwise == true) )
{
Ultrasonic();
rotatingPosition=rotatingPosition+3;
delay(200);
Serial.println( distance);
myservo1.write(rotatingPosition);

if (distance < 20)
{
foundObject=true;
return(distance);

}

}
while (( rotatingPosition>=180) and (clockwise ==true))
{
clockwise = false;
rotatingPosition=178;
myservo1.write(rotatingPosition);

return(rotatingPosition);

}
while (( rotatingPosition>1) and (clockwise==false))
{
rotatingPosition= rotatingPosition-3;
delay(30);
Serial.println( rotatingPosition);

myservo1.write(rotatingPosition);

}

while (( rotatingPosition<=1)and (clockwise==false))
{
rotatingPosition=1;
clockwise=true;
myservo1.write(rotatingPosition);
return(rotatingPosition);

}

}
int Ultrasonic()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(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;
Serial.println(distance);
}

int checkForButton()
{

if (digitalRead(2)== true) // IF SWITCH IS PRESSED
{
delay(500);

if (mode==1)
{
mode=2;
val1RevertTo= val1avg;
val2RevertTo= val2avg;
val3RevertTo= val3avg;
val4RevertTo= val4avg;
Serial.println(“MAKING “);
}
else if (mode==2) /// if the mode is one, make it two. If two, make one
{

mode=1;
goGraduallyTowards(&val1avg,val1RevertTo);
goGraduallyTowards(&val2avg,val2RevertTo);
goGraduallyTowards(&val3avg, val3RevertTo);
goGraduallyTowards(&val4avg,val4RevertTo);
myservo1.write(val1avg); // sets the servo position according to the scaled value
myservo2.write(val2avg);
myservo3.write(val3avg);
myservo4.write(val4avg);
Serial.println(“MOVING BACK”);

}
}
if (digitalRead(2)== true) // IF SWITCH IS PRESSED
{
delay(500);
// Serial.println(“READ”); //print read
if (mode==1)
{
mode=2;
val1RevertTo= val1avg;
val2RevertTo= val2avg;
val3RevertTo= val3avg;
val4RevertTo= val4avg;
}
else if (mode==2 and (abs(val1avg-val1RevertTo)<5) ) /// if the mode is one, make it two. If two, make one
{
mode=1;
goGraduallyTowards(&val1avg,val1RevertTo);
goGraduallyTowards(&val2avg,val2RevertTo);
goGraduallyTowards(&val3avg, val3RevertTo);
goGraduallyTowards(&val4avg,val4RevertTo);
myservo1.write(val1avg); // sets the servo position according to the scaled value
myservo2.write(val2avg);
myservo3.write(val3avg);
myservo4.write(val4avg);

}
}

if (mode==2)
{
digitalWrite(8,true); // IF mode Two, turn on light
}
else if (mode==1)
{
digitalWrite(8,false);
setposition=0;
}// waits for the servo to get there
}

Starter Project: Useless Machine

My name is Eytan, and I am a Rising Senior at SAR High School. I just completed my starter project, the useless machine.

In making this project, I soldered many components, including two resistors, two switches, and a three prong LED. The first Switch, a toggle switch, is connected to power and ground, and based on the position of the switch, lets current flow in one of two directions.

The implications of this are twofold. First it determines the state of the LED. The LED I used is a three pronged LED with a middle ground. LED’s are non-polar, diodes, and therefore current can only flow in one direction. The LED contains two bulbs, and when current runs through the right prong, the green LED is lit, and when current runs through the left prong, the red LED is lit. As I stated before, the first switch determines the direction of the flow of the current. When the switch is in the on position, the current runs through the green LED, and it lights up. However, when the switch is switched to the opposite direction, the current is diverted and goes through the red LED.

The second effect of the switch is the direction of the motor. The motor is polar and based on the direction of the current running through it will run in the opposite direction. When the switch is on, the motor pushes the arm up. When the switch is then pushed off, the direction of current switches and the motor is reversed, bringing the arm down.

The last crucial part of the circuit is the snap switch. The snap switch has a lever that when pressed, opens the circuit, stopping the flow of current. This allows the arm to stop being lowered, and for the LED to not have current flow through it when the arm is in the down position.

Video Presentation of Starter Project

Useless Machine at rest

Useless Machine activated

Circuit 

Diagram

Start typing and press Enter to search