/*
  ESP32 NOTE Explorer ♩ ♪ ♫ ♬
  Plays all ledc notes that are available
  https://wokwi.com/projects/351231798778266200
  by dlloydev, December 2022.

  Continuous play: Press & hold "Repeat" button while moving
                   mouse pointer away, then release.
*/

#include <LiquidCrystal_I2C.h>
#include "pwmWrite.h"

const int tonePin = 4;
const int notePin = 12;
const int octavePin = 13;
const int repeatPin = 23;
const int notes = 12;
const int octaves = 8;
const char *lcdNote[notes] = { "C ", "Cs", "D ", "Eb", "E ", "F ", "Fs", "G ", "Gs", "A ", "Bb", "B " };
const note_t note[notes] = { NOTE_C, NOTE_Cs, NOTE_D, NOTE_Eb, NOTE_E, NOTE_F, NOTE_Fs, NOTE_G, NOTE_Gs, NOTE_A, NOTE_Bb, NOTE_B };
const int octave[octaves] = { 1, 2, 3, 4, 5, 6, 7, 8 };
const int duration = 700;  // note duration ms
const int interval = 0;    // pause ms

byte sound[] = { // ♬
  B00001,
  B00011,
  B00101,
  B01001,
  B01001,
  B01011,
  B11011,
  B11000
};

LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
Pwm pwm = Pwm();

void setup() {
  Serial.begin(115200);
  pinMode(repeatPin, INPUT_PULLUP);
  LCD.init();
  LCD.backlight();
  LCD.createChar(0, sound);
  LCD.setCursor(0, 0);
  LCD.write(0);
  LCD.print("  ");
  LCD.write(0);
  LCD.print("  ");
  LCD.write(0);
  LCD.print(" NOTE_  ");
  LCD.setCursor(1, 1);
  LCD.write(0);
  LCD.print("  ");
  LCD.write(0);
  LCD.print("   Octave  ");
  pinMode(notePin, INPUT);
  pinMode(octavePin, INPUT);
  pwm.attach(notePin, 2);  // attach to ch 2
  //pwm.printDebug();
}

void loop() {
  static int nip = 1, oip = 1;
  int noteRead = analogRead(notePin);
  int octaveRead = analogRead(octavePin);
  int repeat = !digitalRead(repeatPin);

  int ni = map(noteRead, 0, 4095, 0, notes - 1);
  if (nip != ni) {
    nip = ni;
    LCD.setCursor(13, 0);
    LCD.print(lcdNote[ni]);
  }
  int oi = map(octaveRead, 0, 4095, 0, octaves - 1);
  if (oip != oi) {
    oip = oi;
    LCD.setCursor(15, 1);
    LCD.print(oi + 1);
  }
  if (repeat) pwm.resume();
  pwm.note(tonePin, (note_t)note[ni], octave[oi], duration, interval);  // auto attaches to ch 0
  delay(5);
}
esp:VIN
esp:GND.2
esp:D13
esp:D12
esp:D14
esp:D27
esp:D26
esp:D25
esp:D33
esp:D32
esp:D35
esp:D34
esp:VN
esp:VP
esp:EN
esp:3V3
esp:GND.1
esp:D15
esp:D2
esp:D4
esp:RX2
esp:TX2
esp:D5
esp:D18
esp:D19
esp:D21
esp:RX0
esp:TX0
esp:D22
esp:D23
bz1:1
bz1:2
pot1:VCC
pot1:SIG
pot1:GND
NOTE_B NOTE_Bb NOTE_A NOTE_Gs NOTE_G NOTE_Fs NOTE_F NOTE_E NOTE_Eb NOTE_D NOTE_Cs NOTE_C
pot2:VCC
pot2:SIG
pot2:GND
8 7 6 5 4 3 2 1
Octave
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
Repeat ▷