M.E. Team
M.E. Team

Reputation: 31

Arduino infiniti tone

I am making a smoke detector. When it detects smoke iz should alert with buzzer. Is there any way I could make it to buzz forever until external interupt such as restart pin? Or could I just remove timing from tone() function. Here is the code I use.

int sensorValue;

int digitalValue;
int green = 8;
int red = 7;

void setup(){

  Serial.begin(9600);

  pinMode( 0, INPUT);
  pinMode(green, OUTPUT);
  pinMode(red, OUTPUT);


}

void start(){
    digitalWrite(green, HIGH);
}

void loop() {

sensorValue = analogRead(0);       
digitalValue = digitalRead(0); 

Serial.println(sensorValue,DEC);  
Serial.println(digitalValue,DEC);

delay(2000);                    
  if(analogRead(0) < 100){
    tone(9,200,1000);
    digitalWrite(red,HIGH);
  }
 }

Upvotes: 1

Views: 238

Answers (3)

m gold
m gold

Reputation: 21

if you're really bent on using interrupts you didn't specify what board you're working with but for uno the 2 3 pins can be attached as interrupts and just trigger a function that turns off the tone check out this: attachinterrupt

Upvotes: 0

datafiddler
datafiddler

Reputation: 1835

Playing a sound "forever" is straightforward:

if(analogRead(A0) < 100 ) {
  tone(9,2000); // once triggered, will play the sound forever
}

To switch it off, you seem to like the RESET button. So there's no need to ever call

noTone(9);

BTW: what about reading the reference ?

Upvotes: 2

Gerhard
Gerhard

Reputation: 7069

There is lots of ways:

Change your logic that activate the buzzer.

while (analogRead(0) < 100){
  tone(9,200,1000);
}

Just use an infinite loop:

while (1) {
  tone(9,200,1000);
}

Reset the Arduino to get out of the infinite loop.

An variation on this would be to replace (1) with the code that checks a pin to exit the loop or reads the sensor.

Upvotes: 0

Related Questions