IRDistanceSensor

IRDistanceSensor


mimicArm Grabs a Block 

This example takes user interaction to a higher level.  Using the Sharp IR Distance Sensor mimicArm detects the block in the users hand, moves toward it, and grabs it.  If mimicArm detects that the block wasn't grabbed it will start over, otherwise it will move the block to the bucket. 

Commands Used

if(conditional);
else if(conditional);
else;
analogRead(pin);
robotMove(x,y);
gripperMove(x);
robotJog(x,y);
robotGrab(x);
delay(x);

Setup:

IR Distance Sensor Wiring

Connect your Sharp IR Distance to 5V, ground and pin A5 as shown.  Arduino is shown, but you can use the pins on your mimicArm controller that's attached to your Arduino.  Mount the IR Distance sensor in place of mimicArm's eyes. 

Download IRDistanceSensor.ino for Arduino

Cut and paste code:

#include <robot.h>

void setup() {
  // put your setup code here, to run once:
  robotActivate();
  delay(2000);
}

void loop() {
  //robotMode(ARDUINO);
  int distance = analogRead(A5);
  gripperMove(100);  //set the gripper to slightly opened
  robotMove(1, 127); //move to start postiion
  robotMove(3, 150); //move to start position
  robotGrab(0);      //turn off gripper motor
  if (distance <= 300) robotMove(2, 170);  //Numbers below 300 indicate no block is there
  else if (distance > 300  && distance < 350) robotJog(2, -1);  //The block is too close, back up
  else if (distance > 400) robotJog(2, 1);  //The block is too far away, get closer
  else if (distance > 350 && distance < 400) {  //The block is just right.  Get it!
    robotGrab(-75);   //open the gripper some
    delay(500);       //wait for gripper to open
    for (int x = 0; x < 10; x++) {  //slowly move gripper forward to get block
      robotJog(2, -2);
      delay(20);
    }
    robotGrab(100);  //grab the block
    delay(2000);     //wait for gripper to move
    if (gripperPosition() < 30) {  //if gripper position is below 30 it didn't get the block.  Start over.
      robotGrab(0);
      gripperMove(100);
      delay(2000);
    }
    else {  //We got the block, now move it to the bucket.
      robotMove(2, 127);
      delay(100);
      robotMove(1, 255);
      delay(1000);
      delay(1000);
      robotGrab(0);
      gripperMove(100);
      delay(2000);
      robotMove(2, 127);
      delay(1000);
    }
  }
  else robotMove(2, 100);
  delay(20);
}