Author Topic: Ultraschallsensor  (Read 11701 times)

SeviT

  • Newbie
  • *
  • Posts: 42
  • Karma: +1/-0
Ultraschallsensor
« on: 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


pylon

  • freakyfriday
  • Full Member
  • *
  • Posts: 158
  • Karma: +16/-0
Re: Ultraschallsensor
« Reply #1 on: December 19, 2012, 07:24:13 PM »
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.

Code: [Select]
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;

}

SeviT

  • Newbie
  • *
  • Posts: 42
  • Karma: +1/-0
Re: Ultraschallsensor
« Reply #2 on: December 20, 2012, 02:36:20 PM »
hi

danke für die Antwort :)

lg

Sevi

 

anything