Skip to content

Commit 9fa10c2

Browse files
authored
Add files via upload
This file uses adafruit motor shield library (<AFMotor.h>) for better synchronisation of the servo motor and the dc motor.
1 parent 6c8f946 commit 9fa10c2

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

finaltarget.ino

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#include <AFMotor.h>
2+
#include <Servo.h>
3+
# define stepAngle 1.8
4+
# define pi 3.14
5+
Servo servo;
6+
int x, y , z;
7+
float theta,phi,currentAngle,n;
8+
String serialData;
9+
10+
int parseDataX(String data);
11+
int parseDataY(String data);
12+
13+
AF_DCMotor motor(3);//using M1and M2
14+
15+
void setup( ) {
16+
Serial.begin(9600);
17+
//Serial.setTimeout(10);
18+
Serial.println("Motor test!");
19+
motor.setSpeed(255);
20+
servo.attach(10);
21+
servo.write(90);
22+
motor.run(FORWARD);
23+
delay(50*100/36);
24+
motor.run(RELEASE);
25+
26+
}
27+
28+
void loop (){
29+
if(Serial.available()){
30+
31+
//x = Serial.parseInt();
32+
//y = Serial.parseInt();
33+
//z = Serial.parseInt();
34+
serialData=Serial.readString();
35+
if(serialData)
36+
{
37+
theta = parseDataX(serialData);
38+
phi=parseDataY(serialData);
39+
}
40+
if(theta>0){
41+
motor_motion(theta);
42+
}
43+
if(phi>0)
44+
{
45+
servo_motion(phi,servo.read());
46+
}
47+
}
48+
}
49+
50+
51+
int parseDataX(String data)
52+
{
53+
data.remove(data.indexOf("Y"));
54+
data.remove(data.indexOf("X"),1);
55+
unsigned int xdata=data.toInt();
56+
//xdata=(xdata*180)/600;
57+
Serial.print("Dc motor angle: ");
58+
Serial.println(xdata);
59+
return xdata;
60+
}
61+
int parseDataY(String data)
62+
{
63+
data.remove(0,data.indexOf("Y")+1);
64+
unsigned int ydata=data.toInt();
65+
66+
//ydata=(ydata*180)/480;
67+
Serial.print("Servo motor angle: ");
68+
Serial.println(ydata);
69+
return ydata;
70+
}
71+
72+
73+
74+
void motor_motion(int theta){
75+
//float r, phi;
76+
int i, j ,k , l, pos_servo = 0,current_pos = 0,no_step;
77+
78+
//theta = (atan (x/z)*180)/ pi;
79+
//r = z /cos((theta*pi/180));
80+
//phi = (atan(y/r)*180)/pi;
81+
/*Serial.readBytes(input,sizeof(angle));
82+
angle=atoi(input);
83+
memset(input,0,sizeof(input));*/
84+
if(theta>0)
85+
{
86+
if( currentAngle != theta ){
87+
88+
if( currentAngle < theta){
89+
n = theta - currentAngle;
90+
motor.run(FORWARD);
91+
delay(n*100/36);
92+
motor.run(RELEASE);
93+
Serial.print("DC MOTOR: Forward angle =");
94+
//Serial.println(theta);
95+
Serial.println(n);
96+
Serial.println(" ");
97+
}
98+
else if( currentAngle > theta){
99+
n = currentAngle - theta;
100+
if(theta == 0){
101+
n =currentAngle;
102+
}
103+
motor.run(BACKWARD);
104+
delay(n*100/36);
105+
motor.run(RELEASE);
106+
Serial.print("DC MOTOR: Backward angle =");
107+
//Serial.println(theta);
108+
Serial.println(n);
109+
Serial.println(" ");
110+
}
111+
currentAngle=theta;
112+
}
113+
}
114+
}
115+
116+
void servo_motion(int phi, int pos_servo){
117+
118+
phi=180-phi;
119+
if(phi<=pos_servo){
120+
121+
n=pos_servo-phi;
122+
123+
for(int i= pos_servo;i>phi;i--){
124+
servo.write(i);
125+
delay(10);
126+
}
127+
Serial.print("SERVO: Backward angle =");
128+
//Serial.println(phi);
129+
Serial.println(n);
130+
Serial.println(" ");
131+
}
132+
else
133+
{
134+
n=phi-pos_servo;
135+
for(int i=pos_servo; i<=phi; i++){
136+
servo.write(i);
137+
delay(10);
138+
}
139+
Serial.print("SERVO: Forward angle =");
140+
//Serial.println(phi);
141+
Serial.println(n);
142+
Serial.println(" ");
143+
}
144+
}

0 commit comments

Comments
 (0)