-
Notifications
You must be signed in to change notification settings - Fork 0
/
Line_follower.ino
80 lines (66 loc) · 1.77 KB
/
Line_follower.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
//timer to throttle down serial out messages
unsigned long serial_timer = 0;
//infrared sensor teensy pins
const int irL = 19;
const int irC = 20;
const int irR = 21;
// IR Sensor values
int irValL; // Left
int irValC; // Centre
int irValR; // Right
//motor controllers
const int motorL = 23;
const int motorR = 22;
void setup()
{
Serial.begin(9600);
//set infrared inputs with internal pullup resistors
pinMode(irL,INPUT_PULLUP);
pinMode(irC,INPUT_PULLUP);
pinMode(irR,INPUT_PULLUP);
serial_timer = millis(); //set initial timer value
}
int roundVal(int val){
if(val < 500){
val = 0;
}else{
val = 1;
}
return val;
}
void getIR(){
// Read sensors
// Input ranges from 1023 (sensor is over white) and 0 (sensor is over black)
// Typical range is 800 to 40 (tested on matt B&W photocopier printout on bond paper)
// TO DO: calibrate sensors?
irValL = roundVal(analogRead(irL));
irValC = roundVal(analogRead(irC));
irValR = roundVal(analogRead(irR));
}
void setMotors(int speedL,int speedR){
// update motor pin voltages
analogWrite(motorL,speedL);
analogWrite(motorR,speedR);
}
void loop(){
//update IR sensor readings
getIR();
//determine line position
if (irValL == 1 and irValC == 0 and irValR == 0 ){
setMotors(0,1000);
}else if (irValL == 1 and irValC == 1 and irValR == 0 ){
setMotors(300,1000);
}else if (irValL == 1 and irValC == 1 and irValR == 1 ){
setMotors(1000,1000);
}else if (irValL == 1 and irValC == 0 and irValR == 1 ){
setMotors(1000,1000);
}else if (irValL == 0 and irValC == 1 and irValR == 1 ){
setMotors(1000,300);
}else if (irValL == 0 and irValC == 0 and irValR == 1 ){
setMotors(1000,0);
}
//output to serial every second
if(millis() - serial_timer > 1000){
serial_timer = millis();
}
}