-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathArduino_Source_Code.ino
123 lines (115 loc) · 2.67 KB
/
Arduino_Source_Code.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
/*
* Pin definition
* Pin 30 and 31 for left motor
* Pin 32 and 33 for right motor
* Pin 10 for PWM to control the motor speed
* LED pin is 13 to indicate system status
*/
#define LED 13
#define Speed 10
#define leftForward 4
#define leftBackward 5
#define rightForward 6
#define rightBackward 7
#define RX 0
#define TX 1
/*
* Variable definition
*/
char data;//for bluetooth data
/*
* Setup code
*
*/
void setup() {
pinMode(LED, OUTPUT);
pinMode(Speed, OUTPUT);
pinMode(leftForward, OUTPUT);
pinMode(leftBackward, OUTPUT);
pinMode(rightForward, OUTPUT);
pinMode(rightBackward, OUTPUT);
analogWrite(Speed, 200); // speed of DC motor
Serial.begin(38400);
Serial.println("Bluetooth started on bot.");
}
/*
* Function to move bot in FORWARD motion
*/
void leftMotorForward(){
digitalWrite(leftForward, HIGH);
digitalWrite(leftBackward, LOW);
}
void rightMotorForward(){
digitalWrite(rightForward, HIGH);
digitalWrite(rightBackward, LOW);
}
/*
* Function to move bot in Backward motion
*/
void leftMotorBackward(){
digitalWrite(leftForward, LOW);
digitalWrite(leftBackward, HIGH);
}
void rightMotorBackward(){
digitalWrite(rightForward, LOW);
digitalWrite(rightBackward, HIGH);
}
/*
* to Stop the bot
*/
void stopBot(){
digitalWrite(rightForward, LOW);
digitalWrite(rightBackward, LOW);
digitalWrite(leftForward, LOW);
digitalWrite(leftBackward, LOW);
}
/*
* to move left
*/
void moveLeft(){
digitalWrite(leftForward, LOW);
digitalWrite(leftBackward, LOW);
digitalWrite(rightBackward,LOW);
digitalWrite(rightForward, HIGH);
}
/*
* to move right
*/
void moveRight(){
digitalWrite(leftBackward, LOW);
digitalWrite(rightBackward,LOW);
digitalWrite(rightForward, LOW);
digitalWrite(leftForward, HIGH);
}
void loop() {
if(Serial.available() > 0){
data = Serial.read(); // Reads the character data from the serial port
switch(data){
//to move left
case '1': moveLeft();
Serial.println("Left");
break;
//to move right
case '2': moveRight();
Serial.println("Right");
break;
//to move forward
case '3': leftMotorForward();
rightMotorForward();
Serial.println("Forward");
break;
//to move backward
case '4': leftMotorBackward();
rightMotorBackward();
Serial.println("Backward");
break;
//to stop bot movement
case '5': stopBot();
Serial.println("Stop");
break;
default: Serial.print("Incorrect data:- ");
Serial.println(data);
break;
}
}
}