-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplete_code_2_switches
119 lines (97 loc) · 4.4 KB
/
complete_code_2_switches
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <Servo.h>
#define dirPin 8
#define stepPin 9
int servoPin = 10;
Servo Servo1;
int switch1 = 3; //switch at the first apple
int switch2 = 4; //switch at the delivery
int pb1 = 5; //push button for apple 1
int pb2 = 6; //push button for apple 2
int pb3 = 7; //push button for apple 3
#define delay1 5
int angle_down = 180; //down angle for servo
int angle_up = 80; //top angle for servo
int n1 = 415; //steps to reach second position
int n2 = 842; // to reach third position
void setup() {
// Declare pins as output:
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
Servo1.attach(servoPin);
pinMode(switch1, INPUT);
pinMode(switch2, INPUT);
pinMode(pb1, INPUT);
pinMode(pb2, INPUT);
pinMode(pb3, INPUT);
Servo1.write(180);
}
void loop() {
delay(1000);
if( (digitalRead(pb1) == HIGH)) //if first button is pushed
{
move_servoup(angle_up); //put the arm vertically
move_stepper2(switch2, LOW); //move toward the delivery place
deliver(); //put the arm down
reset(0); //Go back to the initial position
}
else if( (digitalRead(pb2) == HIGH) ) //if second button is pushed
{
move_stepper1(n1, LOW); //Go to the second apple place
move_servoup(angle_up); //put the arm vertically
move_stepper2(switch2,LOW); //move toward the delivery place
deliver(); //put the arm down
reset(n1); //Go back to the initial position
}
else if( (digitalRead(pb3) == HIGH)) //if third button is pushed
{
move_stepper1(n2, LOW); //Go to the third apple place
move_servoup(angle_up); //put the arm vertically
move_stepper2(switch2,LOW); //move toward the delivery place
deliver(); //put the arm down
reset(n2); //Go back to the initial position
}
else{delay(1000);}
delay(100);
}
void move_stepper1(int n, bool dir) { //For moving the stepper motor of n steps in dir direction
digitalWrite(dirPin, dir); //set the direction accordingly to the variable dir
for(int i =0; i<n ; i++){ //Do n steps
digitalWrite(stepPin, HIGH); //The 4 lines results in 1 step
delay(delay1);
digitalWrite(stepPin, LOW);
delay(delay1);
}
}
void move_stepper2(int Lswitch, bool dir) { //For moving the stepper motor untill a switch is activated
digitalWrite(dirPin, dir); //set the direction accordingly to the variable dir
while(digitalRead(Lswitch) == LOW){ //Make 1 extra step as long as the selected switch isn't pushed.
digitalWrite(stepPin, HIGH); //The 4 lines results in 1 step
delay(delay1);
digitalWrite(stepPin, LOW);
delay(delay1);
}
}
void move_servoup(int angle) { //To move the servo motor vertically
for ( angle = 180; angle >= 80; angle -= 1) { //Do 1° each time to be able to introduce a delay to control the speed of the process
Servo1.write(angle); //Put the servo to the desired angle
delay(50); //wait a bit
angle = angle-1; //indicate to the for loop where we are in the motion
}}
void move_servodown(int angle) { //To move the servo motor vertically
for ( angle = 80; angle <= 180; angle += 1) { //Do 1° each time to be able to introduce a delay to control the speed of the process
Servo1.write(angle); //Put the servo to the desired angle
delay(50); //wait a bit
angle = angle+1; //indicate to the for loop where we are in the motion
}
delay(1000);
}
void deliver() { //put the arm down
move_servodown(angle_down);
}
void reset(int n) { //Go from the delivery place to the initial position
move_servoup(angle_up); //put the arm vertically
move_stepper2(3,HIGH); //Go to the opposite side as the first switch doesn't work
move_stepper1(n,LOW); //Go to the place where the apple has been taken
move_servodown(angle_down); //Put the arm down
move_stepper2(3,HIGH); //Go back to the initial position, where the broken switch should be
}