88Views1Replies

Author Options:

My PING sensor works only in one direction when the ping sensor is rotated to complete right direction . . . . having trouble in code :/ plz guide me for the code thanQ :) Answered

// here is the code 
// problem facing with the HEAD servo and the PING sensor 
// this is paning from left to right but the problem is that PING works only in 1 direction.

#define trigPin 3
#define echoPin 12
#define led 4
#define relay 8

Servo sl;
Servo head;
Servo sr;
Servo el;  
Servo er;
int posh=0;
int posl=0;
int posr=0;
void setup()
{
  sl.attach(9);
  sr.attach(10);
  el.attach(11);
  er.attach(5);
  head.attach(6);
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
   pinMode(relay, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
}

void loop()
{

  for(posh = 0;posh <= 180; posh +=1)
  {
 delay(15);
    head.write(posh);
  }

  for(posh = 180;posh >=0; posh -=1)
  {

    head.write(posh);
delay(15); 
}



  //shoulder up
  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;

if (distance < .1)
{  // This is where the LED On/Off happens
     digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
     Serial.println("WELCOME");
   head.write(90);


  for(posl = 0, posr = 180; posl <= 90 ; posl +=1)
  {

    sl.write(posl);
     sr.write(posr);
     posr -=1;
    delay(15);
  }


    // elbow close

  for(posr = 0,posl=180; posr <= 45; posr +=1)
  {
    er.write(posr);
    el.write(posl);
    posl -=1;
    delay(15);
  }

  delay(3000);
  //elbow open


  for(posr = 45,posl=135; posr >= 0; posr -=1)
  {
    er.write(posr);
    el.write(posl);
    posl +=1;
    delay(15);
  }

  //shoulder down


  for(posr = 90,posl= 90; posr <= 180; posr +=1)
  {
    sr.write(posr);
    sl.write(posl);
    posl -=1;
    delay(15);
  }

   digitalWrite(relay,HIGH);
   delay(1000);
    digitalWrite(relay,LOW);

  }

  else
  {
    digitalWrite(led,LOW);
  }

  delay(100);

  }

Comments

The forums are retiring in 2021 and are now closed for new topics and comments.
0
mpilchfamily
mpilchfamily

7 years ago

Go back an annotate your code so we know what is going on. Tell us what each variable is for and what is going on with teh for() loops in the Void loop.

Pending that i'll guess that the for() loops are moving the servo that has the sensor attached to it right? If so then the servo is going form 0 to 180 degrees and back again before the controller gets to the code that polls the sensor and performs the actions based on the sensor's readings.

I would set up a separate void (lets call it void Sensor) for the sensor then replace the delay in your for() loops with Sensor. This way every time the servo moves a step it will jump down to void Sensor and perform the required actions before continuing on with the for() loop.