Ok, so I have found some time to put up a couple videos in which I show my two main test programs. If you go to YouTube to watch the clips, there is a bit more information about how the program actually works.
The first program is my latest try, and after you watch the video I will give you the code. Here it is:
And here is the code:
#include [Servo.h]
Servo Tilt; Servo Turn;
int pos = 100, GKLpos = 0, GKL = 0, smthns = 10;
int thresh = 75, allwnc = 10, drkst;
void setup()
{
Tilt.attach(9); Turn.attach(8); pinMode(4, OUTPUT);
Tilt.write(180); Turn.write(pos); drkst = analogRead(0);
}
void loop()
{
int AR_0;
drkst = analogRead(0);
digitalWrite(4, HIGH);
// Right / Left
for(pos = 150; pos > 0 + smthns; pos = pos - smthns)
{
Turn.write(pos);
delay(150);
AR_0 = analogRead(0);
if(AR_0 > GKL)
{
GKLpos = pos;
GKL = AR_0;
}
}
Turn.write(GKLpos);
delay(50);
GKL = 0;
GKLpos = 180;
// Up / Down
for(pos = 180; pos > 10 + smthns; pos = pos - (smthns*2))
{
Tilt.write(pos);
delay(150);
AR_0 = analogRead(0);
if(AR_0 > GKL)
{
GKLpos = pos;
GKL = AR_0;
}
}
Tilt.write(GKLpos);
GKL = 0;
GKLpos = 180;
delay(1000);
AR_0 = analogRead(0);
if(AR_0 > drkst + thresh + allwnc)
{
while(analogRead(0) > AR_0 - allwnc);
}
else delay(2000);
}
Here is the video of the program I did before that one, you will notice that once it loses the light, it takes a while to find it again, but if the light sensor is kept in the rotational axis, the can robot can find it again much quicker than in the previous program.
And here is the code for that program:
#include [Servo.h]
Servo Tilt;
Servo Turn;
int thresh1 = 750,
pos = 0, smthns = 3,
pos2 = 180;
int cont = 0, M;
int ftx = 1;
void setup()
{
Tilt.attach(9);
Turn.attach(8);
pinMode(4, OUTPUT);
Serial.begin(9600);
pos = 0;
Tilt.write(pos2);
Turn.write(pos);
}
int forctr = 0;
void loop()
{
//Find the x axis.
for(cont = 0; cont < 1; cont++){
digitalWrite(4, HIGH);
for (M = 180; pos < M - smthns;)//turn left
{
if (analogRead(0) < thresh1)
{
pos = pos + smthns;
Turn.write(pos);
delay(smthns * 10);
}
else
{
M = pos + 20;
while(analogRead(0) > thresh1)
{
analogWrite(A2, 200);
delay(500);
analogWrite(A2, 20);
delay(200);
}
}
}
delay(100);
for (M = 15; pos > M + smthns;)//turn right
{
if (analogRead(0) < thresh1)
{
pos = pos - smthns;
Turn.write(pos);
delay(smthns * 10);
}
else
{
M = pos - 20;
cont = cont - 1;
while(analogRead(0) > thresh1)
{
analogWrite(A2, 200);
delay(500);
analogWrite(A2, 20);
delay(200);
}
}
}delay(500);}
//find y.
if (pos2 > 10 + 40)
{
pos2 -= 40;
Tilt.write(pos2);
}
else{pos2 = 180; Tilt.write(pos);}
}
P.S. I recognize that by posting my code to the public, I have given you the permission to use and modify to any extent the said code. But, this code may not be sold without permission from me, and I would appreciate it if you would give me credit if you do use this code. Thank you!.
No comments:
Post a Comment