Advertisement
pleasedontcode

"Button Memory" rev_04

Dec 29th, 2023
70
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: "Button Memory"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2023-12-29 10:04:47
  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 and save to EEPROM when pushbutton P1 */
  22.     /* is OFF then stop reading values of AI1 and AI2 and */
  23.     /* print on serial all saved EEPROM values. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <Arduino.h>
  28. #include <EasyButton.h>
  29. #include <EEPROM.h>
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void saveToEEPROM();
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t P1_PushButton_PIN_D2 = 2;
  38.  
  39. /***** DEFINITION OF ANALOG INPUT PINS *****/
  40. const uint8_t AI1_Potentiometer_Vout_PIN_A1 = A1;
  41. const uint8_t AI2_Potentiometer_Vout_PIN_A0 = A0;
  42.  
  43. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  44. EasyButton button(P1_PushButton_PIN_D2);
  45.  
  46. void setup(void)
  47. {
  48.     // put your setup code here, to run once:
  49.     pinMode(P1_PushButton_PIN_D2, INPUT_PULLUP);
  50.     pinMode(AI1_Potentiometer_Vout_PIN_A1, INPUT);
  51.     pinMode(AI2_Potentiometer_Vout_PIN_A0, INPUT);
  52.  
  53.     Serial.begin(115200);
  54.     Serial.println();
  55.     Serial.println(">>> EEPROM Example <<<");
  56.  
  57.     button.begin();
  58. }
  59.  
  60. void loop(void)
  61. {
  62.     // put your main code here, to run repeatedly:
  63.     button.read();
  64.  
  65.     // Check if pushbutton P1 is pressed
  66.     if (button.isPressed())
  67.     {
  68.         // Read values of AI1 and AI2
  69.         int ai1Value = analogRead(AI1_Potentiometer_Vout_PIN_A1);
  70.         int ai2Value = analogRead(AI2_Potentiometer_Vout_PIN_A0);
  71.  
  72.         // Save values to EEPROM
  73.         EEPROM.put(0, ai1Value);
  74.         EEPROM.put(sizeof(int), ai2Value);
  75.     }
  76.     else
  77.     {
  78.         // Check if pushbutton P1 is released
  79.         if (button.isReleased())
  80.         {
  81.             // Stop reading values of AI1 and AI2
  82.             button.disableInterrupt();
  83.         }
  84.  
  85.         // Print saved EEPROM values
  86.         int ai1Value;
  87.         int ai2Value;
  88.         EEPROM.get(0, ai1Value);
  89.         EEPROM.get(sizeof(int), ai2Value);
  90.         Serial.print("AI1 Value: ");
  91.         Serial.println(ai1Value);
  92.         Serial.print("AI2 Value: ");
  93.         Serial.println(ai2Value);
  94.     }
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement