Hi
hab nen Problem hab den parallax ping und den Standard code und möchte jetzt den Distanz Daten ein audi ton zuweisen
const int pingPin = 7;
int speaker = 3;
int distance = duration /29/2;
void setup() {
// initialize serial communication:
Serial.begin(9600);
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
pinMode(speaker, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
long microsecondsToCentimeters (long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
// Umwandlung der Ton Information in Töne
distanceToAudiosignales;
{
if ((distance <= 50) && ( distance >= 35));
tone(3, 50,1000);
delay (1000);
if ((distance <= 34) && ( distance >= 20));
tone(3,40,500);
delay(1000);
if ((distance <= 19) && ( distance >= 10));
tone(3,35,420);
delay(300);
if ((distance <=9) && ( distance >= 5));
tone(3,30,300);
delay (100);
}/code]
so wie deklariert man nun die Distanz wenn diese erst in der loop ermittelt wird ? wie kann ich die variable mit dem Distanz wert dann in der if schleife wieder abfragen/aufrufen ??
Bitte um Hilfe :)
Quack