Advertisement
pleasedontcode

"Arduino Library" rev_05

Dec 29th, 2023
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Arduino Library"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2023-12-29 10:22:18
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* If pushbutton P1 is pressed, read the values of */
  21.     /* AI1 and AI2 in float and save to EEPROM when */
  22.     /* pushbutton P1 is OFF then stop reading values of */
  23.     /* AI1 and AI2 and print on serial all saved EEPROM */
  24.     /* values in float. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Arduino.h>
  29. #include <EasyButton.h>
  30. #include <EEPROM.h>
  31.  
  32. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  33. const uint8_t P1_PushButton_PIN_D2 = 2;
  34.  
  35. /***** DEFINITION OF ANALOG INPUT PINS *****/
  36. const uint8_t AI1_Potentiometer_Vout_PIN_A1 = A1;
  37. const uint8_t AI2_Potentiometer_Vout_PIN_A0 = A0;
  38.  
  39. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  40. EasyButton P1_PushButton(P1_PushButton_PIN_D2);
  41.  
  42. template<typename T>
  43. class EEPROMStorage
  44. {
  45. private:
  46.   int _address;
  47. public:
  48.   EEPROMStorage(int address)
  49.   {
  50.     _address = address;
  51.   }
  52.  
  53.   void write(T value)
  54.   {
  55.     EEPROM.put(_address, value);
  56.   }
  57.  
  58.   T read()
  59.   {
  60.     T value;
  61.     EEPROM.get(_address, value);
  62.     return value;
  63.   }
  64. };
  65.  
  66. EEPROMStorage<float> AI1_Storage(0);
  67. EEPROMStorage<float> AI2_Storage(1);
  68.  
  69. bool readingValuesInProgress = false;
  70.  
  71. void saveValuesToEEPROM()
  72. {
  73.   float ai1Value = analogRead(AI1_Potentiometer_Vout_PIN_A1) * (5.0 / 1023.0);
  74.   float ai2Value = analogRead(AI2_Potentiometer_Vout_PIN_A0) * (5.0 / 1023.0);
  75.  
  76.   AI1_Storage.write(ai1Value);
  77.   AI2_Storage.write(ai2Value);
  78. }
  79.  
  80. void printEEPROMValues()
  81. {
  82.   int numValues = EEPROM.length() / sizeof(float);
  83.  
  84.   Serial.println();
  85.   Serial.println("EEPROM values:");
  86.   for (int i = 0; i < numValues; i++)
  87.   {
  88.     float value;
  89.     EEPROM.get(i * sizeof(float), value);
  90.     Serial.print("Address ");
  91.     Serial.print(i * sizeof(float));
  92.     Serial.print(": ");
  93.     Serial.println(value);
  94.   }
  95.   Serial.println();
  96. }
  97.  
  98. void setup()
  99. {
  100.   // put your setup code here, to run once:
  101.   pinMode(AI1_Potentiometer_Vout_PIN_A1, INPUT);
  102.   pinMode(AI2_Potentiometer_Vout_PIN_A0, INPUT);
  103.  
  104.   P1_PushButton.begin();
  105.  
  106.   Serial.begin(115200);
  107. }
  108.  
  109. void loop()
  110. {
  111.   // put your main code here, to run repeatedly:
  112.   P1_PushButton.read();
  113.  
  114.   if (P1_PushButton.wasPressed() && !readingValuesInProgress)
  115.   {
  116.     readingValuesInProgress = true;
  117.     saveValuesToEEPROM();
  118.   }
  119.   else if (P1_PushButton.wasReleased() && readingValuesInProgress)
  120.   {
  121.     readingValuesInProgress = false;
  122.     printEEPROMValues();
  123.   }
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement