Hallo!
Mein Problem ist, dass die Töne nur direkt bei nach dem Upload funktionieren (Tonleiter) wenn der Value für distance über 60 ist kommt ein für mich unnachvollziehbarer Ton. Auch sobald ich loslasse den Knopf zu drücken ist die Tonleiter "zerstört". Ich vermuthe das die Else nicht wirklich defeniert ist oder sowas. Return ro zero geht in der loop ja auch nicht.
Hier mein Code:
int btnPin = 2;
int buzzerPin = 8;
int echoPin = 12; // SRF05 echo pin (digital 2)
int initPin = 13; // SRF05 trigger pin (digital 3)
unsigned long pulseTime = 0; // stores the pulse in Micro Seconds
unsigned long distance = 0; // variable for storing the distance (cm) we'll use distance as a switch for the speaker
//setup
void setup() {
pinMode(buzzerPin, OUTPUT); //set speaker pin to output
pinMode(btnPin, INPUT); //set pushbutton pin to input
pinMode(initPin, OUTPUT); // set init pin 3 as output
pinMode(echoPin, INPUT); // set echo pin 2 as input
Serial.begin(9600); // start the serial port
}
// execute
void loop() {
digitalWrite(initPin, HIGH); // send 10 microsecond pulse
delayMicroseconds(10); // wait 10 microseconds before turning off
digitalWrite(initPin, LOW); // stop sending the pulse
pulseTime = pulseIn(echoPin, HIGH); // Look for a return pulse, it should be high as the pulse goes low-high-low
distance = pulseTime/58; // convert the pulse into distance (cm)
Serial.println(distance);
if (digitalRead(btnPin) == HIGH) //only play when the button is pressed
{
/digitalWrite(initPin, HIGH); // send 10 microsecond pulse
delayMicroseconds(10); // wait 10 microseconds before turning off
digitalWrite(initPin, LOW); // stop sending the pulse
pulseTime = pulseIn(echoPin, HIGH); // Look for a return pulse, it should be high as the pulse goes low-high-low
distance = pulseTime/58; // convert the pulse into distance (cm)/
if (distance <= 10)
{
tone(8,264,100);
Serial.println(distance); // print the distance value to the serial port
delay(50); // delay for 50 milliseconds before starting again...
}
if (distance >= 11 && distance <= 20)
{
tone(8,297,100);
Serial.println(distance); // print the distance value to the serial port
delay(50); // delay for 50 milliseconds before starting again...
}
if (distance >= 21 && distance <=30)
{
tone(8,330,100);
Serial.println(distance); // print the distance value to the serial port
delay(50); // delay for 50 milliseconds before starting again...
}
if (distance >= 31 && distance <= 40)
{
tone(8,352,100);
Serial.println(distance); // print the distance value to the serial port
delay(50); // delay for 50 milliseconds before starting again...
}
if (distance >= 41 && distance <= 50)
{
tone(8,396,100);
Serial.println(distance); // print the distance value to the serial port
delay(50); // delay for 50 milliseconds before starting again...
}
if (distance >= 51 && distance <= 60)
{
tone(8,440,100);
Serial.println(distance); // print the distance value to the serial port
delay(50); // delay for 50 milliseconds before starting again...
}
/else{
tone(8,0,100);
delay(50);
}/
}
//else if (digitalRead(btnPin) == LOW){}
}