Skip to content

Commit 0c29897

Browse files
committedAug 18, 2013
Create LineFollowing1.ino
This was an early version of the program.
1 parent 3b26b99 commit 0c29897

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed
 

‎LineFollowing1.ino

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#include <Servo.h>
2+
#include <MeetAndroid.h>
3+
4+
Servo servoLeft;
5+
Servo servoRight;
6+
MeetAndroid meetAndroid;
7+
int onboardLed = 13;
8+
int i = 0;
9+
unsigned long time;
10+
unsigned long tinit = 0;
11+
12+
int sensorPin = 3;
13+
int sensorPin1 = 6;
14+
int sensorPin2 = 7;
15+
int qti[3];
16+
17+
int n = 0;
18+
int threshold = 20;
19+
20+
long result = 0;
21+
void setup()
22+
{
23+
Serial.begin(9600);
24+
Serial.println("start");
25+
meetAndroid.registerFunction(testEvent, 'A');
26+
meetAndroid.registerFunction(forward, 'b' );
27+
tone(4, 3000, 1000);
28+
pinMode(onboardLed, OUTPUT);
29+
digitalWrite(onboardLed, HIGH);
30+
servoLeft.attach(11);
31+
servoRight.attach(10);
32+
tinit = micros();
33+
}
34+
35+
//float threshold = RCtime(sensorPin)-((RCtime(sensorPin)+RCtime(sensorPin2))/2);
36+
//read & navigate function
37+
void loop()
38+
{
39+
meetAndroid.receive();
40+
//Serial.println(threshold);
41+
time = micros() - tinit;
42+
if (RCtime(sensorPin)>= threshold){
43+
qti[0] = 1;}
44+
else{
45+
qti[0]=0;}
46+
if (RCtime(sensorPin1)>= threshold){
47+
qti[1] = 1;}
48+
else{
49+
qti[1]=0;}
50+
if (RCtime(sensorPin2)>= threshold){
51+
qti[2] = 1;}
52+
else{
53+
qti[2]=0;}
54+
55+
if(qti[0]==0 && qti[1]==1 && qti[2]==0){
56+
servoLeft.writeMicroseconds(1700);
57+
servoRight.writeMicroseconds(1300);
58+
}
59+
60+
else if(qti[0]==1 && qti[1]==1 && qti[2]==0){
61+
servoLeft.writeMicroseconds(1700);
62+
servoRight.writeMicroseconds(1500);
63+
}
64+
65+
else if(qti[0]==1 && qti[1]==0 && qti[2]==0){
66+
servoLeft.writeMicroseconds(1700);
67+
servoRight.writeMicroseconds(1700);
68+
}
69+
70+
else if(qti[0]==0 && qti[1]==1 && qti[2]==1){
71+
servoLeft.writeMicroseconds(1500);
72+
servoRight.writeMicroseconds(1300);
73+
}
74+
75+
else if(qti[0]==0 && qti[1]==0 && qti[2]==1){
76+
servoLeft.writeMicroseconds(1300);
77+
servoRight.writeMicroseconds(1300);
78+
}
79+
80+
else if(qti[0]==1 && qti[1]==1 && qti[2]==1){
81+
servoLeft.writeMicroseconds(1500);
82+
servoRight.writeMicroseconds(1500);
83+
i++;
84+
if(i>=5){
85+
meetAndroid.send("The Robot has Stopped:");
86+
meetAndroid.send(i);
87+
meetAndroid.send(time);
88+
}
89+
}
90+
delayMicroseconds(40);
91+
92+
//servoLeft.writeMicroseconds(1700);
93+
//servoRight.writeMicroseconds(1300);
94+
95+
//Serial.println(qti[0]);
96+
//Serial.println(qti[1]);
97+
//Serial.println(qti[2]);
98+
99+
}
100+
101+
102+
103+
long RCtime(int sensPin){
104+
long result = 0;
105+
pinMode(sensPin, OUTPUT);
106+
digitalWrite(sensPin, HIGH);
107+
delay(1);
108+
109+
pinMode(sensPin, INPUT);
110+
digitalWrite(sensPin, LOW);
111+
while(digitalRead(sensPin)){
112+
result++;
113+
}
114+
115+
return result;
116+
}
117+
118+
void testEvent(byte flag, byte numOfValues)
119+
{
120+
flushLed(300);
121+
flushLed(300);
122+
}
123+
124+
void flushLed(int time)
125+
{
126+
digitalWrite(onboardLed, LOW);
127+
delay(time);
128+
digitalWrite(onboardLed, HIGH);
129+
delay(time);
130+
}
131+
132+
void forward(byte flag, byte numOfValues)
133+
{
134+
servoLeft.writeMicroseconds(1700);
135+
servoRight.writeMicroseconds(1300);
136+
delay(100);
137+
tinit = micros();
138+
meetAndroid.send("The new initial time is:");
139+
meetAndroid.send(tinit);
140+
i = 0;
141+
}

0 commit comments

Comments
 (0)
Please sign in to comment.