This is my first time using this forum, so bear with me if I've made a mistake somewhere. I'm very new to Arduino and still trying to learn how it works.
Okay, so I'm working on a small project for a class, where I have a Zelda-style treasure chest. I want to hook up this Arduino Protosnap Mini to it. I'm going to put the LED on the lock of the chest, a light sensor on the inside of the chest, and the board on the back.
What I want it to do is detect when the chest is opened (using the light sensor), changing the LED from red (chest closed) to green (chest open) and playing the "da da da DAAAAA" from the Zelda games on the buzzer.
I've got most of the code working. It does detect light, and when it does, the LED changes from red to green and plays the tune. My problem is that it keeps playing the tune over and over, and I can't figure out how to get it to stop. I think the problem may be that the buzzer tone function is in void loop, but I'm not sure where else to put it. If someone could help me get this thing working I would really appreciate it.
Here's my code:
int buzzer = 2;
int light = A0;
int red = 3;
int blue = 6;
int green = 5;
int numTones = 10;
int tones[] = {260, 260, 300, 300, 340, 340, 390, 390, 390, 390};
void setup(){
pinMode(buzzer, OUTPUT);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
}
void loop(){
int temp = analogRead(light);
if(temp > 100){
digitalWrite(red, HIGH);
digitalWrite(green, LOW);
digitalWrite(blue, HIGH);
for (int i = 0; i < numTones; i++){
tone(buzzer, tones[i]);
delay(150);
}
noTone(buzzer);
}
else {
digitalWrite(red, LOW);
digitalWrite(green, HIGH);
digitalWrite(blue, HIGH);
}
}