//This code programs the keypad EEPROM (key) address and character to be stored
//Only Keypads 7/15/2021 by Code Quaker

#include <Wire.h>
#include <EEPROM.h>
#define eeprom 0X50 //I2C buss address of the EEPROM 0X50
int eepromAddress;
int eepromAscii;
char character;
int ASCIIvalue;

void setup() {
 Wire.begin(); //creates a wire object
 Serial.begin(9600); 
}

void serial_flush_buffer(){
    //The "for" statement keeps firing the "while" loop multiple times to ensure flushing of the Serial buffer.
  for (int f = 0; f<20; f++){
    //Serial.print(f);
    //The "while" loop will keep looping Serial.read until the buffer is equal to zero
    //In other words until the "while" statement is false (not greater than zero) 
  while (Serial.available()>0){ 
  Serial.read();
   }
  }
    //Serial.println("  Finished flushing EEPROM buffer");
}

void loop() {

  delay(1000);
 
  Serial.println("Enter EEPROM address location to store character");
  while (Serial.available()<1){} //Wait here for address location input from serial monitor (<1 checks serial buffer is empty)
  Serial.print("You entered an EEPROM address of: ");
  eepromAddress = Serial.parseInt(); //returns valid integer number from serial buffer to variable
  Serial.println(eepromAddress);
  Serial.print("");
  delay(1000);
  serial_flush_buffer(); //Call buffer flush function
  
  Serial.println ("Enter ASCII code - decimal value - for the character to store in EEPROM address: ");
  while (Serial.available()<1){} //Wait here for character input from serial monitor (<1 checks serial buffer is empty)
  Serial.print("Entered the ASCII code - decimal - value of: ");
  eepromAscii = Serial.parseInt(); //copies - decimal value - from serial buffer to variable
  Serial.println(eepromAscii);
  character = eepromAscii;
  Serial.print("ASCII code - decimal value - converted to character: ");
  Serial.println(character);
  Serial.print("");
  delay(1000);
  eeprom_write_func(); //Call function to write data to EEPROM 
  serial_flush_buffer(); //Call buffer flush function
   
}
