Tone Programm, Theremin

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){}
}

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1287248089

Oh danke für den Hinweis, wie hilfsbereit! Das ist jetzt kein konkretes Problem oder wie?

Da du so wenige Infos gibst, wollte ich Leuten die dir bereit sind zu helfen, zeigen was schon gelöst wurde, und was du überhaupt machen willst.

ok danke.... du hast keine idee?

int btnPin    = 2;
int buzzerPin = 8;
int echoPin   = 12;
int initPin   = 13;
unsigned long distance = 0; 

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
}

void loop()
{
      digitalWrite(initPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(initPin, LOW);
      distance = (pulseIn(echoPin, HIGH)/58);
      if(digitalRead(btnPin) == HIGH) //only play when the button is pressed
      {
            if(distance < 11) 
                  tone(264);
            else if(distance > 10 && distance < 21) 
                  tone(297);
            else if(distance > 20 && distance < 31) 
                  tone(330);
            else if(distance > 30 && distance < 41) 
                  tone(352);
            else if(distance > 40 && distance < 51) 
                  tone(396);
            else if(distance > 50 && distance < 61) 
                  tone(440);
            else //nix trifft zu
                  tone(666);
            delay(50);
      }
}

Ich hab nur n paar kleine Änderungen gemacht. zB. das else damit es immer zutrifft wenn kein anderer Fall zutrifft.
Das delay(50) machts immer. Eine Variable weg. Und ganz allg. den Code verkürtzt. Was natürlich der Übersicht hilft, vorallem wenn man n Fehler sucht. Achja, und es ist doch Sinnlos dass Du der tone() immer 3 Argumente mitgibst wenn 2 immer gleich sind.
Ansonsten zum debugen mal in der Tone funktion ausgeben was gespielt wird. Da ja anscheinend nur da Töne gemacht werden. (Dann weisst Du wer den undefinierbaren Ton schickt)
Hilft das weiter?

Hey danke... es sieht zumindest schonmal super aus :slight_smile: aber beim compalieren tritt ein fehler mit der meldung "at this point in file"

C:\Users\Travel\Downloads\arduino-0021\arduino-0021\hardware\arduino\cores\arduino/WProgram.h: In function 'void loop()':
C:\Users\Travel\Downloads\arduino-0021\arduino-0021\hardware\arduino\cores\arduino/WProgram.h:23: error: too few arguments to function 'void tone(uint8_t, unsigned int, long unsigned int)'
sketch_oct22a:24: error: at this point in file
C:\Users\Travel\Downloads\arduino-0021\arduino-0021\hardware\arduino\cores\arduino/WProgram.h:23: error: too few arguments to function 'void tone(uint8_t, unsigned int, long unsigned int)'
sketch_oct22a:26: error: at this point in file
C:\Users\Travel\Downloads\arduino-0021\arduino-0021\hardware\arduino\cores\arduino/WProgram.h:23: error: too few arguments to function 'void tone(uint8_t, unsigned int, long unsigned int)'
sketch_oct22a:28: error: at this point in file
C:\Users\Travel\Downloads\arduino-0021\arduino-0021\hardware\arduino\cores\arduino/WProgram.h:23: error: too few arguments to function 'void tone(uint8_t, unsigned int, long unsigned int)'
sketch_oct22a:30: error: at this point in file
C:\Users\Travel\Downloads\arduino-0021\arduino-0021\hardware\arduino\cores\arduino/WProgram.h:23: error: too few arguments to function 'void tone(uint8_t, unsigned int, long unsigned int)'
sketch_oct22a:32: error: at this point in file
C:\Users\Travel\Downloads\arduino-0021\arduino-0021\hardware\arduino\cores\arduino/WProgram.h:23: error: too few arguments to function 'void tone(uint8_t, unsigned int, long unsigned int)'
sketch_oct22a:34: error: at this point in file
C:\Users\Travel\Downloads\arduino-0021\arduino-0021\hardware\arduino\cores\arduino/WProgram.h:23: error: too few arguments to function 'void tone(uint8_t, unsigned int, long unsigned int)'
sketch_oct22a:36: error: at this point in file
C:\Users\Travel\Downloads\arduino-0021\arduino-0021\hardware\arduino\cores\arduino/WProgram.h: In function 'void loop()':
C:\Users\Travel\Downloads\arduino-0021\arduino-0021\hardware\arduino\cores\arduino/WProgram.h:23: error: too few arguments to function 'void tone(uint8_t, unsigned int, long unsigned int)'
sketch_oct22a:24: error: at this point in file
C:\Users\Travel\Downloads\arduino-0021\arduino-0021\hardware\arduino\cores\arduino/WProgram.h:23: error: too few arguments to function 'void tone(uint8_t, unsigned int, long unsigned int)'
sketch_oct22a:26: error: at this point in file
C:\Users\Travel\Downloads\arduino-0021\arduino-0021\hardware\arduino\cores\arduino/WProgram.h:23: error: too few arguments to function 'void tone(uint8_t, unsigned int, long unsigned int)'
sketch_oct22a:28: error: at this point in file
C:\Users\Travel\Downloads\arduino-0021\arduino-0021\hardware\arduino\cores\arduino/WProgram.h:23: error: too few arguments to function 'void tone(uint8_t, unsigned int, long unsigned int)'
sketch_oct22a:30: error: at this point in file
C:\Users\Travel\Downloads\arduino-0021\arduino-0021\hardware\arduino\cores\arduino/WProgram.h:23: error: too few arguments to function 'void tone(uint8_t, unsigned int, long unsigned int)'
sketch_oct22a:32: error: at this point in file
C:\Users\Travel\Downloads\arduino-0021\arduino-0021\hardware\arduino\cores\arduino/WProgram.h:23: error: too few arguments to function 'void tone(uint8_t, unsigned int, long unsigned int)'
sketch_oct22a:34: error: at this point in file
C:\Users\Travel\Downloads\arduino-0021\arduino-0021\hardware\arduino\cores\arduino/WProgram.h:23: error: too few arguments to function 'void tone(uint8_t, unsigned int, long unsigned int)'
sketch_oct22a:36: error: at this point in file

ich hoffe du kannst das beheben ich schau auch mal

also wenn ich die tone immer tone(8,330) schreibe geht es.... das problem ist jetzt ich ich den butten loslasse hört es nicht auf. zu spielen

Error: too few arguments. = Fehler: Zu wenig Argumente

Das ist, weil ich nur Tone und die Note hingeschrieben habe. Du musst natürlich die Funktion "tone" anpassen, so dass sie nur noch ein Argument annimmt. Jetzt erwartet sie 3 Argumente, deshalb der Fehler.

Ist ein Online C Buch, dass man auch in Buchform kaufen kann. Da werden solche Sachen erklärt. Was ist eine Funktion. Was sind Funktions-Prototypen, Argumente und wie funktioniert das Ganze. Stichwort Kopie oder Pointer etc.

Naja, ich vermute mal, dass deine tone funktion einfach den letzten Ton spielt weil nie ein Hinweis kommt, dass sie aufhören soll.
Du kannst ja mal unter das delay(50) noch tone(0) hinschreiben.

Kannst Du mir mal die tone Funktion zeigen?

int btnPin = 2;
int buzzerPin = 8;
int echoPin = 12;
int initPin = 13;
unsigned long distance = 0;

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
}

void loop()
{
digitalWrite(initPin, HIGH);
delayMicroseconds(10);
digitalWrite(initPin, LOW);
distance = (pulseIn(echoPin, HIGH)/58);
if(digitalRead(btnPin) == HIGH) //only play when the button is pressed
{
if(distance < 5)
tone(8,264);
else if(distance > 6 && distance < 10)
tone(8,131);
else if(distance > 11 && distance < 15)
tone(8,147);
else if(distance > 16 && distance < 20)
tone(8,165);
else if(distance > 21 && distance < 25)
tone(8,175);
else if(distance > 26 && distance < 30)
tone(8,196);
else if(distance > 31 && distance < 35)
tone(8,220);
else if(distance > 36 && distance < 40)
tone(8,247);
else if(distance > 41 && distance < 45)
tone(8,262);
else if(distance > 46 && distance < 50)
tone(8,294);
else if(distance > 51 && distance < 55)
tone(8,330);
else if(distance > 56 && distance < 60)
tone(8,349);
else if(distance > 61 && distance < 65)
tone(8,392);
else if(distance > 66 && distance < 70)
tone(8,440);
else if(distance > 71 && distance < 75)
tone(8,494);
else //nix trifft zu;
noTone(8);
delay(50);

}
else
noTone(8);
}

dies ist mein aktuelles programm, jetzt gehts auch so wie ich will. schönheitsfehler ist noch das die übergänge iwie nciht so sauber sind wie ich das will

die tone funktion ist glaube schon bei aruino vorprogrammiert. siehe http://arduino.cc/en/Tutorial/Tone