voila j'ai un capteur sharp GP2Y0A02YKOF et un micro servo 9g.
Je voudrais savoir comment faire pour que les servo sert de balayage au capteur et que quand le capteur detecte un objet il le suit. (tracking object) j'ai chercher des petits tutos sur google , mais rien !
j'ai un code qui fait tourner le servo de gauche a droite(balayage) avec le capteur dessus et quand il detecte un obstacle tout s'arrête , quand il n'y a plus d'obstacles le servo continu a tourner de gauche a droite.
mais je ne sais pas comment faire pour qu'au lieu qu'il s'arrête , eh bien il suit l'objet.
voila le code en question
#include <Servo.h>
#define sensorIR 15
Servo myservo; // Creates a servo object
int pos = 0; // Variable to store the servos angle
volatile float inches;
void setup()
{
myservo.attach(2);
Serial.begin(9600);
}
void loop()
{
search();
}
//This function sweeps the servo searching the area for an object within range
void search()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
sensorPrint(analogRead(sensorIR));
if (inches < 10) follow();
}
for(pos = 180; pos>=1; pos -= 1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
sensorPrint(analogRead(sensorIR));
if (inches < 10) follow();
}
}
//This function determines the distance from the servo and prints it to the SMonitor
void sensorPrint(float sensorValue)
{
inches = 4192.936 * pow(sensorValue,-0.935) - 3.937; //algorithm from last post
//cm = 10650.08 * pow(sensorValue,-0.935) - 10; //if you use cm instead of in
Serial.print("Inches: ");
Serial.println(inches);
}
//This function only determines the distance from the servo without printing
void noPrint(float sensorValue)
{
inches = 4192.936 * pow(sensorValue,-0.935) - 3.937; //algorithm from last post
//cm = 10650.08 * pow(sensorValue,-0.935) - 10; //if you use cm instead of in
}
//This function keeps the servo locked on the object until it moves out of range
void follow()
{
while (inches < 10)
{
noPrint(analogRead(sensorIR)); //we want to find the distance but not print it
Serial.print("Angle: "); //Current angle of the servo between 0 to 180
Serial.println(pos);
}
}














