CCCPaste

"Arduino Library" rev_05

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "Arduino Library"
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2023-12-29 10:22:18

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* If pushbutton P1 is pressed, read the values of */
	/* AI1 and AI2 in float and save to EEPROM when */
	/* pushbutton P1 is OFF then stop reading values of */
	/* AI1 and AI2 and print on serial all saved EEPROM */
	/* values in float. */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include 
#include 

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t P1_PushButton_PIN_D2 = 2;

/***** DEFINITION OF ANALOG INPUT PINS *****/
const uint8_t AI1_Potentiometer_Vout_PIN_A1 = A1;
const uint8_t AI2_Potentiometer_Vout_PIN_A0 = A0;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
EasyButton P1_PushButton(P1_PushButton_PIN_D2);

template
class EEPROMStorage
{
private:
  int _address;
public:
  EEPROMStorage(int address)
  {
    _address = address;
  }

  void write(T value)
  {
    EEPROM.put(_address, value);
  }

  T read()
  {
    T value;
    EEPROM.get(_address, value);
    return value;
  }
};

EEPROMStorage AI1_Storage(0);
EEPROMStorage AI2_Storage(1);

bool readingValuesInProgress = false;

void saveValuesToEEPROM()
{
  float ai1Value = analogRead(AI1_Potentiometer_Vout_PIN_A1) * (5.0 / 1023.0);
  float ai2Value = analogRead(AI2_Potentiometer_Vout_PIN_A0) * (5.0 / 1023.0);

  AI1_Storage.write(ai1Value);
  AI2_Storage.write(ai2Value);
}

void printEEPROMValues()
{
  int numValues = EEPROM.length() / sizeof(float);

  Serial.println();
  Serial.println("EEPROM values:");
  for (int i = 0; i < numValues; i++)
  {
    float value;
    EEPROM.get(i * sizeof(float), value);
    Serial.print("Address ");
    Serial.print(i * sizeof(float));
    Serial.print(": ");
    Serial.println(value);
  }
  Serial.println();
}

void setup()
{
  // put your setup code here, to run once:
  pinMode(AI1_Potentiometer_Vout_PIN_A1, INPUT);
  pinMode(AI2_Potentiometer_Vout_PIN_A0, INPUT);

  P1_PushButton.begin();

  Serial.begin(115200);
}

void loop()
{
  // put your main code here, to run repeatedly:
  P1_PushButton.read();

  if (P1_PushButton.wasPressed() && !readingValuesInProgress)
  {
    readingValuesInProgress = true;
    saveValuesToEEPROM();
  }
  else if (P1_PushButton.wasReleased() && readingValuesInProgress)
  {
    readingValuesInProgress = false;
    printEEPROMValues();
  }
}