Save value with button

Hi!
I’m trying to save a value with a press of a button! I’m new to arduino!

I’m getting a value from a humidity sensor.

I want to save that value with a press
Of a button so arduino will know when to open the relay the next time that value is the same!

Best regards

where do you want to save it? EEPROM?
have you looked at any button tutorials?
have you looked at the examples of the EEPROM class?

What actual problem are you having. :thinking:


A closed switch can be detected by reading its state with digitalRead( ).

When the switch is closed, you can simply store whatever values you need.

Here is example code showing how to use the state change detection to sense the transition of the button switch from unpressed to pressed to actuate the reading and saving of the humidity value.

// by C Goulding aka groundFungus

const byte  buttonPin = 2;    // the pin to which the pushbutton is attached

void setup()
{
   // initialize the button pin as a input with internal pullup enabled
   pinMode(buttonPin, INPUT_PULLUP);
    
   // initialize serial communication:
   Serial.begin(115200);
   Serial.println("Read and save humidity with push button");
}

void loop()
{   
   static bool lastButtonState = 0;     // previous state of the button
   static unsigned long timer = 0;
   unsigned long interval = 50;  // check switch 20 times per second
   if (millis() - timer >= interval)
   {
      timer = millis();
      // read the pushbutton input pin:
      bool buttonState = digitalRead(buttonPin);
      // compare the new buttonState to its previous state
      if (buttonState != lastButtonState)
      {
         if (buttonState == LOW)
         {
            // if the current state is LOW then the button            
            Serial.println("Reading and saving humidity");

            // *********************************************************
            // read the humidity sensor and save the value
            //**********************************************************
            
         }
      }
      // save the current state as the last state,
      //for next time through the loop
      lastButtonState = buttonState;
   }
}

See the state change for active low inputs tutorial for button switch wiring.

I read something about EEPROM. But I lack the experience of using it :frowning:

I thank u! Have to try it out :slight_smile:

There are examples ready to be explored :wink: