/**
 * This file is used for debugging the analog values of the pins on the ESP32.
 * It reads the analog values from the specified pins and prints them to the Serial Monitor.
 * The values are color-coded based on a defined threshold.
 * Values below the threshold are printed in green, while values above the threshold are printed in red.
 */
#include <Arduino.h>

#define THRESHOLD 3000  // Define the threshold value for analog readings

//const int pinList[] = {34, 35, 32, 33, 25, 26, 27, 14, 15, 2, 0, 4};  // Array of the pins you want to use
const int pinList[] = {4, 0, 2, 15, 14, 27, 26, 25, 33, 32, 35, 34};  // Array of the pins you want to use
const int numPins = sizeof(pinList) / sizeof(pinList[0]);  // Number of pins in the array
const char* noteList[] = {"C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B"};  // Array of note names

int ArrayStates[numPins];  // Array to store previous states of pins
int LastSentStates[numPins];  // Array to store last sent states of pins
float ArrayValues[numPins];  // Array to store analog values of pins

bool isArrayEmpty(int array[], int size) {
    for (int i = 0; i < size; i++) {
        if (array[i] != 0) {  // Check if any element is not zero
        return false;  // Array is not empty
        }
    }
    return true;  // Array is empty
}

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

  // Initialize each pin in the pinList as an input
  for (int i = 0; i < numPins; i++) {
    pinMode(pinList[i], INPUT);  // Set pins as input
    ArrayStates[i] = 0;  // Set initial state to 0
  }
  Serial.println("Starting up...");  // Print a message indicating startup
}

void loop() {
  for (int i = 0; i < numPins; i++) {
    int analogValue = analogRead(pinList[i]);  // Read the analog value of the pin
    ArrayStates[i] = (analogValue < THRESHOLD) ? 1 : 0;  // Compare with threshold
    ArrayValues[i] = analogValue;  // Store the analog value
  }
  if (memcmp(ArrayStates, LastSentStates, sizeof(ArrayStates)) != 0) {
    memcpy(LastSentStates, ArrayStates, sizeof(ArrayStates));  // Update last sent states
    if (isArrayEmpty(ArrayStates, numPins) == false) {
        for (int i = 0; i < numPins; i++) {
            if (ArrayStates[i] == 1) {
                Serial.print(noteList[i]);  // Print the note name if the state is 1
                Serial.print(" ");  // Add a space between note names
            }
        }
        Serial.println();  // Print a new line after printing all note names
        delay(250); //avoid flickering in the sensing
    }
  }
}