forked from theonentn/The-Motion-Controlled-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotion_controlled_car.ino
96 lines (85 loc) · 2.67 KB
/
motion_controlled_car.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
#include<Wire.h>
#include<ADXL345.h>
# define Ln 7
# define Lp 4
# define El 5 //pwm pin for left motor
# define Rn 8
# define Rp 12
# define Er 6 //pwm pin for right motor
ADXL345 adxl; //adxl
int x,y,z;
int rawx,rawy,rawz;
int mappedRawX,mappedRawY,mappedRawZ;
void setup()
{
Serial.begin(9600);
pinMode(Lp, OUTPUT);
pinMode(Ln, OUTPUT);
pinMode(El, OUTPUT);
pinMode(Rp, OUTPUT);
pinMode(Rn, OUTPUT);
pinMode(Er, OUTPUT);
analogWrite(El, 255); //speed of left motor
analogWrite(Er, 255); //speed of right motor
adxl.powerOn(); // from library
}
void loop()
{
adxl.readAccel(&x,&y,&z);
rawx=x-7;
rawy=y-6;
rawz=z+10;
if(rawx<-255)rawx=-255;else if(rawx>255)rawx=255;
if(rawy<-255)rawy=-255;else if(rawy>255)rawy=255;
if(rawz<-255)rawz=-255;else if(rawz>255)rawz=255; // u can use the constrain keyword
mappedRawX=map(rawx,-255,255,0,180);
mappedRawY=map(rawy,-255,255,0,180);
mappedRawZ=map(rawz,-255,255,0,180);
Serial.print("mappedRawX =");Serial.println(mappedRawX);
Serial.print("mappedRawY =");Serial.println(mappedRawY);
//Serial.print("mappedRawZ =");Serial.println(mappedRawZ);
if(50 <= mappedRawX && mappedRawX <= 110 && 50 <= mappedRawY && mappedRawY <= 110) //range of adxl for which the car remains stationary
{
MotorControl(0,0);
Serial.println("STAY");
}
if( 0 <= mappedRawY && mappedRawY <= 50)
{
MotorControl(2,1);
Serial.println("LEFT");
}
if(110 < mappedRawY && mappedRawY <= 180)
{
MotorControl(1,2);
Serial.println("RIGHT");
}
if(0 < mappedRawX && mappedRawX < 50 )
{
MotorControl(1,1);
Serial.println("FORWARD");
}
if(110 < mappedRawX && mappedRawX<180)
{
MotorControl(2,2);
Serial.println("BACKWARD");
}
// delay(1900);
}
void MotorControl(int driveL, int driveR)
{
//Motorcontrol: Controlling the motors. In this case, 2 motors used
switch(driveL)
{ //left motor
case 0: digitalWrite(Ln, LOW); digitalWrite(Lp, LOW); break; //motor stops
case 1: digitalWrite(Ln, LOW); digitalWrite(Lp, HIGH); break; // motor in forward direction
case 2: digitalWrite(Ln, HIGH); digitalWrite(Lp, LOW); break; // motor in backward direction
default: break;
}
switch(driveR)
{ //right motor
case 0: digitalWrite(Rn, LOW); digitalWrite(Rp, LOW); break; //motor stops
case 1: digitalWrite(Rn, LOW); digitalWrite(Rp, HIGH); break; // motor in forward direction
case 2: digitalWrite(Rn, HIGH); digitalWrite(Rp, LOW); break; // motor in backward direction
default: break;
}
}