Pages: [1]   Go Down
Author Topic: Maxsonar sensors with servos  (Read 669 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. 
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26331
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Seeing your code would be a great help.
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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);
  }]
Logged

Pages: [1]   Go Up
Jump to: