char val; // Data received from the serial port int ledPin = 14; // Set the pin to digital I/O 13 //int playing = 0; //void tone(byte pin, double freq) { //ledcSetup(0, freq, 8); // setup beeper //ledcAttachPin(pin, 0); // attach beeper //ledcWriteTone(0, freq); // play tone //playing = pin; // store pin //} int pin = 25; int freq = 1047; int channel = 0; int speed=90; //higher value, slower notes int playing = 0; void tone(byte pin, int freq) { ledcSetup(0, 2000, 8); // setup beeper ledcAttachPin(pin, 0); // attach beeper ledcWriteTone(0, freq); // play tone playing = pin; // store pin } void noTone() { tone(playing, 0); } void setup() { Serial.begin(115200); //ledcSetup(channel, freq, 8); // setup beeper //ledcAttachPin(pin, channel); // attach beeper //ledcWriteTone(channel,freq); } void loop() { if (Serial.available()) { // If data is available to read, val = Serial.read(); // read it and store it in val Serial.print(val); } if (val == '1') { // If 1 was received freq = 294; tone(25, freq); //ledcWrite(channel, 128); // play tone //delay(100); //ledcWriteTone(0, 0); Serial.println("played tone"); } else if (val == '2') { // If 1 was received freq = 349; tone(25, freq); //ledcWrite(channel, 128); // play tone //delay(100); //ledcWriteTone(0, 0); Serial.println("played tone"); } else if (val == '3') { // If 1 was received freq = 370; tone(25, freq); //ledcWrite(channel, 128); // play tone //delay(100); //ledcWriteTone(0, 0); Serial.println("played tone"); } else if (val == '4') { // If 1 was received freq = 440; tone(25, freq); //ledcWrite(channel, 128); // play tone //delay(100); //ledcWriteTone(0, 0); Serial.println("played tone"); } else { //digitalWrite(ledPin, HIGH); // otherwise turn it off //ledcWriteTone(0, 0); } delay(10); // Wait 10 milliseconds for next reading // no need to repeat the melody. }