boxtec Forum
Microcontroller => Sensors => : SeviT December 18, 2012, 10:55:11 PM
-
Hallo Zusammen
Ich habe einen Ultraschallsensor und habe ein Programm im INternet gefunden
int pingPin = 13;
int inPin = 12;
void setup() {
Serial.begin(9600);
}
void loop()
{
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
pinMode(inPin, INPUT);
duration = pulseIn(inPin, HIGH);
cm = microsecondsToCentimeters(duration);
Serial.println(cm, DEC);
delay(100);
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
Ich möchte dieses Programm jetzt so ergänzen, dass man z.Bsp. bei 5cm eine Led leuchtet und bei 10cm. Ich habe es bis jetzt nicht hingekriegt.
Vielen Dank für Hilfe
LG
Sevi
-
Der folgende Code lässt die LED an Pin 6 leuchten, wenn ein Objekt näher als 5cm ist, die LED an Pin 7 näher als 10cm.
int pingPin = 13;
int inPin = 12;
uint8_t led5cm = 6;
uint8_t led10cm = 7;
void setup() {
Serial.begin(9600);
pinMode(pingPin, OUTPUT);
pinMode(inPin, INPUT);
}
void loop()
{
long duration, inches, cm;
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
duration = pulseIn(inPin, HIGH);
cm = microsecondsToCentimeters(duration);
digitalWrite(led5cm, cm <= 5 ? HIGH : LOW);
digitalWrite(led10cm, cm <= 10 ? HIGH : LOW);
Serial.println(cm, DEC);
delay(100);
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
-
hi
danke für die Antwort :)
lg
Sevi