So I have a university project due in a few days and we want the servo to be in the 0 degree position and make a tune for example, 5 secs then using a potentiometer we twist to a 1023 position (180 degrees) and it plays a different tune and then stops. The problem is stopping the buzzer after a certain time period,
We also want the sounds to only come on at these specific points.
PLEASE HELP!
This is our code.
#include <Servo.h> // add servo library
Servo myservo; // create servo object to control a servo
int potpin = A0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int piezo= 8;
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode (potpin, INPUT);
pinMode (piezo, OUTPUT);
Serial.begin (9600);
}
void loop() {
val = analogRead(potpin);
Serial.println (val);// reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
if (val==0)
{
tone (piezo, 1000,500);
delay(1000);
noTone(piezo);
delay (50);
}
else if (piezo, 0);{
//analogWrite (piezo, 0);
}
if (val==1023)
{
tone (piezo, 3000,500);
delay (200);
}
else if( piezo, 0);{
//analogWrite (piezo, 0);
}
}