#include <Keypad.h>

// Define the number of rows and columns on the keypad
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns

// Define the keymap
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

// Connect keypad ROW0, ROW1, ROW2, ROW3 to these Arduino pins.
byte rowPins[ROWS] = {1,2,3,4}; 

// Connect keypad COL0, COL1, COL2, COL3 to these Arduino pins.
byte colPins[COLS] = {5,6,7,8}; 

// Create the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

const int potPin = A0; // Analog pin for potentiometer (SIG)
String inputString = ""; // String to hold the input from the keypad
boolean newInput = false;

void setup() {
  Serial.begin(9600); // Start the serial communication
  inputString.reserve(10); // Reserve 10 bytes for the input string
}

void loop() {
  char key = keypad.getKey();

  if (key) {
    if (key == '#') {
      // If '#' is pressed, convert the input string to a number
      int factor = inputString.toInt();
      inputString = "";
      newInput = true;
      
      // Read the voltage from the potentiometer
      int sensorValue = analogRead(potPin);
      float voltage = sensorValue * (3.3 / 1023.0); // Assuming a 3.3V reference
      
      // Multiply the voltage by the factor
      float result = voltage * factor;
      
      // Print the result to the serial monitor
      Serial.print("Multiplied Voltage: ");
      Serial.println(result);
    } else if (key == '*') {
      // Clear the input string if '*' is pressed
      inputString = "";
      newInput = false;
    } else {
      // Append the pressed key to the input string
      inputString += key;
    }

    Serial.print("Input: ");
    Serial.println(inputString);
  }

  delay(100); // Small delay to debounce the keypad
}