Pages: [1]   Go Down
Author Topic: Stop sound question  (Read 496 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 152
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I got this code SO CLOSE to working but I have just one more hump to get over. I have it set up to play a start up sound (that is supposed to take about 3 seconds) and then, when a button is pushed, go on to play a different sound and light up some LEDs.

The button push and the part after it work perfectly.

The start up sound works well except I need it to STOP after about 3 seconds (when it reaches it's peak) until I push the button to trigger the rest of the festivities.  It is working except it doesn't stop UNTIL I push the button (however long I wait). Could anyone help? I believe the void setup() is where the problem is.  smiley-red

Code:
//LED Pin Variables
const int LEDS=5;
int ledPins[LEDS]={2, 3, 4, 5, 6};//An array to hold the pin each LED is connected to
int ledPin=10;//+ battery test lead connected to digital pin 10
int SPEAKER_PIN=8;
int buttonPin=9;
int buttonState = 0; // variable for reading the pushbutton status

void setup(){
// initialize the pushbutton pin as an input:
        pinMode(buttonPin, INPUT);    
  
       //Set each pin connected to an LED to output mode (pulling high (on) or low (off))
for(int i=0; i<LEDS; i++) pinMode(ledPins[i], OUTPUT);
for(int i=500; i<2000; i+=6){ // adjust startupsound here
tone(SPEAKER_PIN, i);
delay(10);
    }

    while (digitalRead(buttonPin) != HIGH); {
        // read the state of the pushbutton value:
        buttonState = digitalRead(buttonPin);
    }
}

//value should be between 0 and 255 inclusive
void present(int value){
analogWrite(ledPin, value);
for(int i=0; i<LEDS; ++i)
digitalWrite(ledPins[i], value>(256*i+128)/LEDS?HIGH:LOW);
//tone(SPEAKER_PIN, map(value, 0, 255, 0, 400));
        tone(SPEAKER_PIN, map(value, 0, 290, 0, 990));
}

« Last Edit: January 07, 2013, 12:39:17 am by arduinokov » Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

stop using delay and use blink without delay example.... the thing is the delay make your programme unable to receive any instruction for simple delay, using the delay make it easy but for most times it could make your program unresponsive
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 152
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Solved it. noTone(); worked.

 smiley
« Last Edit: January 07, 2013, 12:44:38 am by arduinokov » Logged

Offline Offline
Edison Member
*
Karma: 56
Posts: 2174
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

We like it when a plan comes together.

http://playground.arduino.cc//Code/AvoidDelay
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 152
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the replies. I will learn to avoid using delays. It seems a much better way to do it.

 smiley
Logged

Pages: [1]   Go Up
Jump to: