Go Down

Topic: servo stoppen wenn ping weniger als 100cm ist (Read 3242 times) previous topic - next topic

lukasm

moin moin leute

ich versuche im moment eine ventilator zu bauen welcher einen erkennt also bemerkt wenn man davositzt. mein problem ist das ich es nicht schaffe den servo zu stoppen wenn der lüfter auf mich zeigt. besser gesagt der servo macht garnicht das was ich will :(

hier ist mal der code

int fanPin = 13;
int pingPin = 7;
#include <Servo.h>
Servo myservo;
int pos = 0;




void setup() {
 pinMode(fanPin, OUTPUT);
}
void loop() {
 
 long duration, inches, cm, m;
 pinMode(pingPin, OUTPUT);
 digitalWrite(pingPin, LOW);
 delayMicroseconds(5);
 digitalWrite(pingPin, HIGH);
 delayMicroseconds(5);
 digitalWrite(pingPin, LOW);
 
 pinMode(pingPin, INPUT);
 duration = pulseIn(pingPin, HIGH);

 
 inches = microsecondsToInches(duration);
 cm = microsecondsToCentimeters(duration);


 if(cm < 100){
   digitalWrite(fanPin, HIGH);
;
   
 
   }
 else {
   digitalWrite(fanPin, LOW);
   myservo.write(0)
   myservo.write(180)
 }

   
   

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(100);
}
long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}


PaulS

Code: [Select]
  myservo.write(0)
  myservo.write(180)

Missing semicolons?

Code: [Select]
Servo myservo;
Where is the servo attached to a pin?

lukasm

oh i forgot the myservo.attach(9) but i still dosent stop when the objekt is under 100 cm the servo just dosent move after reaching 180

PaulS

I can't read German (?), so could you describe the problem you are having, in English?

madworm

#4
Jan 15, 2011, 06:29 pm Last Edit: Jan 15, 2011, 06:41 pm by madworm Reason: 1
He's trying to make a fan pan back and forth (using a servo), much like a sentry gun. But the bullets are replaced with a cold breeze of air. It seems the servo part is giving him trouble. Especially making it pan in a sensible way, while listening to the ping sensor.

Maybe it would be helpful to scan in smaller increments and give the servo a bit more time to react. I'm assuming that servo.write(0) and servo.write(180) should make it do a full scan.

From his description of the servo's "misbehaviour" it seems that servo.write() is nonblocking and returns instantly.

lukasm

yeah sry i should have writen in english. what im finding a little strange is, if i write the part myservo.write(0) myservo.write(180) in a seperate sketch it works perfectly the servo just sweeps 180° back and fourth but if i write it in the sketch with the ping sensor it just blocks at the fist 180° spin :-/

PaulS

The servo.write() function is non-blocking. It tells the servo to get moving, and returns. The fact that the servo physically takes time to get to the commanded position is irrelevant.

If it really isn't irrelevant, you need to add a small delay while you wait for the servo to get into position.

Moving the servo from 0 to 180, without pinging anywhere along the sweep doesn't make sense to me. If you were moving the servo, pinging, moving, pinging, etc., and doing something with the results of the pinging, that would make sense.

Go Up