-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstart.ino
211 lines (175 loc) · 5.58 KB
/
start.ino
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#define DIR_MOTOR_PEN 9
#define STEP_MOTOR_PEN 10
#define DIR_MOTOR_PLATE 11
#define STEP_MOTOR_PLATE 12
#define STEP_DURATION 1 //in ms
#define MOTOR_PLATE 2
#define MOTOR_PEN 1
#include <Servo.h>
Servo servo_test;
Servo myservo;
int angle = 0;
int down = 0;
int xPin = A1;
int yPin = A0;
int bPin = A5;
int xDir = 0;
int yDir = 0;
int bValue = 0;
int sValue = 0;
int xPosition = 0;
int yPosition = 0;
int xValue = 0;
int yValue = 0;
int pos = 0;
void joystick_position();
void setup() {
/*-------------------------------------------------------------
* Serial.begin sets the data rate in bits per second (baud)
* for serial data transmission.115200 is the fastest.
-------------------------------------------------------------*/
Serial.begin(115200);
//setting up the pins
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
pinMode(bPin, INPUT);
pinMode(DIR_MOTOR_PEN,OUTPUT);
pinMode(STEP_MOTOR_PEN,OUTPUT);
pinMode(DIR_MOTOR_PLATE,OUTPUT);
pinMode(STEP_MOTOR_PLATE,OUTPUT);
//attaching the servo
myservo.attach(6);
Serial.println("Program started!");
}
void loop() {
joystick_position();
}
void joystick_position(){
/* ---------------------------------------------------------------
* Depending on the joystick position the pen is being moved.
* Also, you can change the degree of the pen via a pottentiometer
* and the servo_move_up and servo_move_down functions
----------------------------------------------------------------*/
//x - left or right // y - forward or backward //b - diagonal
xValue= analogRead(xPin);
yValue = analogRead(yPin);
bValue = analogRead(bPin);
Serial.print(bValue);
//change the pottentiometer of the arduino and it changes the angle of the pen
sValue = map(bValue,0,1023,85,10);
myservo.write(sValue);
/*-------------------------------------------------------
* In the following if-else constructions,
* the constants are cordinates.
* The max is 1023 to both x and y values.
* The middle is with cordinates (512;512).
* If we use it, the joystick will be too precise
* and the error percentage will up.
* That's why we use 525 as middle.
* When yDir and xDir are 1,we move the pen diagonally
-------------------------------------------------------*/
if((xValue>490 && xValue <525 ) && (yValue>490 && yValue<525))
{
stepStop();
}
else if((xValue>490 && xValue <525 ))
{
if(yValue > 525)
{
//push the y value => pen moves to the right
yValue = map(yValue,525,1023,20,1);
yDir = 1;
}
else
{
//push the y value => pen to the left
yValue = map(yValue,490,0,20,1);
yDir = 0;
}
digitalWrite(STEP_MOTOR_PLATE, LOW);
stepMeHigh(STEP_MOTOR_PEN,yDir,yValue);
}
else if((yValue>490 && yValue <525 ))
{
if(xValue > 525)
{
//push the y value and it moves the plate forward => the pen moves backward
xValue = map(xValue,525,1023,20,1);
xDir = 1;
}
else
{
xValue = map(xValue,490,0,20,1);
xDir = 0;
}
digitalWrite(STEP_MOTOR_PEN, LOW);
stepMeHigh(STEP_MOTOR_PLATE,xDir,xValue);
}
else
{
/*------------------------------------------------
* By changing the y value( by moving the joystick),
* we move the position of the plate backward
* ===> the pen moves forward.
------------------------------------------------*/
if(xValue > 525) //525 - not pushed joystick
{
xValue = map(xValue,525,1023,20,1);
xDir = 1;
}
else
{
xValue = map(xValue,490,0,20,1);
xDir = 0;
}
if(yValue > 525) //525 - not pushed joystick
{
yValue = map(yValue,525,1023,20,1);
yDir = 1;
}
else
{
yValue = map(yValue,490,0,20,1);
yDir = 0;
}
stepMeHigh(STEP_MOTOR_PLATE,xDir,xValue);
stepMeHigh(STEP_MOTOR_PEN,yDir,yValue);
}
}
void servo_move_down(){
/*----------------------------------------
* Via the servo and the pottentiometer,
* we move the pen downward
----------------------------------------*/
for (pos = 90; pos >= 0; pos -= 1) //change the angle of the pen from 90 to 0
{
myservo.write(pos); //command to rotate the servo to the specified angle
}
}
void servo_move_up(){
/*----------------------------------------
* Via the servo and the pottentiometer,
* we move the pen upward
----------------------------------------*/
for(angle = 0; angle < 90; angle += 1) //change the angle of the pen from 0 to 90
{
servo_test.write(angle); //command to rotate the servo to the specified angle
}
}
void stepMeHigh(int motor, int dir, int del) {
/*-----------------------------------------------
* Function parametres:
* motor : 1 - motor pen, 2 - motor plate
* dir : 0 - left, 1 - right
-----------------------------------------------*/
digitalWrite(DIR_MOTOR_PLATE, dir);
digitalWrite(DIR_MOTOR_PEN, dir);
digitalWrite(motor, HIGH);
delay(del);
digitalWrite(motor, LOW);
}
void stepStop(){
//nulls the step
digitalWrite(STEP_MOTOR_PEN, LOW);
digitalWrite(STEP_MOTOR_PLATE, LOW);
}