Advertisement
pleasedontcode

"Analog Storage" rev_08

Dec 29th, 2023
82
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: "Analog Storage"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2023-12-29 10:49:05
  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 <Wire.h>
  30. #include <DFRobot_ADS1115.h>
  31. #include <EEPROM.h>
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36. void saveToEEPROM(float value, int address);
  37. float readFromEEPROM(int address);
  38.  
  39. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  40. const uint8_t P1_PushButton_PIN_D2 = 2;
  41.  
  42. /***** DEFINITION OF ANALOG INPUT PINS *****/
  43. const uint8_t AI1_Potentiometer_Vout_PIN_A1 = A1;
  44. const uint8_t AI2_Potentiometer_Vout_PIN_A0 = A0;
  45.  
  46. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  47. DFRobot_ADS1115 ads(&Wire);
  48.  
  49. /****** DEFINITION OF EEPROM ADDRESSES *****/
  50. const int AI1_EEPROM_ADDRESS = 0;
  51. const int AI2_EEPROM_ADDRESS = 4;
  52.  
  53. /****** GLOBAL VARIABLES *****/
  54. bool isSavingEnabled = false;
  55.  
  56. void setup(void)
  57. {
  58.     // put your setup code here, to run once:
  59.     Serial.begin(115200);
  60.  
  61.     ads.setGain(eGAIN_TWOTHIRDS);
  62.     ads.setMode(eMODE_SINGLE);
  63.     ads.setRate(eRATE_128);
  64.     ads.setOSMode(eOSMODE_SINGLE);
  65.     ads.init();
  66.  
  67.     pinMode(P1_PushButton_PIN_D2, INPUT_PULLUP);
  68.     pinMode(AI1_Potentiometer_Vout_PIN_A1, INPUT);
  69.     pinMode(AI2_Potentiometer_Vout_PIN_A0, INPUT);
  70. }
  71.  
  72. void loop(void)
  73. {
  74.     // Check if pushbutton P1 is pressed
  75.     if (digitalRead(P1_PushButton_PIN_D2) == LOW)
  76.     {
  77.         isSavingEnabled = true;
  78.         delay(10); // Debounce delay
  79.     }
  80.     else
  81.     {
  82.         isSavingEnabled = false;
  83.     }
  84.  
  85.     if (isSavingEnabled)
  86.     {
  87.         // Read and save values from AI1 and AI2 to EEPROM
  88.         float ai1Value = ads.readVoltage(AI1_Potentiometer_Vout_PIN_A1);
  89.         saveToEEPROM(ai1Value, AI1_EEPROM_ADDRESS);
  90.  
  91.         float ai2Value = ads.readVoltage(AI2_Potentiometer_Vout_PIN_A0);
  92.         saveToEEPROM(ai2Value, AI2_EEPROM_ADDRESS);
  93.     }
  94.     else
  95.     {
  96.         // Print all saved EEPROM values to serial
  97.         float ai1SavedValue = readFromEEPROM(AI1_EEPROM_ADDRESS);
  98.         float ai2SavedValue = readFromEEPROM(AI2_EEPROM_ADDRESS);
  99.  
  100.         Serial.print("AI1 Saved Value: ");
  101.         Serial.println(ai1SavedValue);
  102.  
  103.         Serial.print("AI2 Saved Value: ");
  104.         Serial.println(ai2SavedValue);
  105.     }
  106.  
  107.     delay(1000);
  108. }
  109.  
  110. void saveToEEPROM(float value, int address)
  111. {
  112.     // Convert float value to byte array representation
  113.     byte* valueBytes = (byte*) &value;
  114.  
  115.     // Write each byte of the value to EEPROM
  116.     for (int i = 0; i < sizeof(float); i++)
  117.     {
  118.         EEPROM.write(address + i, valueBytes[i]);
  119.     }
  120. }
  121.  
  122. float readFromEEPROM(int address)
  123. {
  124.     // Read each byte of the value from EEPROM
  125.     byte valueBytes[sizeof(float)];
  126.     for (int i = 0; i < sizeof(float); i++)
  127.     {
  128.         valueBytes[i] = EEPROM.read(address + i);
  129.     }
  130.  
  131.     // Convert byte array back to float value
  132.     float value = *((float*) valueBytes);
  133.  
  134.     return value;
  135. }
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement