How do i make it when i continually push a button down sound is outputted from a piezo speaker and when the button is released the sound stops?
Please post the code that you are using to drive the piezo speaker. In code tags.
88jake:
How do i make it when i continually push a button down sound is outputted from a piezo speaker and when the button is released the sound stops?
while (digitalRead(theInputPin) == LOW) //or test for HIGH depending on your circuit (don't let the input float)
{
//code here to create sound
}
NOTE : this blocks program execution until the button is released which may not work for you if the program needs to be doing something else
Im making a morse code decoder and want it so when i press the button it outputs sound for how long i hold it down
here is the code
unsigned long signal_len,t1,t2; //time for which button is pressed
int inputPin = 2; //input pin for push button
int ledPin = 4; //outpu pin for LED
int soundPin = 6; //piezo output
String code = ""; //string in which one alphabet is storedvoid setup() {
Serial.begin(9600);
pinMode(inputPin, INPUT_PULLUP); //internal pullup resistor is used to simplify the circuit
pinMode(ledPin,OUTPUT);
pinMode(soundPin,OUTPUT);
}void loop()
{
NextDotDash:
while (digitalRead(inputPin) == HIGH) {}
t1 = millis(); //time at button press
digitalWrite(ledPin, HIGH); //LED on while button pressed
while (digitalRead(inputPin) == LOW) {}
t2 = millis(); //time at button release
digitalWrite(ledPin, LOW); //LED off on button release
signal_len = t2 - t1; //time for which button is pressed
if (signal_len > 50) //to account for switch debouncing
{
code += readio(); //function to read dot or dash
}
while ((millis() - t2) < 700) //if time between button press greater than 0.5sec, skip loop and go to next alphabet
{
if (digitalRead(inputPin) == LOW)
{
goto NextDotDash;
}
}
convertor(); //function to decipher code into alphabet
}char readio()
{
if (signal_len < 400 && signal_len > 50)
{
return '.'; //if button press less than 0.4sec, it is a dot
}
else if (signal_len > 400)
{
return '-'; //if button press more than 0.4sec, it is a dash
}
}void convertor()
{
static String letters[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-",
".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "E"
};
int i = 0;
if (code == ".-.-.-")
{
Serial.print("."); //for break
}
else
{
while (letters != "E") //loop for comparing input code with letters array
- {*
_ if (letters == code)_
* {*
* Serial.print(char('A' + i));*
* break;*
* }*
* i++;*
* }*
_ if (letters == "E")
* {
Serial.println(""); //if input code doesn't match any letter print wrong input*
* }
}
code = ""; //reset code*
}[/quote]_
Im making a morse code decoder and want it so when i press the button it outputs sound for how long i hold it down
From your program
while (digitalRead(inputPin) == HIGH) {}
Waits until the button is pressed
and 2 lines later
while (digitalRead(inputPin) == LOW) {}
Waits until the button is released. Why ?
Then later
if (digitalRead(inputPin) == LOW)
{
goto NextDotDash;
}
Why ?
Not part of the problem, but unwise, is the use of goto. It is not necessary or advisable
It's really easy to add sound to your sketch. You already have it looping when the key gets pressed and when it gets released. All you have to do is start and stop the sound at the right places. And define the constants soundPin and soundFrequency.
void loop()
{
NextDotDash:
while (digitalRead(inputPin) == HIGH) {}
t1 = millis(); //time at button press
tone(soundPin, soundFrequency); // Start the sound
digitalWrite(ledPin, HIGH); //LED on while button pressed
while (digitalRead(inputPin) == LOW) {}
t2 = millis(); //time at button release
noTone(soundPin); // Stop the sound
digitalWrite(ledPin, LOW); //LED off on button release
...
Incidentally, HeliBob is correct that you don't really need the goto. loop() by definition loops. That's what it does. You could just do a return; there instead of goto.
thank you i got it to work don't know why i couldn't get it to.