// Analog StateChange Detection for
// https://wokwi.com/projects/391124361363049473
// for https://forum.arduino.cc/t/esp8266-analog-input-triggering-issue-for-one-shot/1229757/24
// and // and https://forum.arduino.cc/t/using-analog-input-to-both-turn-on-and-turn-off-a-digital-output/1292092/5

int lastButtonState = 0; // integer to remeber a range of distinct states

const int AnalogPin = A2;
const int SensorPin = A0;

int currentButtonState = 0;
int currentSensorState;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

void setup() {
  Serial.begin(115200);
}

void loop() {
  checkAndReportSliderA();
  checkAndReportSensor2();

}

bool checkAndReportSensor2() {
  bool changed = false;
  static int lastSensorState = -1;
  currentSensorState = analogRead(SensorPin);
  if (currentSensorState != lastSensorState) {
    changed = true;
    lastSensorState = currentSensorState;
    Serial.print(currentSensorState);
    Serial.print(" ");
  }
}

void checkAndReportSliderA() {
  // This checks an analog input,
  // bins it into categories,
  // and prints a message when the category changes
  const long NumCategories = 52;
  const int MaxADC = 1023;

  currentButtonState = analogRead(AnalogPin) * NumCategories / (MaxADC + 1); // transform into distinct ranges
  if (currentButtonState != lastButtonState && millis() - lastDebounceTime >= debounceDelay) {
    // The input has changed
    lastDebounceTime = millis(); // record the time for rate limiting
    Serial.print(num2azAZ(currentButtonState));
    lastButtonState = currentButtonState;
  }
}

char num2azAZ(int num) {
  char ch = num < 26 ?   char('a' + num) : char('A' + num - 26);
  return ch;
}