Pages: [1]   Go Down
Author Topic: tone instruction does not operate with interrup  (Read 434 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi every body,
 instruction tone ( xpin, yHz) does not operate when it is in a interrupt routine/
Here is my code
Code:
byte HP = 8;
byte Ledrouge = 7;
byte Ledverte =6;
byte Bouton = 3;

void setup (){
   pinMode ( HP, OUTPUT);
   pinMode (Ledrouge, OUTPUT);
   pinMode (Ledverte, OUTPUT);
   pinMode (Bouton, INPUT);
   attachInterrupt (1, interruption, RISING);
}
void loop () {
   digitalWrite (Ledrouge, HIGH);
   delay (500);
    digitalWrite (Ledrouge, LOW;
    delay (500);
   }

void interruption () {
    tone (HP, 650);
    digitalWrite (Ledverte, HIGH);
    for (volatile int i =0; i< 100; i++) {
       delayMicroseconds (5000);
       }
    noTone;
    }
when this progamme operate, I don't hear nothing

Thanks for help and best regards

Moderator edit: CODE TAGS

  
  
« Last Edit: January 26, 2013, 01:02:19 pm by AWOL » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26287
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
noTone;
What's that?
Why have you got a delay in an interrupt?
Why is 'i' declared volatile?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 613
Posts: 49310
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How do you suppose that tone() makes the pin wiggle appropriately? Hint: It use interrupts, which are disabled in an interrupt service routine.

Why is the local variable i declared volatile?

More importantly, what are you trying to do? Whatever it is, your approach is all wrong.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi AWOL, Hi PaulS,
Thank for your responses. In fact I try to start a sound and in the same time a led ON, when an interruption is detected (Int 1 on pin 3 of arduino board UNO).
This is a part of a small programme to stop a engine of a motorcycle when I put ON the rear speed: the sensor of rear speed is connected on the interrupt 1
In my progam, rear speed is the "Bouton" variable and it is on the interrutp pin .

 I follow councils to use volatile variable and delayMicroseconds in a interrupt subroutine.

Thanks for help, and sorry for my bad english, because I am french
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26287
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I follow councils to use volatile variable and delayMicroseconds in a interrupt subroutine.
You only use volatile if the variable is shared between interrupt and non interrupt contexts.
Your variable "i" was a local and absolutely should not be declared volatile, because of the potential performance hit you could take.
Any variable larger than a byte on the AVR should have further protection to ensure, for instance, the halves of a sixteen bit update are not split across an interrupt.
« Last Edit: January 28, 2013, 02:58:07 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4257
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In fact I try to start a sound and in the same time a led ON, when an interruption is detected (Int 1 on pin 3 of arduino board UNO).
You don't have to stay in the interrupt routine while the sound is made.  The ISR could just set a global variable to a value checked by the loop() function and acted upon if the button has been pressed.  You still need to consider if you want the code in loop() to stall while the sound is on.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you to all?
I understand the tone instruction does not operate when it is  inside  an interrupt routine.
I will choose an other solution.
write you later , GF
Logged

Pages: [1]   Go Up
Jump to: