Go Down

Topic: Maxsonar sensors with servos (Read 780 times) previous topic - next topic

So i am using lv-maxsonar with 2 servos. i want to use the sensors with the servos for motion detection. I am using the sweep coding arduino offers and I know that there is a lot of code for distance reading with the sensors but i cant figure out how to get the sensors to work with the servos when i put my hand in front of the sensors. I am going to use two or three sensors so it can cover a wide range. Any assistance would be appreciated. 

AWOL

Seeing your code would be a great help.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Code: [Select]
i don't have any set code but this. the servo moves but it only goes to one angle and does not follow anything. The servo just moves one time once i put my hand in front of the sensor.

[#include <Servo.h>  // Library for the servo's
Servo myservotop;
Servo myservobottom;    // Object Declaration Servo
int pos = 0;
void setup()
{
  Serial.begin(9600);  // start servo communication
  myservotop.attach(9);     // servo that pin 9
  myservobottom.attach(8);
}
void motion()
{
  for(pos = 40; pos < 160; pos += 2)  // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservotop.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                                    // waits 15ms for the servo to reach the position
  }
  for(pos = 40; pos>=0; pos-=2)
  {myservobottom.write(pos);
    delay(15);
  } 
  for(pos = 40; pos>=0; pos-=2)     // goes from 180 degrees to 0 degrees
  {                               
    myservotop.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for(pos = 40; pos < 160; pos += 2)
  {
    myservotop.write(pos);
    myservobottom.write(pos);
    delay(15);               
  }
  }  void loop()
{
//in our quadrant (90 degrees) to be launched about 1.5 meters (60 inches)
//Sensor everything closer than about 5 inches by 5 inches determined as
//So we map the values of 10-120 0-90 degrees
  int minDistance = 10;
  int maxDistance = 120; 
  int spread = maxDistance - minDistance;
  int quadrant = 180;
 
  int distance = analogRead(0);  // We load value sensor 
  // certainly position servo
  int position = (float)quadrant / (float)spread * (float)distance; 
 
  // limit Quadrant (90 degrees)
  position = min(position, 180);
  position = max(position, 50);
 
  // set position servo
  myservotop.write(position);
  myservobottom.write(position); 
 
  // value send after serial lynx
  Serial.print(distance);
  Serial.print(":");
  Serial.println(position);
 
  // wait 1/5 second do go further measurements
  // hand will be less bound
  delay(200);
  }]

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy