-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPROJECT.c
120 lines (102 loc) · 2.74 KB
/
PROJECT.c
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
/*
Black Line Follower with Obstace Detection and Avoidance with Wireless Speed Control
*/
#include "cc2500.h"
#include "lcd.h"
#include "motor.h"
#include "timer_init.h"
// ADD RESPECTIVE MACRO DEFINITIONS HERE
#define IRL1 (!(PINA&(1<<1))) // Check Digital Line Sensor 1 for PRESENCE of line
#define IRL3 (!(PINA&(1<<3))) // Check Digital Line Sensor 3 for PRESENCE of line
#define IRL5 (!(PINA&(1<<5))) // Check Digital Line Sensor 5 for PRESENCE of line
#define TSOP (!(PINA&(1<<6))) // Check TSOP sensor for PRESENCE of obstacle
#define DIRL1 DDRA&=~(1<<1) // SET INPUT
#define DIRL3 DDRA&=~(1<<3) // SET INPUT
#define DIRL5 DDRA&=~(1<<5) // SET INPUT
#define DTSOP DDRA&=~(1<<6) // SET INPUT
void lineFollow(); // Line follower logic
void obs(); // Obstacle detector/avoider logic
void rpmControl(); // Wireless rpm control logic
void rpmDisplay(); // rpm display on LCD logic
void main(){
init_motor();
init_uart();
init_cc2500();
lcd_init();
init_timer0_normal();
/*
WRITE PORT INITIALIZATION CODE HERE
*/
DIRL1;
DIRL3;
DIRL5;
DTSOP;
lcd_num(75);
lineFollow();
}
void lineFollow(){
unsigned int flag=1; // To decide whether to turn right or left when all 3 sensors go off line
unsigned char temp;
unsigned int rpm=75;
while(1){
temp=uart_rx();
uart_tx(temp);
if((!IRL1 && IRL3 && !IRL5) || (IRL1 && IRL3 && IRL5)){ // Go straight if only 1 line found or if a junction is encountered
forward();
}
if((!IRL1 && IRL3 && IRL5) || (!IRL1 && !IRL3 && IRL5)){ // Turn right if line curves or turns sharply to right until LED3 detects line again
while(!IRL3){
r360();}
}
if((IRL1 && IRL3 && !IRL5) || (IRL1 && !IRL3 && !IRL5)){ //Turn left if line curves or turns sharply to left until LED3 detects line again
while(!IRL3){
l360();}
}
if(IRL1 && !IRL3 && IRL5){ //Turn right until LED3 detects line again, if LED1 and LED5 detect line but LED3 doesn't
r360();
}
if(!IRL1 && !IRL3 && !IRL5){ // Turn left until line detected if all 3 sensors go off the line and flag=1
l360();
}
if(TSOP){ // Call obs() function if obstacle detected
obs();
}
if(temp=='w'){
if(rpm<=15 && rpm>=3){
rpm*=5;
}
gotoxy1(0);
lcd_num(rpm);
}
if(temp=='s'){
if(rpm>=15 && rpm<=75){
rpm/=5;
}
gotoxy1(0);
lcd_num(rpm);
}
if(rpm==15){
delay_200ms();
stop();
delay_200ms();
}
if(rpm==3){
delay_1s();
stop();
delay_1s();
}
}
}
void obs(){
while(1){
if(TSOP){ // Turn to the right and follow the obstacle if detected
right();
}
if(!TSOP){
left();
}
if(IRL1 || IRL3 || IRL5){ // Call lineFollow() if line detected again
lineFollow();
}
}
}