Trying to control two HS-311 standard Servos with two HC-SR04 Ultrasonic Sensors


hey everyone!

for project i'm trying create motion following animatronic head,

 i lucky enough find perfect code on instructables work ( here's link it).

and here video of original code in action: youtube.

(just restate did not create original code got nice enough share)
 
so i've been working adjust code control 2 servos, , have 1 servo follow sensors detect , have other basic sweep when sensors detect @ or under 5 inches away.

i'm beginner when comes coding has proven bit difficult, tried creating 2 different loops either came errors or 1 servo work while other did nothing.

 here code i've been working with:

code: [select]



#include <servo.h>

servo yservo; servo xservo;//servos x , y

const int lin = 10, rin = 12, lout = 11, rout = 13, yserv = 9, xserv = 8; //setting sensor pins , servo pin

// establish variables duration
// , distance result in inches
long rduration, lduration, rinches, linches;

int threshold = 10; //sensor threshold in inches

int angle = 80; //initial angle

int pos = 0;

boolean debug = false; //serial communication debuging. set true serial communication.

void setup() {
  // initialize serial communication:
  if (debug)
  {
    serial.begin(9600);
  }
  yservo.attach(9); //attach servo pin 9
  xservo.attach(8);// attach servo pin 8
}

void loop()
{
 
  //most of sensor code has been taken david mellis's ping sensor code
  //i modified 4 pin sensor oppsed 3 pin sensor
  // give short low pulse beforehand ensure clean high pulse:
  pinmode(rout, output);
  digitalwrite(rout, low);
  delaymicroseconds(2);
  digitalwrite(rout, high);
  delaymicroseconds(5);
  digitalwrite(rout, low);

  rduration = pulsein(rin, high);
 
  pinmode(lout, output);
  digitalwrite(lout, low);
  delaymicroseconds(2);
  digitalwrite(lout, high);
  delaymicroseconds(5);
  digitalwrite(lout, low);

  lduration = pulsein(lin, high);

  // convert time distance
  rinches = microsecondstoinches(rduration);
  linches = microsecondstoinches(lduration);
 
  if (debug)
  {
    serial.print("left: ");
    serial.print(linches);
    serial.println(" in");
    serial.print("right: ");
    serial.print(rinches);
    serial.println(" in");
  }
  follow();
 
}

long microsecondstoinches(long microseconds)
{
  // according parallax's datasheet ping))), there are
  // 73.746 microseconds per inch (i.e. sound travels @ 1130 feet per
  // second).  gives distance travelled ping, outbound
  // , return, divide 2 distance of obstacle.
  // see: http://www.parallax.com/dl/docs/prod/acc/28015-ping-v1.3.pdf
  return microseconds / 74 / 2;
}

void follow()
{
  if (linches <= threshold || rinches <= threshold)
  {
    if (linches + 2 < rinches)
    {
      angle = angle - 2;
    }
    if (rinches + 2 < linches)
    {
      angle = angle + 2;
    }
  }
  if (angle > 160)
  {
    angle = 160;
  }
  if (angle < 0)
  {
    angle = 0;
  }
  yservo.write(angle);

{
  if (linches <= 5 || rinches <= 5)
{
  for(pos = 0; pos <= 180; pos += 1) // goes 0 degrees 180 degrees
  {                                  // in steps of 1 degree
    xservo.write(pos);              // tell servo go position in variable 'pos'
    delay(15);                       // waits 15ms servo reach position
  }
  for(pos = 180; pos>=0; pos-=1)     // goes 180 degrees 0 degrees
  {                               
    xservo.write(pos);              // tell servo go position in variable 'pos'
    delay(15);                       // waits 15ms servo reach position
  }
  }
   
 
  xservo.write(angle);
 
}
}





i appreciate or if point me in right direction. let me know if there information needed have not included,
 thank you!

you need move away using loops , delays restrict moving 1 servo other servo because 1 loop need end before other starts.  look @ blinkwithoutdelay example in ide , examine principle uses.

save start time of action, such moving servo small distance each time through loop() check whether time move again.  if it, else else such checking whether time move second servo little.  if it, else go round loop() again.

by way, how servos powered ?  have external power supply (correct answer) or powered arduino (wrong answer).


Arduino Forum > Using Arduino > Programming Questions > Trying to control two HS-311 standard Servos with two HC-SR04 Ultrasonic Sensors


arduino

Comments

Popular posts from this blog

invalid use of void expresion in FlexiTimer2 library

error: a function-definition is not allowed here before '{' token

LED Strip Code