const int T_C = 262;
const int T_D = 294;
const int T_E = 330;
const int T_F = 349;
const int T_G = 392;
const int T_A = 440;
const int T_B = 493;
#include <Servo.h>
Servo servo1;
const int Buzz = 11;
const int LED = 13;
#define potPin A0
const int C = 10;
const int D = 9;
const int E = 8;
const int F = 7;
const int G = 6;
const int A = 5;
const int B = 4;
unsigned long lastNoteTime = 0;
int currentNote = 0;
void setup()
{
servo1.attach(3);
pinMode(LED, OUTPUT);
pinMode(C, INPUT);
digitalWrite(C, HIGH);
pinMode(D, INPUT);
digitalWrite(D, HIGH);
pinMode(E, INPUT);
digitalWrite(E, HIGH);
pinMode(F, INPUT);
digitalWrite(F, HIGH);
pinMode(G, INPUT);
digitalWrite(G, HIGH);
pinMode(A, INPUT);
digitalWrite(A, HIGH);
pinMode(B, INPUT);
digitalWrite(B, HIGH);
digitalWrite(LED, LOW);
}
void playNote(int note, int duration)
{
tone(Buzz, note);
digitalWrite(LED, HIGH);
lastNoteTime = millis();
currentNote = note;
}
void stopNote()
{
noTone(Buzz);
digitalWrite(LED, LOW);
}
void loop()
{
int potValue = analogRead(potPin);
int potValue1 = potValue / 5.7;
if (potValue1 > -0.1 && potValue1 < 45)
servo1.write(0);
else if (potValue1 > 45 && potValue1 < 90)
servo1.write(90);
else if (potValue1 > 135)
servo1.write(180);
if (digitalRead(C) == LOW)
playNote(T_C + potValue, 70);
if (digitalRead(D) == LOW)
playNote(T_D + potValue, 70);
if (digitalRead(E) == LOW)
playNote(T_E + potValue, 70);
if (digitalRead(F) == LOW)
playNote(T_F + potValue, 70);
if (digitalRead(G) == LOW)
playNote(T_G + potValue, 70);
if (digitalRead(A) == LOW)
playNote(T_A + potValue, 70);
if (digitalRead(B) == LOW)
playNote(T_B + potValue, 70);
// Check if it's time to stop the note
if (currentNote != 0 && millis() - lastNoteTime >= 70)
{
stopNote();
currentNote = 0;
}
}