Hi, I'm 13 and new to Arduino and my first program was the Tone program. I'm trying to create a dog whistle that plays for a few seconds after I press a button. I'm trying to figure out the programming so the sound doesn't go off when I stop pressing the button. Here's a piece of my program,
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// sound tone
tone(8, note1);
}
else {
//turn off sound
notone(8);
}
}
See the cool smilie in your code, you didn't put it there the forum software did. That is why we ask people to put code in code tags. Read the how to use this forum sticky to see how.
You have to be clear in your mind exactly what you want to do when you write a program. Do you want the sound never to stop or do you want it to stop a fixed time after you press the button or a fixed time after you release the button?
What you need to do is to use the millis timer to make a note of the time when the action to stop the time occours, then in the lop you keep checking if that time is up, if it is then stop the sound.
This is known as a state machine and you can see an example in the blink without delay example in the Arduino's IDE.
Thanks for the help. I made a code that plays for 5 seconds after I stop pressing a button, but as soon as I upload the code to the Arduino, it plays for 5 seconds without pressing the button. Here is my code (with code tags)
/* Simple Tone Keyboard
Gregg Horton 2011
*/
#define NOTE_DS8 24013
const int buttonPin = 2; // the number of the pushbutton pin
int note1 = NOTE_DS8; // define note sound
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
long previousMillis = 0;
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 5000; // interval at which to blink (milliseconds)
void setup() {
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
unsigned long currentMillis = millis();
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// sound tone
tone(8, note1);
// don't start counting yet
previousMillis = currentMillis;
}else
// start counting now
if(currentMillis - previousMillis > interval)
// turn off tone
noTone(8);
else
// keep playing
tone(8, note1);
}
Hey, I'm trying to make a bark activated dog whistle. At the moment, the whistle is at 24kHz. When it activates, I can't get it to shut off. Is it feedback between my microphone and speaker that keeps it from shutting off? I'm using a ZX-Sound analog microphone. I hoped the dog whistle would be beyond the frequency that the microphone picked up. Here's the code I'm using.
/* Knock Sensor
created 25 Mar 2007
by David Cuartielles <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
*/
#define NOTE_DS8 24000
// these constants won't change
const int knockSensor = A0; // the piezo is connected to analog pin 0
const int threshold = 800; // threshold value to decide when the detected sound is a knock or not
int note1 = NOTE_DS8;
// these variables will change:
int sensorReading = 0; // variable to store the value read from the sensor pin
//int ledState = LOW; // variable used to store the last LED status, to toggle the light
long previousMillis = 0;
long currentMillis;
long interval = 5000;
void setup() {
//pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
//Serial.begin(9600); // use the serial port
previousMillis = currentMillis - 10000;
}
void loop() {
// read the sensor and store it in the variable sensorReading:
sensorReading = analogRead(knockSensor);
// if the sensor reading is greater than the threshold:
if (sensorReading >= threshold) {
// sound tone
tone(8, note1);
// don't start counting yet
previousMillis = currentMillis;
}
else
// start counting now
if(currentMillis - previousMillis > interval)
// turn off tone
noTone(8);
else
// keep playing
tone(8, note1);
}
The variable currentMillis is only defined in the setup function so it is meaningless.
When setting the start of the tome timing you should use the millis() function:-
previousMillis = millis();
and when testing if the time is up again you should use the current value of the millis counter:-