Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* 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: "Analog Storage"
- - Source Code compiled for: Arduino Uno
- - Source Code created on: 2023-12-29 10:49:05
- ********* 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 <Arduino.h>
- #include <Wire.h>
- #include <DFRobot_ADS1115.h>
- #include <EEPROM.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void saveToEEPROM(float value, int address);
- float readFromEEPROM(int address);
- /***** 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*****/
- DFRobot_ADS1115 ads(&Wire);
- /****** DEFINITION OF EEPROM ADDRESSES *****/
- const int AI1_EEPROM_ADDRESS = 0;
- const int AI2_EEPROM_ADDRESS = 4;
- /****** GLOBAL VARIABLES *****/
- bool isSavingEnabled = false;
- void setup(void)
- {
- // put your setup code here, to run once:
- Serial.begin(115200);
- ads.setGain(eGAIN_TWOTHIRDS);
- ads.setMode(eMODE_SINGLE);
- ads.setRate(eRATE_128);
- ads.setOSMode(eOSMODE_SINGLE);
- ads.init();
- pinMode(P1_PushButton_PIN_D2, INPUT_PULLUP);
- pinMode(AI1_Potentiometer_Vout_PIN_A1, INPUT);
- pinMode(AI2_Potentiometer_Vout_PIN_A0, INPUT);
- }
- void loop(void)
- {
- // Check if pushbutton P1 is pressed
- if (digitalRead(P1_PushButton_PIN_D2) == LOW)
- {
- isSavingEnabled = true;
- delay(10); // Debounce delay
- }
- else
- {
- isSavingEnabled = false;
- }
- if (isSavingEnabled)
- {
- // Read and save values from AI1 and AI2 to EEPROM
- float ai1Value = ads.readVoltage(AI1_Potentiometer_Vout_PIN_A1);
- saveToEEPROM(ai1Value, AI1_EEPROM_ADDRESS);
- float ai2Value = ads.readVoltage(AI2_Potentiometer_Vout_PIN_A0);
- saveToEEPROM(ai2Value, AI2_EEPROM_ADDRESS);
- }
- else
- {
- // Print all saved EEPROM values to serial
- float ai1SavedValue = readFromEEPROM(AI1_EEPROM_ADDRESS);
- float ai2SavedValue = readFromEEPROM(AI2_EEPROM_ADDRESS);
- Serial.print("AI1 Saved Value: ");
- Serial.println(ai1SavedValue);
- Serial.print("AI2 Saved Value: ");
- Serial.println(ai2SavedValue);
- }
- delay(1000);
- }
- void saveToEEPROM(float value, int address)
- {
- // Convert float value to byte array representation
- byte* valueBytes = (byte*) &value;
- // Write each byte of the value to EEPROM
- for (int i = 0; i < sizeof(float); i++)
- {
- EEPROM.write(address + i, valueBytes[i]);
- }
- }
- float readFromEEPROM(int address)
- {
- // Read each byte of the value from EEPROM
- byte valueBytes[sizeof(float)];
- for (int i = 0; i < sizeof(float); i++)
- {
- valueBytes[i] = EEPROM.read(address + i);
- }
- // Convert byte array back to float value
- float value = *((float*) valueBytes);
- return value;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement