-
Notifications
You must be signed in to change notification settings - Fork 1
/
Piano.ino
126 lines (107 loc) · 3.36 KB
/
Piano.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <WiFi.h>
#include <WebServer.h>
// Wi-Fi credentials
const char* ssid = "RFID";
const char* password = "30678921";
// Pin where the buzzer is connected
#define BUZZER_PIN 14
// Note frequencies (in Hz) for 25 keys (C4 to C6)
const int NOTE_FREQUENCIES[] = {
261, // C4
277, // C#4
293, // D4
311, // D#4
329, // E4
349, // F4
370, // F#4
392, // G4
415, // G#4
440, // A4
466, // A#4
493, // B4
523, // C5
554, // C#5
587, // D5
622, // D#5
659, // E5
698, // F5
740, // F#5
784, // G5
831, // G#5
880, // A5
932, // A#5
987, // B5
1046 // C6
};
// Create a web server object
WebServer server(80);
// Function to play the tone
void playTone(int frequency, int duration) {
tone(BUZZER_PIN, frequency); // Start playing the tone
delay(duration);
noTone(BUZZER_PIN); // Stop the tone
}
// Function to handle root URL ("/")
void handleRoot() {
String html = "<html><head>";
html += "<style>";
html += "body { display: flex; justify-content: center; align-items: flex-end; height: 100vh; background: #333; margin: 0; overflow: hidden; }";
html += ".key { width: 50px; height: 250px; margin: 0 1px; background: white; border: 1px solid #000; position: relative; }";
html += ".key.black { width: 30px; height: 150px; background: black; position: absolute; left: 35px; z-index: 1; margin-left: -15px; }";
html += ".key:hover { background: #ddd; }";
html += ".key.black:hover { background: #444; }";
// Media queries for responsive design
html += "@media (max-width: 600px) {";
html += ".key { width: 40px; height: 200px; }";
html += ".key.black { width: 25px; height: 120px; }";
html += "}";
html += "@media (max-width: 400px) {";
html += ".key { width: 30px; height: 180px; }";
html += ".key.black { width: 20px; height: 100px; }";
html += "}";
html += "</style></head><body>";
// Add buttons for each note (white keys)
for (int i = 0; i < 25; i++) {
html += "<div class='key' onclick='playNote(" + String(i) + ")'></div>"; // White key
// Add black keys where appropriate
if (i % 7 == 2 || i % 7 == 5 || i % 7 == 9 || i % 7 == 12 || i % 7 == 16) {
html += "<div class='key black' onclick='playNote(" + String(i) + ")'></div>"; // Black key
}
}
html += "<script>";
html += "function playNote(note) { fetch('/play?note=' + note); }"; // JavaScript function to play note
html += "</script>";
html += "</body></html>";
server.send(200, "text/html", html);
}
// Function to handle note playback via URL
void handlePlayNote() {
if (server.hasArg("note")) {
int noteIndex = server.arg("note").toInt();
if (noteIndex >= 0 && noteIndex < 25) {
int frequency = NOTE_FREQUENCIES[noteIndex];
playTone(frequency, 500); // Play the note for 500ms
}
}
server.send(200, "text/plain", "Note played");
}
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to Wi-Fi...");
}
Serial.println("Connected to Wi-Fi");
Serial.println(WiFi.localIP()); // Print the local IP address
// Setup the web server
server.on("/", handleRoot);
server.on("/play", handlePlayNote);
server.begin();
}
void loop() {
// Handle incoming client requests
server.handleClient();
}