[go: up one dir, main page]

0% found this document useful (0 votes)
124 views10 pages

Egr 103 - Code For Project

This code controls a mechanism for separating recyclable materials. It connects Arduino components like servos and sensors. The code tests sensors, then runs a loop that uses readings from two photoresistors to identify disks as clear, paper, cloth, steel, or aluminum by comparing opacity and reflectivity values. Servos then guide identified disks to appropriate bins, and counters track the quantities sorted.

Uploaded by

api-392769552
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views10 pages

Egr 103 - Code For Project

This code controls a mechanism for separating recyclable materials. It connects Arduino components like servos and sensors. The code tests sensors, then runs a loop that uses readings from two photoresistors to identify disks as clear, paper, cloth, steel, or aluminum by comparing opacity and reflectivity values. Servos then guide identified disks to appropriate bins, and counters track the quantities sorted.

Uploaded by

api-392769552
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

%{

Authors: Morgan Walli


Team: The Tater Puffs
Assignment: EGR 103-003 Code for Project
Changed: 23 April 2017
History:

Begun - 02 Feb 2017

Purpose: control mechanism for separating types of


recyclable materials

Notes: parts of this code may still be changed before the


end of the
semester
%}
%connect to arduino if needed
if ~( exist('a', 'var') && isa(a, 'arduino')) %this part of
the code is courtesy of Mr. Penn and Dr. Moore
fprintf('Arduino now connecting...'); %the similar code
below has been modified but is based on this code
a = arduino('com4', 'uno');
disp(' Connected.');
end

%create a Servo object, assign to pin D2 --> this is the


lower servo
%the pulse speeds were taken from the servo's specs
%these servos controls the pulley that guides the disks
into the bin
if ~( exist('s1', 'var') && isa(s1, 'servo')) %this part of
the code is courtesy of Mr. Penn and Dr. Moore
fprintf('Servo 1 now connecting...');
s1 = servo(a, 'D2') % 'MinPulseDuration', 500*10^-
6,'MaxPulseDuration', 2500*10^-6);
disp(' Connected.');
end
pause on %enable pause function

% S2 is the upper servo


if ~(exist('s2', 'var') && isa(s2, 'servo')) %this part of
the code is courtesy of Mr. Penn and Dr. Moore
fprintf('Servo 2 now connecting...');
s2 = servo(a, 'D11')%'MinPulseDuration', 500*10^-
6,'MaxPulseDuration', 2500*10^-6);
disp(' Connected.');
end

%make servos not move


writePosition(s1, 0.5) % 0.5 is the value needed for this
servo to not move - not 0.5
writePosition(s2, 0.5) % 0.5 is the value needed for this
servo to not move - not 0.5

%create a Servo object, assign to pin D6


%the pulse speeds were taken from the servo's specs
%this servo control the input mechanism
if ~(exist('s3', 'var') && isa(s3, 'servo')) %this part of
the code is courtesy of Mr. Penn and Dr. Moore
fprintf('Servo 3 now connecting...');
s3 = servo(a, 'D6','MinPulseDuration', 700*10^-6,
'MaxPulseDuration', 2300*10^-6);
disp(' Connected.');
end

%create a Servo object, assign to pin D7


%the pulse speeds were taken from the servo's specs
%this servo controls the door that allows disks out of the
sensor box

if ~( exist('s4', 'var') && isa(s4, 'Servo')) %this part of


the code is courtesy of Mr. Penn and Dr. Moore
fprintf('Servo 4 now connecting...');
s4 = servo(a, 'D7','MinPulseDuration', 700*10^-6,
'MaxPulseDuration', 2300*10^-6);
disp(' Connected.');
end

writePosition (s4, 0.8) %initialize servo

% check sensors for functionality of setup

%define variables and initialize

writePosition (s4, 0.4) %initialize servo to starting


position
baselineOpacity = readVoltage(a, 'A0'); % initialize first
photoresistor; this one senses opacity
baselineReflectivity = readVoltage(a, 'A1'); %initialize
second photoresistor; this one senses reflectivity

%initialize count at 0 for all disks


clear = 0;
paper = 0;
cloth = 0;
steel = 0;
aluminum = 0;
red = 0;
sandpaper = 0;

%obtain instruction from operator


%promptOne = 'Type number of chips (use numbers not
letters): ';
%n = input (promptOne, 's'); %numchips

promptTwo = 'Type 1 to run program. Type 2 to end. Type 3


for a summary of results. Type 0 to test.';
instructionTwo = input(promptTwo, 's');

%execute appropriate command


while instructionTwo == ('0')

getBaselineOpacity = readVoltage(a, 'A0'); % test first


photoresistor; this one senses opacity
getBaselineReflectivity = readVoltage(a, 'A1'); %test
second photoresistor; this one senses reflectivity

if getBaselineOpacity == 0 %test reading


fprintf('ERROR 1!!!\n')

elseif getBaselineReflectivity == 0 %test reading


fprintf('ERROR 2!!!\n')

%print results
else
fprintf('All SYSTEMS GO!!! \n Inital test opacity: %d
\n Initial test reflectivity: %d \n', getBaselineOpacity,
getBaselineReflectivity);
end
%obtain intruction
promptTwo = 'Type 1 to run program. Type 2 to end. Type
3 for a summary of results. Type 0 to test.';
instructionTwo = input(promptTwo, 's');
end

%execute instruction

while (instructionTwo == ('1'))

for (i = 0:7)

%run program

%start input mechanism


writePosition(s3, 0); %move servo disk out of PVC loading
area; prep-stage

pause(1) %pause

writePosition(s3, 0.65); %return servo to previous position


and push disk out

pause (2) %delay to allow disk to settle over

testOpacity = readVoltage(a, 'A0'); % obtain reading from


first photoresistor; this one senses opacity
testReflectivity = readVoltage(a, 'A1'); %obtain reading
from second photoresistor; this one senses reflectivity

%determine value at checkPoint and send disk to appropriate


location
if (testOpacity <= baselineOpacity + 0.5 && testOpacity >=
baselineOpacity - 0.3 && testReflectivity > 1 ) % determine
if chip is clear
fprintf('testOpacity: %d \n testReflectivity: %d \n
Identity: clear \n', testOpacity, testReflectivity) %print
results

%move pulley

writePosition(s1, 0.6) %move servo 1


writePosition(s2, 0.0) %move servo 2

pause(2.5) %pause to move belt as needed

writePosition(s1, 0.5) %for this servo 0.5 is off - not


0.5
writePosition(s2, 0.5) %for this servo 0.5 is off - not
0.5

pause(2)

writePosition(s4, 0.05) %open door to allow disk


through

pause (2) %pause

writePosition(s4, 0.4) %reset servo 4

pause (2)

writePosition(s1, 0.0) %move servo 1


writePosition(s2, 0.6) %move servo 2

pause(2.5) %pause to move belt as needed

writePosition(s1, 0.5) %for this servo 0.5 is off - not


0.5
writePosition(s2, 0.5) %for this servo 0.5 is off - not
0.5

clear = clear +1; %add one to the "clear" count

elseif (testOpacity<= baselineOpacity - 0.2 && testOpacity


>= baselineOpacity - 0.6) % determine if chip is cloth

fprintf('testOpacity: %d \n testReflectivity: %d \n
Identity: cloth \n', testOpacity, testReflectivity) %print
results

%move pulley

writePosition(s1, 0.6) %move servo 1


writePosition(s2, 0.0) %move servo 2
pause(1.5) %pause to move belt as needed

writePosition(s1, 0.5) %for this servo 0.5 is off - not


0.5
writePosition(s2, 0.5) %for this servo 0.5 is off - not
0.5

writePosition(s4, 0.05) %open door to allow chip through

pause (2)

writePosition(s4, 0.4) %reset system

writePosition(s1, 0.0) %move servo 1


writePosition(s2, 0.6) %move servo 2

pause(1.5) %pause to move belt as needed

writePosition(s1, 0.5) %for this servo 0.5 is off - not


0.5
writePosition(s2, 0.5) %for this servo 0.5 is off - not
0.5

cloth = cloth + 1; %add one to the count

elseif (testOpacity < 1.5) % deterine if there is a low


opacity

if (testReflectivity >= baselineReflectivity - 2.4 &&


testReflectivity <= baselineReflectivity - 1.1) %determine
if chip is steel

fprintf('testOpacity: %d \n testReflectivity: %d \n
Identity: Steel \n', testOpacity, testReflectivity) %print
results

%move pulley
writePosition(s1, 1.0) %move servo 1
writePosition(s2, 0.0) %move servo 2

pause(1) %pause to move belt as needed


writePosition(s1, 0.5) %for this servo 0.5 is off - not
0.5
writePosition(s2, 0.5) %for this servo 0.5 is off - not
0.5

writePosition(s4, 0.05) %open door to allow chip


through

pause (2) %pause

writePosition(s4, 0.4) %reset system

writePosition(s1, 0.0) %move servo 1


writePosition(s2, 1.0) %move servo 2

pause(1) %pause to move belt as needed

writePosition(s1, 0.5) %for this servo 0.5 is off - not


0.5
writePosition(s2, 0.5) %for this servo 0.5 is off - not
0.5

steel = steel + 1; %add one to the "steel" count

else(testReflectivity >= baselineReflectivity - 1 &&


testReflectivity <= baselineReflectivity - 0.1); %determine
if chip is aluminum

fprintf('testOpacity: %d \n testReflectivity: %d \n
Identity: Aluminum \n', testOpacity, testReflectivity);
%print results

%move pulley

writePosition(s1, 0.6) %move servo 1


writePosition(s2, 0.0) %move servo 2

pause(0.5) %pause to move belt as needed

writePosition(s1, 0.5) %for this servo 0.5 is off - not


0.5
writePosition(s2, 0.5) %for this servo 0.5 is off - not
0.5

writePosition(s4, 0.05) %open door to allow chip


through

pause (2) %pause

writePosition(s4, 0.4) %reset system

writePosition(s1, 0.0) %move servo 1


writePosition(s2, 0.6) %move servo 2

pause(0.5) %pause to move belt as needed

writePosition(s1, 0.5) %for this servo 0.5 is off - not


0.5
writePosition(s2, 0.5) %for this servo 0.5 is off - not
0.5

aluminum = aluminum + 1; %add one to the "alumunim"


count

end

elseif (testOpacity <= baselineOpacity && testOpacity >=


0.5) %determine if chip is paper by process of elimination
with values

fprintf('testOpacity: %d \n testReflectivity: %d \n
Identity: paper \n', testOpacity, testReflectivity) %print
results

%move pulley
writePosition(s1, 0.6) %move servo 1
writePosition(s2, 0.0) %move servo 2

pause(2) %pause to move belt as needed

writePosition(s1, 0.5) %for this servo 0.5 is off - not


0.5
writePosition(s2, 0.5) %for this servo 0.5 is off - not
0.5
writePosition(s4, 0.05) %open door to allow chip
through

pause (2) %pause

writePosition(s4, 0.4) %reset system

writePosition(s1, 0.0) %move servo 1


writePosition(s2, 0.6) %move servo 2

pause(2) %pause to move belt as needed

writePosition(s1, 0.5) %for this servo 0.5 is off - not


0.5
writePosition(s2, 0.5) %for this servo 0.5 is off - not
0.5

paper = paper + 1; %add one to the "paper" count

else
fprintf('ERROR with sensor!!! \n') %print that there is
an error if the chip doesnt appear to be anything

fprintf('testOpacity: %d \n testReflectivity: %d \n',


testOpacity, testReflectivity) %print the readings for the
error
end

end

%obtain instruction
%promptOne = 'Type number of chips (use numbers not
letters): ';
%numChips = input (promptOne, 's');

promptTwo = 'Type 1 to run program. Type 2 to end. Type 3


for a summary of results. Type 0 to test.';
instructionTwo = input(promptTwo, 's');

end

totalCount = (clear + paper + cloth + steel + aluminum +


red + sandpaper); %create total count
%execute instruction
while instructionTwo ==('2')

%turn off feeding mechanism

writePosition(s1, 0.5); %turn off servo


fprintf('Program ended.\n'); %print command

fprintf('SUMMARY \n clear plastic: %d \n paper: %d \n


cloth: %d \n metals: %d \n', clear, paper, cloth, metals)
%print summary of results

%obtain instruction
promptTwo = 'Type 3 for a summary of results.';
instructionTwo = input(promptTwo, 's');

end

%execute instruction
while instructionTwo == ('3')

fprintf('SUMMARY \n Clear: %d \n Paper: %d \n Cloth: %d


\n Steel: %d \n Aluminum: %d \n Red: %d \n Sandpaper: %d \n
Total: %d \n', clear, paper, cloth, steel, aluminum, red,
sandpaper, totalCount) %print summary of results
results = [clear, paper, cloth, steel, aluminum, red,
sandpaper]; % create array for number of each material
bar(results) %graph the results on a bar graph

set(gca,'XTickLabel',{'Clear', 'Paper', 'Cloth',


'Steel', 'Aluminum',}); %set labels for bar graph

%obtain instruction from operator


promptTwo = 'Type 1 to run program. Type 2 to end. Type
3 for a summary of results. Type 0 to test.';
instructionTwo = input(promptTwo, 's');

end

%obtain instruction from operator


promptTwo = 'Type 1 to run program. Type 2 to end. Type 3
for a summary of results. Type 0 to test.';
instructionTwo = input(promptTwo, 's');

You might also like